2

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"?

enter image description here

# 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";
}
Ajean
  • 5,528
  • 14
  • 46
  • 69
White Boat
  • 23
  • 4

1 Answers1

4

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.

logan rakai
  • 2,519
  • 2
  • 15
  • 24
  • thanks Logan for your help - but could you please explain it further ? – White Boat Dec 10 '16 at 18:21
  • When you say `int n; cin >> n;`, and `cin` contains `3.14`, it will only read the integer `3` from `cin`, and leave `.14` in `cin`. When you get to `cin >> String1;` it doesn't ask the user to enter anything because `cin` already contains a 'string': `.14` can be considered a string. So it writes `.14` to `String1` and continues. – Jack Deeth Dec 10 '16 at 18:25
  • Changing `n` to a `float` would fix it. Erasing the contents of `cin` before asking for input fix it too - [here's how](http://stackoverflow.com/a/257182/2449857). Sorry, it's not a very elegant way – Jack Deeth Dec 10 '16 at 18:27
  • 1
    Stated another way, when `64.2` is entered, `cin` will read as much as it can to read in an `int` (n is declared `int` not `float` in your code). The `6` and the `4` are valid for an `int` but It will stop when it reaches the `.` because `.` isn't part of an integer. – logan rakai Dec 10 '16 at 18:28
  • I got it - THANKS LOGAN – White Boat Dec 10 '16 at 18:29
  • Great, happy C++ing – logan rakai Dec 10 '16 at 18:29
  • Quite Happy - I spent 1 hour trying to figure what is happening - cheers – White Boat Dec 10 '16 at 18:30
  • 1
    Appreciated very much your help all - certainly Stack Overflow will be my Home Land in the upcoming next months – White Boat Dec 10 '16 at 18:34
  • is there a way to show my appreciation - like : like on facebook ? – White Boat Dec 10 '16 at 18:34
  • 2
    If you are satisfied with the answer, you can mark it as correct and it will put a green check mark by it. This gives the answerer some reputation points and lets others know you have had your question adequately responded to. – logan rakai Dec 10 '16 at 18:36
  • May I ask also - how to validate the input for a specific type (using simple c++ statements ?) – White Boat Dec 10 '16 at 18:37
  • 1
    Stackoverflow has a wealth of existing answers and you can search through them to find that, e.g. http://stackoverflow.com/a/16934374/5894196. If after searching you don't find the answer you need, you can ask a new question but try to avoid asking duplicate questions. – logan rakai Dec 10 '16 at 18:40
  • Thanks Jack Deeth - sorry I missed your contribution yesterday - cheers – White Boat Dec 11 '16 at 12:42