Stroustrup - Programming: Principles and Practice Using C++ - If Statement Weirdness

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 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.

Any ideas?

Thanks,
Doug
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";
Last edited on
Ganado,
Thank you. That made it work. I was trying to stay with what has been covered so far in the book and trying to do it without a cast.

Thanks,
Doug
Without a cast


?? to_string() is not a cast - it's a function that takes as a parameter a number and returns the string representation. This isn't a cast.
"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 const char*.
The '.' is char.
The '\n' is char.

1. Therefore, there are:
(const char*) + (int) + (char) + (char)

2.The pointer + int creates a new pointer

(const char*) + (char) + (char)

3. The char converts implicitly to integer, so we have an another pointer + int

(const char*) + (char)

4. Same for the last. The result is a const char*

You did not concatenate text. You did pointer math. You did add four integers to address, where the 'N' of your const string literal is.

What are the chances that that memory location has a valid C-string?
Last edited on
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).
Topic archived. No new replies allowed.