Thread: Error that isn't an error

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Error that isn't an error

    Hi, I'm new to programming and I'm trying out some basic codes. I'm using Gedit as a text editor and g++ to compile in my CLI. I am learning basic i/o right now so I wrote this little code to try it out.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	int age;
    	int zip;
    
    	age = 20;
    	zip = 50021;
    
    	cout << "I am " << age << " years old." endl;
    	cout << "My zipcode is " << zip << ".";
    
    	return 0;
    }
    I get this error message when I try to compile it:
    Code:
    [metatron@Babylon Desktop]$ g++ -o ioexp ioexp.cpp
    ioexp.cpp: In function ‘int main()’:
    ioexp.cpp:15: error: expected `;' before ‘endl’
    I gather that it's telling me in line 15 I need a ";" before the "endl" command but firstly, I shouldn't need that right? It comes AFTER the "endl." And second, when I did try that I got another error anyway, which I expected.
    Any ideas?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first:
    Error that isn't an error
    compilers are incredibly "intelligent" pieces of software--they do not lie.

    the line in question does have an error in it:
    Code:
    cout << "I am " << age << " years old." endl;
    you are trying to concatenate " years old." and 'endl'. in order to concatenate, you have to use "<<", as you did with the other strings.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Thanks, you know, when I came back to it and looked again I figured that too. The problem was that I was using a bit of code in a tutorial as a reference and it was not a complete line of code. Thanks for getting back! I look forward to utilizing this forum as I learn.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    As a side note, this isn't C. You don't have to declare all your variables at the top of your function.
    This:
    Code:
    int age;
    int zip;
    
    age = 20;
    zip = 50021;
    could have just as easily (and probably more clearly) been written as:
    Code:
    int age = 20;
    int zip = 50021;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Thanks, I was a little mixed up about that so I just wrote it all out to make myself feel better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM