Skip to main content

Variables » Strings » Examples

Personal details

#include <iostream>
#include <string>

int main()
{
// Setup the four variables we will read the user's information into
std::string first_name;
std::string last_name;
std::string favorite_color;
int age;

// Ask the user 4 questions and store their answers
std::cout << "What is your first name? ";
std::cin >> first_name;

std::cout << "What is your last name? ";
std::cin >> last_name;

std::cout << "What is your age? ";
std::cin >> age;

std::cout << "What is your favorite color? ";
std::cin >> favorite_color;

// Output the user's information whilst calculating the difference between their age and first name's length
std::cout << "\n\nWelcome, " << first_name << " " << last_name << "!\n";
std::cout << "You are " << age << " years old, and your favorite color is " << favorite_color << "\n";
std::cout << "There are " << first_name.size() << " characters in your first name... ";
std::cout << (age - first_name.size()) << " less than your age!\n";
}

Team Name

#include <iostream>
#include <string>

int main()
{
std::string animal;
std::string adjective;
int team_size;

std::cout << "I'm going to help you make a team name!\n";

std::cout << "Enter an animal: ";
std::cin >> animal;

std::cout << "Enter an adjective: ";
std::cin >> adjective;

std::cout << "Enter the size of the team: ";
std::cin >> team_size;

// Form the team name by concatenating the three answers we have so far
std::string teamName = adjective + animal + std::to_string(team_size);
std::cout << "Your current team name is \"" << teamName << "\"!";

std::string pluralNoun;
std::cout << "Enter a plural noun: ";
std::cin >> pluralNoun;

// Finalize the team name by appending the plural noun
teamName += " sans " + pluralNoun;
std::cout << "Your final team name is \"" << teamName << "\"!";
}

Numbers and Strings

#include <iostream>
#include <string>

int main()
{
int a;
int b;

std::cout << "Input two numbers: ";
std::cin >> a >> b;

int sum = a + b;
// Use an int-to-string conversion to concatenate the two numbers
std::string concat_str = std::to_string(a) + std::to_string(b);

std::cout << "Their sum is " << sum << " and their concatenation is " << concat_str << "\n";

// Perform a string-to-int conversion and compute the difference
int concatVal = std::stoi(concat_str);
int diff = concatVal - sum;

std::cout << "Their difference is " << diff;
}

Variables » Strings » Examples

Personal details

#include <iostream>
#include <string>

int main()
{
// Setup the four variables we will read the user's information into
std::string first_name;
std::string last_name;
std::string favorite_color;
int age;

// Ask the user 4 questions and store their answers
std::cout << "What is your first name? ";
std::cin >> first_name;

std::cout << "What is your last name? ";
std::cin >> last_name;

std::cout << "What is your age? ";
std::cin >> age;

std::cout << "What is your favorite color? ";
std::cin >> favorite_color;

// Output the user's information whilst calculating the difference between their age and first name's length
std::cout << "\n\nWelcome, " << first_name << " " << last_name << "!\n";
std::cout << "You are " << age << " years old, and your favorite color is " << favorite_color << "\n";
std::cout << "There are " << first_name.size() << " characters in your first name... ";
std::cout << (age - first_name.size()) << " less than your age!\n";
}

Team Name

#include <iostream>
#include <string>

int main()
{
std::string animal;
std::string adjective;
int team_size;

std::cout << "I'm going to help you make a team name!\n";

std::cout << "Enter an animal: ";
std::cin >> animal;

std::cout << "Enter an adjective: ";
std::cin >> adjective;

std::cout << "Enter the size of the team: ";
std::cin >> team_size;

// Form the team name by concatenating the three answers we have so far
std::string teamName = adjective + animal + std::to_string(team_size);
std::cout << "Your current team name is \"" << teamName << "\"!";

std::string pluralNoun;
std::cout << "Enter a plural noun: ";
std::cin >> pluralNoun;

// Finalize the team name by appending the plural noun
teamName += " sans " + pluralNoun;
std::cout << "Your final team name is \"" << teamName << "\"!";
}

Numbers and Strings

#include <iostream>
#include <string>

int main()
{
int a;
int b;

std::cout << "Input two numbers: ";
std::cin >> a >> b;

int sum = a + b;
// Use an int-to-string conversion to concatenate the two numbers
std::string concat_str = std::to_string(a) + std::to_string(b);

std::cout << "Their sum is " << sum << " and their concatenation is " << concat_str << "\n";

// Perform a string-to-int conversion and compute the difference
int concatVal = std::stoi(concat_str);
int diff = concatVal - sum;

std::cout << "Their difference is " << diff;
}