Comp Sci Solving "Strange" Syntax Errors with Cookie Fun

AI Thread Summary
The discussion revolves around resolving a syntax error in C++ related to the use of the `cout` statement. The error arises from an incomplete line where `<< endl;` is incorrectly placed after a semicolon. The correct usage is to either chain the output in one line or ensure each line has a proper stream before the `<<` operator. Participants clarify that `cout` is a stream for output, while `cookieresponse` is a variable containing data. The conversation also touches on the nature of streams and objects in programming, emphasizing their roles in data handling.
Lancelot59
Messages
640
Reaction score
1

Homework Statement


We had to basically modify a few variables in the given code and then shoot it out with a valid output. I decided to have some fun with it.

However I get a strange syntax error, here is the code:

Code:
	  //Conditionals controlling the cookie response output
	  if(cookies == "Yes")
	  {
		  cookieresponse = "Hooray! Cookies are awesome! :D";
	  }
	  else
	  {
		  cookieresponse = "YOU MONSTER! HOW CAN YOU NOT LIKE COOKIES!? D:";
	  }

	  cout << cookieresponse;
	  << endl;
	  
	  return 0;
	}

VS says I'm missing a ";" before the "<<" of the endl command. What's happening here. According to my book I've done it correctly.
 
Physics news on Phys.org
This piece of code:

Code:
cout << cookieresponse;
	  << endl;

is incorrect. The first line is a complete statement, with a stream on the left and an object on the right. The second line is incomplete; it does not specify a stream.

Consider removing the semicolon at the end of the first line.

- Warren
 
Thanks, it worked. Could please you explain in a little more detail? I'm still new to C++. All were were supposed to do is copy some code from the handout and change a few variables. I added the cookies part in.

So from what I understand cout is a stream, and the cookieresponse variable is an object. Correct?

So if I want to end a line I have to have a stream or something coming before it?

Also do you happen to know any good online resources for C++ programming?
 
Last edited:
Yes, cout is a stream, and cookieresponse is a variable.

You can do either of the following:

Code:
cout << cookieresponse;
cout << endl;

or

Code:
cout << cookieresponse << endl;

You can also split up the statement onto multiple lines, but notice that there is still only one semicolon for the whole statement. The compiler ignores whitespace.

Code:
cout << cookieresponse
<< endl;

- Warren
 
I see now. Just to clarify: streams are paths that information can take to enter or leave the program, and objects are elements contained in the program?
 
A stream is an abstract thing which can source or a sink data. You can read data from streams, or send data to streams. The keyboard is a source of data, so it is a stream. The console is a sink of data, so it is also a stream. Files on disk can be read from or written to, so they are streams. Finally, a network connection between a client and server program is also a stream.

An object is essentially a data structure wrapped up together with the methods used to modify that data. At this point in your programming career, you don't need a well-defined definition of "object," though. That'll come later.

- Warren
 
Alright, thanks for your help! :)
 

Similar threads

Replies
5
Views
2K
Replies
1
Views
10K
Replies
6
Views
3K
Replies
13
Views
2K
Replies
4
Views
3K
Replies
4
Views
3K
Replies
4
Views
2K
Replies
3
Views
2K
Replies
6
Views
4K
Back
Top