Thread: not reading in input

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    not reading in input

    Code:
    #include <iostream>
    #include <limits>
    
    using std::endl;
    using std::cout;
    using std::cin;
    
    void Input(int& number, char name[])
    {
    	cout << "Enter a number: ";
    	cin >> number;
    
    
    	if (number == 0)
    		return;
    
    	cout << "Enter a name: ";
    	cin.getline(name, 15);
    
    	cout << endl << name;
    }
    
    void PutData(int number, char name[])
    {
       cout << endl << "Thank you. Your number and name were " 
            << number << " and \"" << name << "\"" << endl;
    }
    
    	
    
    
    int main()
    {
    	char name[15];
    	int number;
    
    	while (true)
    	{
    		Input(number, name);
    
    		cin.ignore(10000, '\n');
    		cin.clear();
    
    		if (number == 0)
    			break;
    		PutData(number, name);
    	}
    
    
    
    	return EXIT_SUCCESS;
    }
    I'm not able to input the name variable why ?

    When I print it it shows nothing

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You're passing an int, but receiving a reference to an int.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Dino is not answering the right question: You are mixing cin >> and cin.GetLine - just like mixing scanf() and fgets() - the first form leaves any terminating newline (or other whitespace) in the input buffer, whilst the second form "eats" newlines. So when you enter a number, the input buffer would contain something like "42\n". cin >> number would consume the "42" part of the input, and cin.getline() will find a newline immediately, and thus skip the input.

    If you want to enter a name, you could enter "42Mats" as the answer to Number, and the name variable would be "Mats".

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:43 AM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM