C++ won't recognize getline(cin, itemName)

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. ";
system("pause");
//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;

system("pause");
return 0;
} //end of main function
1
2
3
//display the first name, item name, original price, 
	total before quantity, item quantity, total after quantity, 
	years lived

This would defiantly cause a problem. You'll have to either use multiple C++ comments // or a single C comment /* */ if you want to have a comment that spans multiple lines.

The code below is also incorrect.
1
2
cout << "So, " << firstName << " how many years have you lived 
	in this community? " << endl;

Half of the string literal is on one line, and the rest is on the next line. You can't do that unless you end that string literal and use the insertion operator again. Your code should compile just fine after fixing all of those mistakes.

1
2
cout << "So, " << firstName << " how many years have you lived" 
<< "in this community? " << endl;

Here's your fixed code. http://codepad.org/8Kfl8QDq
Last edited on
All of my code was compiling. The problem wasn't that it wasn't compiling, the problem is that it was skipping one of the inputs. I used your corrected code and it's still doing the same thing. The following lines of code get fused together as one input. This is the problem: getline(cin,itemName) is being bunched together with the next input.

1
2
3
4
5
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;
Last edited on
1
2
3
4
5
6
cout << "Thanks, " << firstName << "." " Now, please tell us the 
name of the item you are looking to buy. " << endl;
std::cin.ignore();
getline(cin, itemName);
cout << firstName<< ", how many of these would you like 
to purchase today? " << endl;

Just add std::cin.ignore() to 'ignore' the newline character left in the input buffer from the previous stream.
Last edited on
Thanks!
Topic archived. No new replies allowed.