Can't find my mistake?

closed account (z09TURfi)
Don't really know what I am doing wrong with this code, although there aren't no errors and it debugs without a problem, i'm trying to figure out why is my three questions which I declared for the user input skipping to the last third question without the second question having a user input. For example, I wrote
cout<<"Enter First name:> ";
getline (cin, Fname);

cout<<"Enter Last name:> ";
getline (cin, Lname);

and the insertion goes straight to "Enter Last name:> " without the user even putting a name for the Fname. Here is my code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <climits>

using namespace std;

#define Pause(); system("Pause")
#define Clear(); system("Cls")

string Line (75,'_'), Fname, Lname; //F = First, & L = Last

unsigned int Age, Month, Day, Year;

int main(void)
{
printf("Before we begin, you must be 15 years or over to proceed"); std::cout<<endl
<<endl
<<endl;
printf("Enter Age:> "); cin>>Age;

if (Age < 15)
{
cout<<"You are underage, Goodbye";
Pause();
Clear();
} cout<<endl<<endl;

Pause();
Clear();

cout<<endl<<"You are "<<Age<<" years old and may proceed"<<endl
<<endl<<Line<<endl<<endl; Pause(); Clear();

cout<<"Enter first name only:> ";
getline (cin, Fname); cout<<endl
<<endl;

cout<<"Enter last name only:> ";
getline (cin, Lname); cout<<endl
<<endl;

Pause();
return EXIT_SUCCESS;
}
closed account (zb0S216C)
When you used system( ), you left newline escape sequences in your input buffer, causing the first input request (input of the first name) to skip. When std::cin saw the newline escape sequence, it used that as your input instead of a string.

Here's your code, with proper formatting :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

int Wait( )
{
    std::cin.sync( );
    std::cin.ignore( std::numeric_limits < std::streamsize >::max( ), '\n' );
    return 0;
}

int main(void)
{
    std::string Line( 75, '_' ), Fname, Lname;
    unsigned int Age( 0 ), Month( 0 ), Day( 0 ), Year( 0 );

    std::cout << "Before we begin, you must be 15 years or over to proceed" << std::endl;

    std::cout << "Enter age:> ";

    std::cin >> Age;

    if( Age < 15 )
    {
        std::cout << "You are underage, Goodbye" << std::endl;
        return Wait( );
    }

    Wait( );

    std::cout << std::endl << "You are " 
              << Age << " years old and may proceed " << std::endl;

    Wait( );

    std::cout << "Enter first name only:> "; 
    getline( std::cin, Fname );

    std::cout << "Enter last name only:> "; 

    getline( std::cin, Lname ); 
    std::cout << std::endl;

    Wait( );
    return 0;
}

A few explanations:
- std:: indicates that the leading statement resides within the standard namespace.
- The funny looking code within Wait( ) basically ignores all of the characters within the input buffer.
- Always initialize variables, even if you use them directly after their declaration.
- Avoid system( ) as much as possible. However, using it for development is fine, but exclude it during the final build. Why remove system( )? Because it pulls in the operating system and it's platforms-dependant.

Wazzak
While the cin.ignore() method does work, save yourself some space. This method works just as well:

1
2
3
4
void wait() {
    std::cin.sync();
    std::cin.get();
}
Topic archived. No new replies allowed.