Wrox Programmer Forums
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 25th, 2003, 07:05 PM
Registered User
 
Join Date: Aug 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default New to C++

I'm new to C++. Why doesn't this code run correctly? I will accept the input of the number, but it blows right past the cin.getline() function???? What am I missing? I appreciate the help. Thanks.

#include<iostream>

using namespace std;

int main()
{
    int num = 0;
    const int MAX = 15;
    char name[MAX];

    for(;;)
    {
        cout << "\nEnter a number: ";
         cin >> num;
         if(num == 0)
              break;

        cout << "\nEnter a string: ";
         cin.getline(name, MAX, '\n');
         cout << "\nThe number is " << num;
         cout << "\nThe string is " << name;
     }

     cout << "\nZero entered!\n\n";

    return 0;
}
 
Old August 26th, 2003, 04:57 AM
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

That is a typical problem that we all experience. It should be covered in Ivor Horton's Beginning C++, or other beginning C++ texts, but unfortunately it's not. It has to do with the way cin>> works. When these statements are executed:

cout << "\nEnter a number: ";
cin >> num;

The program stops and waits for you to enter the data. Then, you enter the data and hit return. If you enter 10 and hit return, this is what your program sees:

10\n

When you hit return a '\n' character is entered and that becomes part of your input. However, cin>> is defined to stop reading when it encounters a '\n' character. So, your program reads 10 into your variable num, and then things look like this:
Code:
10\n
  ^
Your program actually keeps track of where it stopped reading your input, and as far as it's concerned there's still input left to be read at some future date, namely the '\n' character. Then, execution continues to these lines:

cout << "\nEnter a string: ";
cin.getline(name, MAX, '\n');

cin.getline() is defined to read in input until it encounters a '\n' or MAX-1 characters, but it's also defined to read in the '\n'--unlike with cin>> which stops and doesn't read in the '\n'. Well, your program doesn't wait for you to input anything because it still has stuff to read--there is still a '\n' left over from when you used cin>>. So, cin.getline() happily reads that '\n', which is also its signal to stop reading, and as far as your program is concerned cin.getline(name, MAX, '\n'); has done its job and it's time to move on. Therefore, your program continues execution making it seem like it skipped right by that line.

The solution? There is a function called ignore() that you can use:

cin.ingnore();

That ingnores the number of characters you indicate between the parentheses with the default being 1. So, the above line will ignore one character. If you include that after your cin>> line, then your program will see this:
Code:
10\n
    ^
and then when it comes to this line:

cin.getline(name, MAX, '\n');

There won't be any data for it to read, so it will wait for you to enter data.

The conclusion of that long explanation is that whenever you use cin>> and then use cin.getline() you need to use cin.ignore() after the cin>> line.

---------------

Note: That may lead you to wonder why these lines work(you can verify yourself that you can use cin>> twice in a row and the data will be read in correctly):

cout<<"Enter a number: ";
cin>>num1;

cout<<"Enter a second number: "
cin>>num2;

If you enter a 10 and hit return after the first cout<< statement, your program will see this:
Code:
10\n
and cin>> will read in the 10, leaving this:
Code:
10\n
  ^
Then the second cout<< and cin>> will be executed, but since cin>> is required to stop at '\n' how does it get by the '\n' and correctly read in the second number you enter:

Code:
10\n
  ^
The answer is that cin>> is defined to skip a leading '\n', but stop at a trailing '\n'. The '\n' left over from the first cin>> is now a leading character(i.e nothing has been read in prior to it by the current cin>> statement) so it is skipped, and the program waits for you to enter the next number.









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.