1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Problem

  1. Sep 11, 2009 #1
    1. The problem statement, all variables and given/known data
    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 (Text):
          //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.
     
  2. jcsd
  3. Sep 11, 2009 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    This piece of code:

    Code (Text):
    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
     
  4. Sep 11, 2009 #3
    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: Sep 11, 2009
  5. Sep 11, 2009 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Yes, cout is a stream, and cookieresponse is a variable.

    You can do either of the following:

    Code (Text):

    cout << cookieresponse;
    cout << endl;
     
    or

    Code (Text):

    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 (Text):

    cout << cookieresponse
    << endl;
     
    - Warren
     
  6. Sep 11, 2009 #5
    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?
     
  7. Sep 11, 2009 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  8. Sep 12, 2009 #7
    Alright, thanks for your help! :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ Problem
  1. C++ problems (Replies: 12)

  2. C++ problem (Replies: 3)

  3. Problem infile.get(c) (Replies: 1)

Loading...