简体   繁体   中英

Beginner C++ input behviour

This program is a very simple code in my practice to learn C++. The problem is at some point it does not accept input from cin and behaves strangely. The code and the output of the program are below.

Why does the program not consider cin at "Enter your first name please"?

在此处输入图片说明

# include "cmath"
# include <iostream>
using namespace std;

int main()
{
    string FirstName, MiddleName, LastName;
    string WelcomeMessage = "Welcome to Visual C++";
    int Number_of_Steps = 5;
    int LoopStart = 1, LoopEnd = 5;
    int AgeYears, AgeMonths;
    double Pi = 3.14;
    float k = 5.366;
    double Age;
    char* Symbol = "k"; 
    bool TestResult = true;

    MiddleName = "Milton";

    cout << "Input Your First Name and Last Name" << endl;
    cin >> FirstName >> LastName;
    cout << "Input your Age in Years" << endl; 
    cin >> AgeYears;
    cout << "Imput your Age in Months " << endl;
    cin >> AgeMonths;
    Age = AgeYears + AgeMonths / 12;
    cout << endl << "Your Name is " << FirstName << ' ' << LastName << endl;
    cout << "Your Age is " << Age << endl;
    cout << "The Character is " << Symbol << endl;

    // Testing operators
    cout << "Please Enter a floating point number \n";
    int n;
    cin >> n;
    cout << "n==" << n
        << "\n n+1==" << n + 1
        << "\n n three times==" << 3 * n
        << "\n n twice ==" << n + n
        << "\n nsquared ==" << n*n
        << "\n half of n ==" << n / 2
        << "\n square root of n ==" << sqrt(n)
        << "\n";    

    // Testing string addition
    cout << "Eneter your first name please" << endl;
    string String1, String2, String3;
    cin >> String1;
    cout << "Enter your family name please" << endl;
    cin >> String2;
    String3 = String1 + " " + String2;
    cout << "Welcome" << " " << String3 << endl;

    // testing inequalities to strings
    string FirstString, SecondString;
    cout << "Input First String "
        << endl;
    cin >> FirstString;
    cout << "Input Second String "
        << endl;
    cin >> SecondString;
    if (FirstString == SecondString)
        cout << "The two words are identical \n";
    if (FirstString >= SecondString)
        cout << "First word is bigger than second word \n";
    if (FirstString <= SecondString)
        cout << "Second word is bigger than first word \n";
}

You get a hint from the output displaying .2 as the first name ( String1 ). The cin operation before asking for first name had 64.2 on the buffer but because you read the value into an int n it only read the integer portion 64 and left the .2 on the buffer. Changing the declaration n to float n or doing some input validation if you did want an integer should leave the buffer empty when you get to the request for first name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM