Simple Question relating to the IOStream Lib and Cin

This is a discussion on Simple Question relating to the IOStream Lib and Cin within the C++ Programming forums, part of the General Programming Boards category; When I'm testing a piece of code (by your tutorials) I get a problem, it pertains to "cin>>" cin is ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    Simple Question relating to the IOStream Lib and Cin

    When I'm testing a piece of code (by your tutorials) I get a problem, it pertains to "cin>>" cin is an input stream, I've included the library in the header, and also included the "using" arguement (because I'm not using everything in the header file of the iostram.) However cin isn't giving me the input area, thus recieving the number isn't working either.

    Heres the code:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    
    #include <conio.h>
    int main()
    {
      int input;	  
      cout<<"1. Play game\n";	
      cout<<"2. Load game\n";	
      cout<<"3. Play multiplayer\n";	
      cout<<"4. Exit\n";	
      cin>>input;	 
      switch (input)	
      {	   
       case 1: 
       cout<<"Playing game...";
    	   break;	 
       case 2:
    	cout<<"Loading game...";	 
            break;	
       case 3:	      //Note use of : not ;	 
            cout<<"Going into Multiplayer mode...";
    	  break;	
       case 4:
            return 0;   
      default: 	  
            cout<<"Error, bad input, quitting";	
      }
      return 0;
    
    }

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I've just compiled and run it and it seems to be working fine. What do you mean, "not giving me the input area"?

    Why have you included conio.h?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    like when I compile it runs but it never gives the area to insert the numerical number, and even if I enter something in the blank space it just exits, it doesn't evaluate the cases (some tutorial told me to use cornio.h for cases)

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    18

    Re: Simple Question relating to the IOStream Lib and Cin

    before each break in the cases, add a
    Code:
    system("PAUSE");
    Quantum Theory Entertainment

    Team #0005 US FIRST Robotics Team Website Designer
    Contact me on AIM: evanescence s0ul

    Compiler/IDE: Microsoft Visual Studio (C++) 6.0 / Microsoft .NET 2003

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    >>>(some tutorial told me to use cornio.h for cases)
    ??? switch is part of the C++ language.... do you have the link to the tutorial?

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    Re: Re: Simple Question relating to the IOStream Lib and Cin

    Originally posted by Eric Hansen
    before each break in the cases, add a
    Code:
    system("PAUSE");
    Thats not the problem, heres an example

    http://phpnetzero.sourceforge.net/1.gif

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I am still not really sure what you mean by...

    >>> never gives the area to insert the numerical number

    ... look at your code. You tell the machine to output your 4 options, then tell it to read a value from the keyboard. Where are you telling it to "give you an area"? If you mean you want an edit box or similar - then you need to be writing a GUI Windows program, (or whatever OS you use), which is a significantly different animal to what you have here.

    When I run your code, I get the 4 options up, then when I enter a digit and press return, it gives me the output from the case, (you do not need conio.h). As I am running it in my IDE, it waits for me to press return. If you are running the .exe, it will not wait, it will exit as you have not told it to do anything else.

    Salem pointed you to the FAQ entry which deals with stopping your program exiting before you have had a chance to read it.

    Here is what happens when I enter "1" - NOTE the "Press any key..." is generated by my IDE, you will not see that if you are running the .exe.
    Attached Images Attached Images  
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

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