Can't show correct number.

Hi guys,

I just started programming and I am already stuck on my first assignment. :( The professor told us to code the following and finds any errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std ;
int main ()
{
	int BREAD = 63;    //calories in a slice of bread
	int CHEESE = 106;    //calories in a slice of cheese
	int MAYONNAISE = 49;    //calories in 1 gram of mayonnaise
	int PICKLE = 25;    //calories in a slice of pickle

	int TotalCalories = BREAD + CHEESE + MAYONNAISE + PICKLE;
	cout << "There were" << TotalCalories <<
	cout << "calories in my lunch yesterday." << endl;
	cout << "What is for lunch today?" << endl;
	return 0;
}


There are 2 slices of bread, 3 slices of cheese, 4 pieces of pickles, and 2 grams of mayonnaise. I think I will have to put something like

BREAD = 63 * 2;

And then add them together, correct? But when I tested the initial code, I get this weird number 2436......C3E8. I tried to make it show the right answer, but I haven't been able to figure it out. Also, were there any syntax or coding errors?
1
2
cout << "There were" << TotalCalories <<
         /* cout << */ "calories in my lunch yesterday." << endl;
Wow! Thanks a lot!
Can you explain how that works, please? :)
std::cout << std::cout does print something
(an address, which would look something like what you got: 0x2436C3E8).

See the difference between the two lines printed out in this program:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
    int i = 123456789 ;

    // print an address, an integer, an address, a new line
    std::cout << std::cout << i << std::cout << '\n' ;

    // print an integer, and then then a new line
    std::cout << i << '\n' ;
}
Last edited on
So... You need to enter std::cout once to display the correct number?

Is that how I should explain to my professor? :O
To print a, we can write std::cout << a ;

To print a, followed by a space, then b we can write

either
std::cout << a << ' ' << b ;

or
1
2
3
std::cout << a ;
std::cout << ' ' ;
std::cout << b ;


But we do not write: std::cout << a << std::cout << ' ' << std::cout << b ;


> Is that how I should explain to my professor?

What did your professor ask you to explain?
I have to explain what was the error in the code. Today, I asked the TA and he said the issue was

cout << "There were" << TotalCalories <<


The << after TotalCalories must be replaced with a semi-colon. And that was all I needed to change. But it's good to know another way, too. :)

Thanks for your help. :D
Topic archived. No new replies allowed.