Guys I am completely lost....what I want to do is make a loop to where the code im about to show. The issue is I need when I type a negative number in hotdogs, tacos, etc to make me type an error message and stop the code...here is my code and if I don't text wrap right let me know and show me how to do it right

//Assignment: Lab 2
//Date: 01/22/08
#include <iostream>
#include <string>
using namespace std;
const double TACO_COST = 0.49;   
const double HOTDOG_COST = 0.75;
const double DRINKS_COST = 0.99;
const double SOURCREAM_COST = 0.15;
const double TAX_RATE = 0.0725;
int sum = 0;
int count = 0;
int main ()
{



	int hotdogs, tacos, tacos_with_sourcream, drinks;
	double hotdogs_total_cost, tacos_total_cost, tacos_with_sourcream_total_cost, drinks_total_cost, subtotal, taxes, total_cost;
	char ans;



	cout << endl << "Programmed By: Bryan Kruep \n" <<endl;

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


	do
	{

	


	cout << "Number of Hotdogs: ";
	cin >> hotdogs;



		
	cout << "Number of Tacos: ";
	cin >> tacos;

	

	

		
	cout << "How many of those tacos with sour cream: ";
	cin >> tacos_with_sourcream;
    cout << "Number of drinks: ";
	cin >> drinks;


	
	

	cout << endl << endl << endl << "BILL";
	cout << endl << "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _";
    
	cout << endl << "\t" << hotdogs << " Hotdogs"; 
	cout << endl << "\t" << tacos << " Tacos" << " - " << tacos_with_sourcream << " with sour cream";
	cout << endl << "\t" << drinks << " Drinks";
	

	//Calculate totals
	hotdogs_total_cost = hotdogs * HOTDOG_COST;
	tacos_total_cost = tacos * TACO_COST;
	tacos_with_sourcream_total_cost = tacos_with_sourcream * SOURCREAM_COST;
	drinks_total_cost = drinks * DRINKS_COST;
	subtotal = hotdogs_total_cost + tacos_total_cost + tacos_with_sourcream_total_cost + drinks_total_cost;
	taxes = subtotal * TAX_RATE;
	total_cost = subtotal + taxes;

	//Display output

	cout << endl << endl << "Subtotal: " << subtotal;
	cout << endl << "Taxes: " << taxes;
	cout << endl << "Total: " << total_cost;
	cout << endl;

			sum = 0 ;
		cout << "Another order? (Y/N)";
		cin >> ans;
		cout << endl << endl;
	
 
	}while ( (ans != 'n') && (ans != 'N') );

	

return 0;
}

Recommended Answers

All 15 Replies

you forgot to post the code you have written so far

[edit]Oops! you used wrong kind of tags which hid your code. [/edit]

When you have all the input, just check if one of them is smaller then 0 and print a error message:

if (tacos < 0 || hotdogs < 0 ||  [etc...]   )
{
    cout << "Error message" << endl;
    return 0;
}

The || sign are two 'pipes' and mean: 'or'. So if tacos are smaller then 0 OR hotdogs are smaller then 0 OR (etc)

Niek

When you have all the input, just check if one of them is smaller then 0 and print a error message:

Or better yet, wrap each input in a while loop that tests for <0:

inp = -1;    // force the value to be < 0
while (inp < 0)
{
    cout << "Input Value: ";
    cin  << inp;
    if (inp < 0)    // test the new value
    {
        cout << "try again" << endl;
    }
}

...and if I don't text wrap right let me know and show me how to do it right

Thanks for asking :) See this

can anyone show me how to do with the tags to make them in color

you mean [code=c++] // your code here

[/code]

The above code tags will add line numbers and colorize your code.

still having problems this I what I came up with:

//Name: Bryan Kruep
//Class: CS 140-001
//Assignment: Lab 2
//Date: 01/22/08
#include <iostream>
#include <string>
using namespace std;
const double TACO_COST = 0.49;   
const double HOTDOG_COST = 0.75;
const double DRINKS_COST = 0.99;
const double SOURCREAM_COST = 0.15;
const double TAX_RATE = 0.0725;
int sum = 0;
int count = 0;
int main ()
{



	int hotdogquantity, tacoquantity, sourcreamquantity, drinkquantity;
	double hotdogs_total_cost, tacos_total_cost, tacos_with_sourcream_total_cost, drinks_total_cost, subtotal, taxes, total_cost;
	char ans;



	cout << endl << "Programmed By: Bryan Kruep \n" <<endl;

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


	{

	
if (hotdogquantity >= 0)

{
	 


	cout << "Number of Hotdogs: ";
	cin >> hotdogquantity;
}
else 
cout << "Number of hotdogs cannot be a negative number!! Try again: ";

if (tacoquantity >= 0)
{

	cout << "Number of Tacos: ";
	cin >> tacoquantity;

}

else
cout << "Number of tacos cannot be a negative number!! Try again: ";

if (sourcreamquantity < tacoquantity)
{

	cout << "How many of those tacos with sour cream: ";
	cin >> sourcreamquantity;
}
else 
cout << "Can't have more tacos with sourcream than the number of tacos ordered. Number of sour creams cannot be a negative. Try again: ";

if (drinkquantity >= 0)
{
	cout << "Number of drinks: ";
	cin >> drinkquantity;
}
else

cout << "Number of drinks cannot be a negative number!! Try again: ";



	
	
	

	cout << endl << endl << endl << "BILL";
	cout << endl << "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _";
    
	cout << endl << "\t" << hotdogquantity << " Hotdogs"; 
	cout << endl << "\t" << tacoquantity << " Tacos" << " - " << sourcreamquantity << " with sour cream";
	cout << endl << "\t" << drinkquantity << " Drinks";
	

	//Calculate totals
	hotdogs_total_cost = hotdogquantity * HOTDOG_COST;
	tacos_total_cost = tacoquantity * TACO_COST;
	tacos_with_sourcream_total_cost = sourcreamquantity * SOURCREAM_COST;
	drinks_total_cost = drinkquantity * DRINKS_COST;
	subtotal = hotdogs_total_cost + tacos_total_cost + tacos_with_sourcream_total_cost + drinks_total_cost;
	taxes = subtotal * TAX_RATE;
	total_cost = subtotal + taxes;

	//Display output

	cout << endl << endl << "Subtotal: " << subtotal;
	cout << endl << "Taxes: " << taxes;
	cout << endl << "Total: " << total_cost;
	cout << endl;

			sum = 0 ;
		cout << "Another order? (Y/N)";
		cin >> ans;
		cout << endl << endl;

 
}while ( (ans != 'n') && (ans != 'N') );

	

return 0;
}

It's times like this where I think it would be simpler to use a function. Follow the previous advice of WaltP, creating a while() loop. This function will keep looping until the user enters a non-negative integer.

int getPositiveInt() {

   int value = -1;
   cin >> value;

   // keep looping until user enters a non-negative number
   while (value < 0)
   {
      cout << "Error, please enter a non-negative number.\n";
      cin >> value;
   }

   return value;
}

To use, it's very simple:

cout << "Number of hot dogs: ";
hotdogquantity = getPositiveInt();

I originally used just the quantity as my integer but when I get down to the sourcream I need to display an error message if tacos with sourcream are more than the tacos quantity...wouldnt that mess it up?

Sorry, I didn't read through all your code. You'd have to do it manually for this one:

int sourcreamquantity = 0;
cin >> sourcreamquantity;

// keep looping until user enters less sourcream than tacos
while sourcreamquantity > tacoquantity)
{
   cout << "Error, tacos with sour cream must be less than total number of tacos.\n";
   cin >> sourcreamquantity;
}

guys im almost there I am getting 55 errors tho so I think I just about have it can anyone see what I am doing wrong it has to be something I can't find?

//Name: Bryan Kruep
//Class: CS 140-001
//Assignment: Lab 2
//Date: 01/22/08
#include <iostream>
#include <string>
using namespace std;
const double TACO_COST = 0.49;   
const double HOTDOG_COST = 0.75;
const double DRINKS_COST = 0.99;
const double SOURCREAM_COST = 0.15;
const double TAX_RATE = 0.0725;
int sum = 0;
int main ()
{

	int hotdogquantity, tacoquantity, sourcreamquantity = 0, drinkquantity;
	double hotdogs_total_cost, tacos_total_cost, tacos_with_sourcream_total_cost, drinks_total_cost, subtotal, taxes, total_cost;
	char ans;

	cout << endl << "Programmed By: Bryan Kruep \n" <<endl;

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

	if (hotdogquantity >= 0)
{
	cout << "Number of Hotdogs: ";
	cin >> hotdogquantity;
}

while (hotdogquantity < 0)
{
	
cout << "Number of hotdogs cannot be a negative number!! Try again: ";
cin >> hotdogquantity;
}

if (tacoquantity >= 0)
{

	cout << "Number of Tacos: ";
	cin >> tacoquantity;
}

while (tacoquantity < 0)
{
cout << "Number of tacos cannot be a negative number!! Try again: ";
cin >> tacoquantity;
}

if (sourcreamquantity < tacoquantity)
{
	cout << "How many of those tacos with sour cream: ";
	cin >> sourcreamquantity;
}

while (sourcreamquantity > tacoquantity)
{

cout << "Can't have more tacos with sourcream than the number of tacos ordered. Number of sour creams cannot be a negative. Try again: ";
cin >> sourcreamquantity;
}

if (drinkquantity >= 0)
{
	cout << "Number of drinks: ";
	cin >> drinkquantity;
}
while (drinkquantity < 0)
(
cout << "Number of drinks cannot be a negative number!! Try again: ";
cin >> drinkquantity;
}


	cout << endl << endl << endl << "BILL";
	cout << endl << "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _";
    
	cout << endl << "\t" << hotdogquantity << " Hotdogs"; 
	cout << endl << "\t" << tacoquantity << " Tacos" << " - " << sourcreamquantity << " with sour cream";
	cout << endl << "\t" << drinkquantity << " Drinks";
	
	//Calculate totals
	hotdogs_total_cost = hotdogquantity * HOTDOG_COST;
	tacos_total_cost = tacoquantity * TACO_COST;
	tacos_with_sourcream_total_cost = sourcreamquantity * SOURCREAM_COST;
	drinks_total_cost = drinkquantity * DRINKS_COST;
	subtotal = hotdogs_total_cost + tacos_total_cost + tacos_with_sourcream_total_cost + drinks_total_cost;
	taxes = subtotal * TAX_RATE;
	total_cost = subtotal + taxes;

	//Display output

	cout << endl << endl << "Subtotal: " << subtotal;
	cout << endl << "Taxes: " << taxes;
	cout << endl << "Total: " << total_cost;
	cout << endl;
	

		sum = 0 ;
		cout << "Another order? (Y/N)";
		cin >> ans;
		cout << endl << endl;

 }while ( (ans != 'n') && (ans != 'N') );

return 0;
}

I have a feeling iot is in one of my braces

After a couple requests and an explanation on how to use them, you still refuse to use CODE tags. Is there a reason for this?

while (drinkquantity < 0)
(

This brace should be a curly one { not (

}while ( (ans != 'n') && (ans != 'N') );

This while is missing a do { Just place is somewhere after de declaration of ans.

READ THIS about using CODE TAGS. If you use indentation, you would have found the problems yourself. Here's a nice thread about it

Niek

and i do the same for the other 3 quantities right

nevermind figured it out...thanks everyone for your help

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.