c++ problem reading input

This is a discussion on c++ problem reading input within the C++ Programming forums, part of the General Programming Boards category; my code Code: #include<iostream> #include<cstring> using namespace std; int main() { int x; x=1; int choice; int usenr; usenr=001; char ...

  1. #1
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29

    c++ problem reading input

    my code
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    
    int main()
    {
    int x;
    x=1;
    int choice;
    int usenr;
    usenr=001;
    char usename[20];
    char comment[250];
    int age;
    char pwd[10];
    
    	while(x==1)
    	{
    		cout<<endl;
    		cout<<"This program demonstrates all functions i worked with up to now!\n";
    		cout<<"These include... \n IF...ELSE \n LOOPS \n FUNCTIONS \n SWITCH CASE \n POINTERS \n STRINGS \n";
    		cout<<"PLease select from the following...\n";
    		cout<<" 1.EDIT PROFILE 			\n 2.VIEW PROFILE \n";
    		cout<<" 3.EXIT			\n";
    		cin>>choice;
    		switch(choice)
    		{
    			case 1:
    			cout<<"To chnge your profile you need to enter your password.\n Please enter your password now \n";
    			cin.getline(pwd,10);
    			if(strcmp(pwd,"rhino")==0)
    			{
    				cout<<"Admin access granted!\n";
    				cout<<"Please enter your name: \n";
    				cin.getline(usename,20);
    				cout<<"Please enter your age: \n";
    				cin>>age;
    				cout<<"Tell us a little bout yourself(max 250 characters):\n";
    				cin.getline(comment,250);
    			}
    			else
    			{
    				cout<<"Sorry, wrong password!\n";
    			}
    			break;
    			case 2:
    			cout<<"Welcome "<<usename<<" This is your profile:\n";
    			cout<<"User Number: "<<usenr<<"\n";
    			cout<<"User name: "<<usename<<"\n";
    			cout<<"User age: "<<age<<"\n";
    			cout<<"Your comment is: "<<comment<<"\n";
    			cout<<"Please make sure this is correct and press enter\n";
    			cin.get();
    			break;
    			case 3:
    			cout<<"Are you sure?\n";
    			break;
    			default:
    			cout<<"Sorry, incorrect input\n";
    			break;
    		}
    		cout<<"Thank you for your participation! \n To try again press 1. \n or else press any key to exit\n";
    		cin>>x;
    		
    	}
    	cin.get();
    }
    the following part
    Code:
    cin.getline(pwd,10)
    why is this? what am i missing?

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    I am sure I encountered this a while back but can't remember the solution I was given, anyway here is an answer I found by searching.
    http://p2p.wrox.com/topic.asp?TOPIC_ID=3385

  3. #3
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    thanks, a simple solution if adding
    Code:
    cin.get();
    and i also forgot
    Code:
    cin.ignore();
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not reading file input :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2009, 02:55 PM
  2. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 09:41 AM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 12:50 AM
  5. Problem with reading strings
    By goron350 in forum C++ Programming
    Replies: 8
    Last Post: 06-05-2005, 12:05 AM

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