Thread: I cant do it but its in the tutorial

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    enumclaw wa
    Posts
    5

    I cant do it but its in the tutorial

    I keep trying lesson 2:If statements in C++ programming in my complier but it doesnt come out like it says it should. Here it is:
    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            
    {
      int age;                            
      
      cout<<"Please input your age: ";    // I input my age which is 13
      cin>> age;                         
      cin.ignore();                       
      if ( age < 100 ) {                  
         cout<<"You are pretty young!\n"; 
      }
      else if ( age == 100 ) {              
         cout<<"You are old\n";           
      }
      else {
        cout<<"You are really old\n";      
      }
      cin.get();
    }
    And all that comes out of it is 13 and not You are pretty young like the tutorial says it is suppose to what do I do could you help me plz!!!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    13 doesn't come out of it. You put 13 in... and then you press enter. Do that and tell me what happens. Does it close?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    well it worked fine for me

    I just added a "for" loop for testing three diffrent ages
    13, 100 and 120.

    Code:
    int main(void)
    {
      int age;                            
      int i;
    
    
    for ( i = 0 ; i < 3 ; ++i )  
      {
      cout<<"Please input your age: ";    // I input my age which is 13
      cin>> age;                         
      cin.ignore();                       
      if ( age < 100 ) {                  
         cout<<"You are pretty young!\n"; 
      }
      else if ( age == 100 ) {              
         cout<<"You are old\n";           
      }
      else {
        cout<<"You are really old\n";      
      }
      }
    
      cin.get();
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    the output I got :
    Please input your age: 13
    You are pretty young!
    Please input your age: 100
    You are old
    Please input your age: 120
    You are really old

    Press any key to continue . . .

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    And all that comes out of it is 13 and not You are pretty young
    the output I got :
    Please input your age: 13
    You are pretty young!
    What??!

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    enumclaw wa
    Posts
    5
    yeah when I compile and run the original slymaelstrom it comes out 13 thats it nothing else and I tried yours dude543 it keeps saying something is wrong with the line where you input your age This is how I compiled it for dude543's code

    Code:
    int main(void)
    {
      int age;                            
      int i;
    
    
    for ( i = 0 ; i < 3 ; ++i )  
      {
      cout<<"13";    
      cin>> age;                         
      cin.ignore();                       
      if ( age < 100 ) {                  
         cout<<"You are pretty young!\n"; 
      }
      else if ( age == 100 ) {              
         cout<<"You are old\n";           
      }
      else {
        cout<<"You are really old\n";      
      }
      }
    
      cin.get();
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    Also I am using dev-C++ if that means anything give me a reply

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    So what happens when you hit enter?

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    What does it says ?

    Quote Originally Posted by milesmiles
    I tried yours dude543 it keeps saying something is wrong with the line where you input your age This is how I compiled it for dude543's code
    What does it says exactly when you compile my code ?
    Did you both add "using namespace std;"

    Post all of the code.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Location
    enumclaw wa
    Posts
    5
    Since I am using Dev-C++ it just has a "x" before the line and highlights it and wont run it. I didnt use "using namespace std" when I compiled your code and where do I put "using namespace std" heres what I put exactly into my complier.

    Code:
    int main(void)
    {
      int age;                            
      int i;
    
    
    for ( i = 0 ; i < 3 ; ++i )  
      {
      cout<<"13";    
      cin>> age;                         
      cin.ignore();                       
      if ( age < 100 ) {                  
         cout<<"You are pretty young!\n"; 
      }
      else if ( age == 100 ) {              
         cout<<"You are old\n";           
      }
      else {
        cout<<"You are really old\n";      
      }
      }
    
      cin.get();
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    well there it is reply

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    cout<<"13";
    I have a slight suspicion that this line is causing "13" to be outputted to the screen.

    If you're not having to use namespaces, I suspect you're using an outdated version of Dev-C++ that uses the old C++ standard. No biggie - but you need to be aware of that. I'd recommend updating and learning about the differences. If that's exactly what you entered into your compiler, where are the #includes?

    Also - the use of system("PAUSE"); is often discouraged. You can learn more about this in the FAQ in the entries about waiting for a keypress. On that note, why on earth are we using both cin.get() and system("PAUSE")?

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Well here is my full code

    The take for every warning your compiler is giving you.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
    int age;                            
    int i;
    
    
    for ( i = 0 ; i < 3 ; ++i )  
      {
      cout<<"Please input your age: ";    // I input my age which is 13
      cin>> age;                         
      cin.ignore();                       
      if ( age < 100 ) {                  
         cout<<"You are pretty young!\n"; 
      }
      else if ( age == 100 ) {              
         cout<<"You are old\n";           
      }
      else {
        cout<<"You are really old\n";      
      }
      }
    cin.get();
    return EXIT_SUCCESS;
    }
    PS my compiler is bloodshed 4.9.9.2
    Search for bloodshed in this site and you find where to download it. ( In my opinion it is very poor. the debgur wont work ... etc)

  11. #11
    Registered User
    Join Date
    Jan 2006
    Location
    enumclaw wa
    Posts
    5
    Yeah sean it isnt different than what I posted because if you read the top from all the way down to the bottom I origanally had a different code than dude543 showed me his code, that did the same thing and I said it didnt work and he wanted me to post exactly what I put into my complier so I did.
    There was nothing wrong with the origanal code, but it only comes up as 13 and not "you are pretty young" like it is suppose to. For all that just joined us here is the code:

    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            
    {
      int age;                            
      
      cout<<"Please input your age: ";    // I input my age which is 13
      cin>> age;                         
      cin.ignore();                       
      if ( age < 100 ) {                  
         cout<<"You are pretty young!\n"; 
      }
      else if ( age == 100 ) {              
         cout<<"You are old\n";           
      }
      else {
        cout<<"You are really old\n";      
      }
      cin.get();
    }
    What do I do? and I am using bloodshed 4.9.92 just to clarify.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You type in 13 and then you hit enter?

    The only thing I would guess is to add cout <<endl; after the "You are pretty young!\n" part.

  13. #13
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49
    Hello, newbie here, hope I can help.

    Have you tried the last code you've posted with this before the end brace of main?
    Code:
    return 0;
    I think this is what's wrong because you code doesn't provide a return value for main.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Though that shouldn't matter, it wouldn't hurt to try at this point. However, I feel this error is on the side of the end-user (or perhaps his machine), not the program.
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    I'm using Dev-C++, maybe I can help yea. The 1st code you post will work, just dont add anything els, just your age, like 13. It should compile and if not and gives a error, at the botten it tells what the error is and you can click on that and post whats wrong.
    The only thing I would guess is to add cout <<endl; after the "You are pretty young!\n" part
    Dont really need the <<endl; thing. And to make sure what your using, are you using 4.9.9.2?I hear they are updateing it or something so that could be a bug if your using 4.9.92
    Last edited by adr; 01-31-2006 at 05:09 AM.

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. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 02:49 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM