// form letter
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter the name of the person you want to write to(followed by 'enter'):\n";
string first_name; // first_name is a variable of type string
cin >> first_name; // read characters into first_name
string letter = "Dear " + first_name + ",\n";
letter += "\tHow are you doing? I am fine.\n";
cout << "Enter the name of another friend(followed by enter)\n";
string friend_name;
cin >> friend_name;
cout << "Is your friend male(m) or female(f):\n";
char friend_sex = 0;
cin >> friend_sex;
if (friend_sex == 'm')
letter += "If you see " + friend_name + " please ask him to call me.\n";
if (friend_sex == 'f')
letter += "If you see " + friend_name + " please ask her to call me.\n";
cout << "Enter the age of your friend(followed by 'enter')";
int age = 0;
cin >> age;
if (age < 1)
simple_error("you're kidding!");
if (age > 109)
simple_error("you're kidding!");
if (age < 12)
letter += "Next year you will be " + (age + 1) + '.' + '\n';
if (age == 17)
letter += "Next year you will be able to vote.\n";
if (age > 70)
letter += "I hope you are enjoying your retirement.\n";
letter += "\nYours sincerely,\n\nKonstantin Thermopylae\n";
cout << letter;
return 0;
}
It compiles without a hitch. When I run the program, it asks all the questions, but after I enter the age, it responds to all the if statements except:
1 2
if (age < 12)
letter += "Next year you will be " + (age + 1) + '.' + '\n';
If I enter an age under 12 it gets squirrely. Using the constants: Dan, Dave, m 1:
1 2 3 4 5 6 7 8
Dear Dan,
How are you doing? I am fine.
If you see Dave please ask him to call me.
o vote.
Yours sincerely,
Konstantin Thermopylae
For some reason, it is taking the last part of the following if statement.
if I enter 2 for the age I get:
vote
for 3:
vote
with 4:
ote
Until there are no letters left. From there to 11, there is no output for the statement.
I think it may have something to do with the concatenating the chars but am unsure.
Right, you can't directly do "string or string literal" + int. It doesn't know how to concatenate a string directly to an int.
You can convert (age + 1) into a string, using to_string. https://www.cplusplus.com/reference/string/to_string/
You can do: letter += "Next year you will be " + to_string(age + 1) + ".\n";
"Next year you will be " + (age + 1) + '.' + '\n';
Lets look at the type of operands in that expression.
The 'age' is int. The (age + 1) is int too.
The "Next year you will be " is constchar*.
The '.' is char.
The '\n' is char.
Responding to "trying to stay with what has been covered so far in the book": I don't know what you've covered, or the requirements of this particular problem. You could just avoid making a string in the first place, and print everything to cout.
Or, you could use a stringstream to parse out the int: https://stackoverflow.com/a/13636164
Or (C-style), you could use sprintf to convert the int to a string: https://stackoverflow.com/a/8257728
Or, you could make your own function, which takes in an int, and loops through each digit to build up a string (use a combination of % 10 and / 10 as you iterate to retrieve/remove each digit).