I am having trouble getting MS Visual C++ to recognize my input for the 2nd getline I have listed. It already recognizes getline(cin, firstName) and allows for input, but won't recognize the second instance of getline. When the program gets to that line of output, it bunches the next two cout statements together like this:

Tell us the name of the item you wish to buy? How many of these would you like to purchase today?

The code should give the user a chance to enter the name of the item and allow for spaces, but it shows the output without allowing for the input and posts the next input statement instead. Here is the code below.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()

    //declare variables
    string       firstName            = "";
    string       itemName             = "";
    int          itemQuantity         = 0;
    const double STORE_DISCOUNT       = 0.35;
    double       originalPrice        = 0.0;
    double       totalAfterQuantity   = 0.0;
    double       totalBeforeQuantity  = 0.0;
    int          yearsLived           = 0;

    //welcome the user and explain the purpose of the software
    cout << "Welcome to Mike's Hardware! ";
    cout << "We would like to get to know a little bit about you. ";

    //enter input items
    cout << "Please tell us your first name. " << endl;
    getline(cin, firstName);
    cout << "So, " << firstName << " how many years have you lived 
    in this community? " << endl;
    cin >> yearsLived;
    cout << "Thanks, " << firstName << "." " Now, please tell us the 
    name of the item you are looking to buy. " << endl;
    getline(cin, itemName);
    cout << firstName<< ", how many of these would you like 
    to purchase today? " << endl;
    cin >> itemQuantity;
    cout << firstName << ", what is the original advertised 
    price for this item? " << endl;
    cin >> originalPrice;

    //calculate total before quantity and total after quantity
    totalBeforeQuantity = originalPrice - (originalPrice * STORE_DISCOUNT);
    totalAfterQuantity = totalBeforeQuantity * itemQuantity;

    //display the first name, item name, original price, 
    total before quantity, item quantity, total after quantity, 
    years lived
    cout << "Well," << firstName << " you asked for the new price 
    for a(n) " << itemName << ". With the 35% discount 
    today, our " << itemName << " has been marked down from the 
    advertised price of $" << originalPrice << " down to a 
    new price of $" << totalBeforeQuantity << ". So, since you 
    want to buy " << itemQuantity << " of these, your total 
    cost for them comes to $" << totalAfterQuantity << ". Since 
    you have lived around here for " << yearsLived << " 
    years, I am surprised we have not met you before. But, we 
    are awfully glad to count you as a valued customer.  
    Thanks for stopping by and we hope to see you again soon." << endl;

    return 0;
}   //end of main function

You have a cin >> yearsLived between the two getline() calls, which means the second call succeeds immediately on the newline left in the stream by the intervening formatted input. It's basically the problem described here.

This article has been dead for over six months. Start a new discussion instead.