This is pretty damn embarassing, I've been programming for a really long time and have been defeated by a trivial problem.

#include "stdafx.h"
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
	int choice = 0;
	string itemDesc;
	double itemPrice;

	cout << "1. Process a sale" << endl;
	cout << "2. View sales totals" << endl;
	cout << "3. Change the GST percentage" << endl;
	cout << "4. Quit" << endl;
	cin >> choice;

		cout << "Enter Item Description " << endl;
		getline(cin, itemDesc);

		cout << "Enter Item Price" << endl;
		cin >> itemPrice;

	return 0;

So as you may have guessed from the code, I'm creating a little store/cashier program. First a menu is displayed, the user chooses what he wants to do out of the four options. The option is read into the variable "choice".

So far so good, here comes the problem. After the user chooses an option, he should then prompted to "Enter Item Description", which should be read into the variable itemDesc.

That doesn't happen.

Instead after choosing from the menu you are prompted like this

Enter Item Description
Enter Item Price

With only the price being available for you to input. I've checked the variable itemDesc and it is completely blank. I've spent the last two hours on this and finally decided to get some help.

Thanks in advanced,

7 Years
Discussion Span
Last Post by AnGuRuSO

I took a look at the code in that link and I'm not really sure what the guy is talking about.

#include <istream>

void ignore_line ( std::istream& in )
  char ch;

  while ( in.get ( ch ) && ch != '\n' )

How do I apply that to my code?


when you use cin>> characters are left in the input buffer, then when you call getline() the characters that are left in the input buffer, thus effectively skipping it.

So you should call that function he has used after your call to cin>> and before getline() to clear the input buffer so that the input stream is clear.



ALRIGHT! Thanks guys, got it. I've programmed a bit of stuff in C++, a few drivers, some openGL code and everyone warned me that it would bite you in the ass.

It finally did.

Anyway thanks a lot

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.