another noobie question

This is a discussion on another noobie question within the C++ Programming forums, part of the General Programming Boards category; cin.get(); The FAQ said to use this to keep my window from closing, however, i put it in the below ...

  1. #1
    noobie
    Guest

    another noobie question

    cin.get();

    The FAQ said to use this to keep my window from closing, however, i put it in the below code and it still closes....

    Code:
    #include <iostream.h>			
    
    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
    
      if(age<100)				//If the age is less than 100
    
      {
    
         cout<<"You are pretty young!";     //Just to show it works
    
      }
    
      else if(age==100)		//I use else just to show an example 
    
      {
    
         cout<<"You are old";		//Just to show you it works...
    
      }
    
      else
    
      {
    
        cout<<"You are really old";	//Executed if no other statement is executed
    
      }
    
      cin.get();
    
      return 0;
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lookup cin.ignore(); which you can use to flush unwanted characters from your input buffer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Noobie
    Guest
    how do i look something up? Why would I want to flush characters?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When you used cin>> to load data into the int variable, it left the newline character in the input buffer. When cin.get() ran, it simply read that newline character in, and gave you the impression that the program wasn't waiting for the user. That is why you need to flush the input buffer when you use this method.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    87
    just before

    Code:
    return 0;
    put in:

    Code:
    system("PAUSE");
    It'll ask you to press a key before closing...
    **********************
    *==================*
    * Many Can. One Must... *
    *==================*
    **********************

  6. #6
    Noobie
    Guest
    I tried

    system("PAUSE");


    but that resulted in a call to undefined system error

  7. #7
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    Have you used getchar();

    ?
    Such is life.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    87
    You've gotta include system.h, stdlib.h and iostream.h also if you've got it conio.h (the last one definately has it, not sure bout the others though)
    **********************
    *==================*
    * Many Can. One Must... *
    *==================*
    **********************

  9. #9
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    ooops forgot to say-getchar(); will hold the screen for you.Put before your return 0.
    Such is life.

  10. #10
    Noobie
    Guest
    ok now system.h cannot be opened and getchar is undefined

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Noobie
    ok now system.h cannot be opened and getchar is undefined
    Just try <iostream.h> and <conio.h>

  12. #12
    Noobie
    Guest
    both system and getchar are undefined functions

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Repost your code plz

  14. #14
    Noobie
    Guest
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
    
      char choice;
      char room;
      char x=0;
    
      cout<<"Please choose a room (1 or 2): ";
    
      while(x==0)
    
     {
    
        cin>>choice;	  
    
    if(choice==1)
    x=x++;
    else if(choice==2)
    x=x++;
    else
    x=x;        		
      } 				
    
      if(choice==1)				
    
      {
    
      		room=1;
         cout<<"You enter a room\n";    
    
      }
    
      else if(choice==2)		
    
      {
      		room=2;
    
         cout<<"You are in room 2\n";		
    
      }
    
      else
    
      {
    
        cout<<"You should not see this.\n";
    
      }
    
      if (room==1)
      {
      cout<<"Yep";
      }
      else if (room==2)
      {
      cout<<"Nope";
      }
      else
      {
      cout <<"I need to know how to ask them to plz enter a proper room choice, but how to do that? i don't know.";
      }
      system("PAUSE");
      getchar();
      return 0;
    
    }

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Try this

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
    
      char choice;
      int room;
      int x=0;
    
      cout<<"Please choose a room (1 or 2): ";
    
      while(x==0)
      {
    
    		cin>>choice;	  
    
    		if(choice=='1')
    		x=x++;
    		else if(choice=='2')
    		x=x++;
    		else
    		x=x;        		
      } 				
    
      if(choice=='1')				
      {
    
      		room=1;
    		cout<<"You enter a room\n";    
    
      }
      else if(choice=='2')		
      {
      		room=2;
    		cout<<"You are in room 2\n";		
      }
      else
      {
    		cout<<"You should not see this.\n";
      }
      
      if (room==1)
      {
    		cout<<"Yep";
      }
      else if (room==2)
      {
    		cout<<"Nope";
      }
      else
      {
    		cout <<"I need to know how to ask them to plz enter a proper room choice, but how to do that? i don't know.";
      }
      system("PAUSE");
      getchar();
      return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. A fourth noobie question - namespace std?
    By Noobie in forum C++ Programming
    Replies: 24
    Last Post: 08-12-2005, 02:10 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. A third noobie question
    By Noobie in forum C++ Programming
    Replies: 5
    Last Post: 01-20-2003, 11:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21