Solving "Strange" Syntax Errors with Cookie Fun

  • Context: Comp Sci 
  • Thread starter Thread starter Lancelot59
  • Start date Start date
  • Tags Tags
    Errors Fun Strange
Click For Summary

Discussion Overview

The discussion revolves around resolving a syntax error encountered in a C++ programming assignment involving the use of streams and variables. Participants explore the correct usage of output statements and clarify concepts related to streams and objects in C++.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant presents a code snippet that generates a syntax error, specifically pointing out an issue with the output statement involving "cout" and "endl."
  • Another participant identifies that the syntax error arises from an incomplete statement in the code, suggesting the removal of a semicolon.
  • A participant seeks further clarification on the concepts of streams and objects in C++, asking if their understanding of "cout" as a stream and "cookieresponse" as an object is correct.
  • Further explanations clarify that streams can be sources or sinks of data, and provide examples such as the keyboard and console.
  • One participant notes that an object is a data structure combined with methods for modification, indicating that a precise definition is not necessary at the current learning stage.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the syntax error and the correct usage of output statements in C++. There is a shared understanding of the concepts of streams and objects, though the depth of understanding varies among participants.

Contextual Notes

Some participants express uncertainty about the definitions and roles of streams and objects, indicating a learning phase in their programming journey.

Who May Find This Useful

Individuals learning C++ programming, particularly those encountering syntax errors and seeking clarification on output statements and basic programming concepts.

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 count 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 1 ·
Replies
1
Views
11K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K