Thread: C++ tutorial 2 (if statements) problem

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

    C++ tutorial 2 (if statements) problem

    I'm new to C++ and programing and an error message came up on my program and I don't understand it: here is the program
    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            // Most important part of the program!
    {
      int age;                            // Need a variable...
      
      cout<<"Please input your age: ";    // Asks for age
      cin>> age;                          // The input is put in age
      cin.ignore();                       // Throw away enter
      if ( age < 100 ) {                  // If the age is less than 100
         cout<<"You are pretty young!\n"; // Just to show you it works...
      }
      else if ( age == 100 ) {            // I use else just to show an example 
         cout<<"You are old\n";           // Just to show you it works...
      }
      else {
        cout<<"You are really old\n";     // Executed if no other statement is
      }
      cin.get();
    }
    this is the error message: 13 C:\Dev-Cpp\If and else.cpp expected `;' before '}' token

    It also highlights } between " cout<<"You are pretty young!\n"; // Just to show you it works..." and " else if ( age == 100 ) { // I use else just to show an example "

    can someone please help me out, I would be grateful.
    Last edited by Jester P; 07-23-2008 at 09:50 AM.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I get no errors.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's really strange, as the code compiles just fine on my machine, copied from your post.

    However, I would recommend avoiding spaces in filenames - if you want to separate things, use _ [or POSSIBLY . - but that can lead to interesting problems too]. So the name would be: If_and_else.cpp

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Quote Originally Posted by indigo0086 View Post
    I get no errors.
    thanks matsp
    I just tried it again after closing the program and reopening it and it worked. Sorry to waste your time. You can lock this now if you want

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You probably didn't save your changes or something when compiling.

    Anyway, get rid of the excessive comments - they are really quite superfluous and make it harder to read the code.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    They're still superfluous. Putting them in a tut teaches bad habits, IMO.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The code doesn't "need" any comments at all, but the noobs have to start somewhere.
    The appropriate level of commenting depends on the audience.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  2. [Help] Tutorial : If Statements
    By hackerkts in forum C++ Programming
    Replies: 8
    Last Post: 02-12-2006, 04:27 PM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. MSVC Tutorial problem - Monochrome Palette
    By colinH in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2002, 03:57 AM
  5. Problem running a Cboard C++ tutorial!
    By Monkey Liar in forum C++ Programming
    Replies: 6
    Last Post: 02-15-2002, 03:32 AM