hello all,
I'm taking a C++ class at my local community college. A project that was assigned was to make a menu program. I am about 90% done (I think), when I ran into this issue: when making a selection, the program doesn't know how to handle decimal numbers. The output asks for a quantity, but there is no input, then it displays the menu again. (EDIT: when a negative decimal number is inputted, it goes into an infinite loop) What I want it to display is "That is not a valid option." I think it might have something to do with the parameter I have set for my [while] command. Any thoughts?

Example run:

(correct)
Welcome to spendCo.

Please make a selection.

Enter 1 for Coffee - $12.95
Enter 2 for Tea - $ 9.95
Enter 3 for Sugar - $ 1.50
Enter 4 for Salt - $ 2.25
Enter 5 for Milk - $ 3.00
Enter 6 to see what you owe
Enter 0 when you are done

4
Please ender desired quantity
2

(incorrect)
Please make a selection.

Enter 1 for Coffee - $12.95
Enter 2 for Tea - $ 9.95
Enter 3 for Sugar - $ 1.50
Enter 4 for Salt - $ 2.25
Enter 5 for Milk - $ 3.00
Enter 6 to see what you owe
Enter 0 when you are done

2.3
Please enter desired quantity

Please make a selection.

Enter 1 for Coffee - $12.95
Enter 2 for Tea - $ 9.95
Enter 3 for Sugar - $ 1.50
Enter 4 for Salt - $ 2.25
Enter 5 for Milk - $ 3.00
Enter 6 to see what you owe
Enter 0 when you are done

Here it is:

/******************************************************************************
 *Written by Aaron Wells  Project 02  3/03/10
 ******************************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;

/******************************************************************************
 * Note:
 * 
 ******************************************************************************/

void main()
{
	int choice;
	double qty, total = 0, total_tax;

	cout.setf(ios::fixed); 
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << "Welcome to spendCo." << endl;
	
	do
	{
	cout << "\nPlease make a selection." << endl;
	cout << "\nEnter 1 for Coffee - $12.95";
	cout << "\nEnter 2 for Tea - $ 9.95";
	cout << "\nEnter 3 for Sugar - $ 1.50";
	cout << "\nEnter 4 for Salt - $ 2.25";
	cout << "\nEnter 5 for Milk - $ 3.00";
	cout << "\nEnter 6 to see what you owe";
	cout << "\nEnter 0 when you are done\n" << endl;
	
	cin >> choice;

		switch (choice)
		{
		case 0:
			total_tax = total + total * .06;
			cout << "Proceeding to checkout...\n" << endl
				<< "Your total is: $" << total << endl
				<< "Your total after tax is: $" << total_tax << endl
				<< "Thank you, come again." << endl;
			break;
		case 1:
			cout << "Please enter desired quantity" << endl;
			cin >> qty;
			total = total + 12.95 * qty;
			break;
		case 2:
			cout << "Please enter desired quantity" << endl;
			cin >> qty;
			total = total + 9.95 * qty;
			break;
		case 3:
			cout << "Please ender desired quantity" << endl;
			cin >> qty;
			total = total + 1.50 * qty;
			break;
		case 4:
			cout << "Please ender desired quantity" << endl;
			cin >> qty;
			total = total + 2.25 * qty;
			break;
		case 5:
			cout << "Please ender desired quantity" << endl;
			cin >> qty;
			total = total + 3.00 * qty;
			break;
		case 6:
			cout << total << endl;
			break;
		default:
			cout << "That is not a valid option" << endl;
		}

	}while (choice != 0);

}

Recommended Answers

All 6 Replies

EDIT2: also crashes with a non-numeric input...

looks like I'm not 90% done...

This happens because when the input is 2.2, 5.6, etc...
"choice" takes the first number ignoring the dot(.) and the numbers after it (because "choice" is an int), so the rest need to be cleared from cin, like if we input this:
254k5874

The number in green will be taken into "choice", but the other letters and digits in red will not...

You can clear cin after requesting the input by something like this:

#include <limits> // For maximum stream size.
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

...

another simpler solution is changing the type of "choice" into double and using if statments instead of the switch statment.

commented: Kim's a great help +0

Here's the code with the changes:

#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;

/******************************************************************************
 * Note:
 * 
 ******************************************************************************/

void main()
{
	double choice;
	double qty, total = 0, total_tax;

	cout.setf(ios::fixed); 
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << "Welcome to spendCo." << endl;
	
	do
	{
	cout << "\nPlease make a selection." << endl;
	cout << "\nEnter 1 for Coffee - $12.95";
	cout << "\nEnter 2 for Tea - $ 9.95";
	cout << "\nEnter 3 for Sugar - $ 1.50";
	cout << "\nEnter 4 for Salt - $ 2.25";
	cout << "\nEnter 5 for Milk - $ 3.00";
	cout << "\nEnter 6 to see what you owe";
	cout << "\nEnter 0 when you are done\n" << endl;
	
	cin >> choice;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

		if(choice==0) {
			total_tax = total + total * .06;
			cout << "Proceeding to checkout...\n" << endl
				<< "Your total is: $" << total << endl
				<< "Your total after tax is: $" << total_tax << endl
				<< "Thank you, come again." << endl;
}
		if(choice==1) {
			cout << "Please enter desired quantity" << endl;
			cin >> qty;
			total = total + 12.95 * qty;
}
		if(choice==2) {
			cout << "Please enter desired quantity" << endl;
			cin >> qty;
			total = total + 9.95 * qty;
}
		if(choice==3) {
			cout << "Please ender desired quantity" << endl;
			cin >> qty;
			total = total + 1.50 * qty;
}
		if(choice==4) {
			cout << "Please ender desired quantity" << endl;
			cin >> qty;
			total = total + 2.25 * qty;
}
		if(choice==5) {
			cout << "Please ender desired quantity" << endl;
			cin >> qty;
			total = total + 3.00 * qty;
}
		if(choice==6) {
			cout << total << endl;
}
		else {
			cout << "That is not a valid option" << endl;
}

	}while (choice != 0);

}

Thank you so much!

That was not included in our lessons thus far, but I have been marked down for that on previous projects...sweet, 'eh?

Anyway, that fixes part one, any ideas on how to include non-numeric inputs in the [default] output? is it the same sort of issue with [int] choice?

Use cin.clear() to clear the bad states of cin, so this piece of code will look like:

cin >> choice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

Hope that helped...

sweet, thank you again.

you're a life-saver :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.