Thread: Very simple question

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    Question Very simple question

    Hello everyone, I'm new to this board and new to C++ but I have a question about a program I've run into in Sams Teach Yourself C++ in 21 Days. I've written it correctly and when I tried to compile it I got errors (I'm not supposed to get any with this piece) and since I'm new to this, I can't exactly figure out what the problem is. Here it is, tell me what you think:

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int x = 5;
    	int y = 7;
    	cout "\n";
    	cout << x + y << " " << x * y;
    	cout "\n";
    	
    	return 0;
    }
    Simplicity at it's best But I figured it's worth a try...these are the errors I get:
    Line 7: error C2143: syntax error : missing ';' before 'string'
    Line 9: error C2143: syntax error : missing ';' before 'string'

    Thanks

    PS - Sorry to those of you that see this as a waste of your time.

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    For single characters you use ' ' to enclose them, not " ", ie cout << '\n';.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    add "<<" after cout:
    Code:
    cout<<"\n";
    cout<<endl;
    cout<<"\\n creates a new line, while endl creates a new line and flushes the output buffer";
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    Code:
    #include <iostream.h>
    
    int main()
    {
    	int x = 5;
    	int y = 7;
    	cout "\n";
    	cout << x + y << " " << x * y;
    	cout "\n";
    	
    	return 0;
    }
    Your problem is you don't have the '<<' operator (I don't remember the name offhand) on line 7. It should be this:

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int x = 5;
    	int y = 7;
    	cout << "\n";
    	cout << x + y << " " << x * y;
    	cout "\n";
    	
    	return 0;
    }
    Brendan
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

  5. #5
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    Ah I was beaten to it.

    Brendan
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    Thanks

    Awesome, thanks all. Turns out I needed the << at the beginning...good to know

  7. #7
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Originally posted by harryP

    Your problem is you don't have the '<<' operator (I don't remember the name offhand) on line 7. It should be this:
    The << is called the "insertion operator" as it inserts the data on the right into the stream on the left. Very helpful to remember it that way too as it reminds you that cout is a stream object and you can do all the nifty things with it that you can do with streams. Hope this helps.

    -jEssYcAt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM