trying to display a value using a string equation to assign a value to a string

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
#include <iostream>
#include <string>

using namespace std;

int main( )

{
    cout <<"Hello and welcome to Jake McGhee's calorie counter.\n"
    "All you need to do is follow the instructions.\n";

    string breakfast;
 cout<<" Okay now just type in the amount of calories you ate at breakfast and before lunch.\n";
 getline (cin,breakfast);
    string lunch;
    cout<<"Now enter the amount of Calories you ate a lunch till dinner.\n";
    getline (cin,lunch);
 string dinner;
 cout <<"Finally enter the amount of Calories you ate for dinner and anything else you ate and didn't account for yet.\n";
 getline (cin,dinner);
 string totalcal;
 breakfast+lunch+dinner= totalcal;
cout << "Here is the amount of calories you have eaten today";
<< totalcal << endl;


    cout<<"press enter to finish this program.";
    cin.ignore(2,'1');
    return 0;

}


It takes my values and I get no errors. However the totalcal string doesn't display any value. when I call it using cout. I originally used functions but decided strings were a lot let hassle. Let me know if I am way off here. And be gentle please if this is a simple mistake in my cout. I am uber new and between work, kids, school, and this I don't have as much time as I would like to devote to troubleshooting code.
No errors? I get the following error:

prog.cpp: In function β€˜int main()’:
prog.cpp:24: error: expected primary-expression before β€˜<<’ token


Anyway, you've got bigger problems than that semi-colon at the end of line 23.

breakfast and lunch and dinner are strings. Not numbers. They are strings. When you add two strings like this:

string1 + string2

you get out the concatenated strings.

Here's an example. If string1 is "eggs" and string2 is "beans"

then string1+string2 will give you "eggsbeans".

If string1 is the string (NOT the number, but a string) "34" and string2 is the string "123"

then string1+string2 will give you the string "34123".

As it is, this

breakfast+lunch+dinner= totalcal;

is not at all doing what you probably meant.

Firstly, I expect you meant

totalcal = breakfast+lunch+dinner;

However, you need to read in breakfast and lunch and dinner not as strings, but as numbers. Making them int rather than string would do.



I get it now. I read the tutorial and go confused as to how strings could be used. That makes the tutorial makes sense thanks. Man it feels good to have this forum to go to. The first time I tried to learn c++ I just got a book and went for it. I did okay but got too frustrated and gave up. So Far I have gotten much farther in understanding this language thanks to the people on here.
Topic archived. No new replies allowed.