4 Replies - 642 Views - Last Post: 12 October 2011 - 12:55 PM Rate Topic: -----

#1 fifa12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 21-September 11

Program returning wrong answer.

Posted 12 October 2011 - 12:12 PM

Hi everyone my program is returning the wrong values at the end of the program. What is wrong with the program? It seem right.

#include <iostream>
using namespace std;

float regular () // Function that holds the price of a regular slice
{
	float regSlice = 1.50;
	return regSlice;
}
float sicilian () // Function that holds the price of a sicilian slice
{
	float siciSlice = 1.75;
	return siciSlice;
}
float calzone () // Function that holds the price of a calzone slice
{
	float calSlice = 2.00;
	return calSlice;
}
float softDrinks () // Function that holds the price of a soft drink
{
	float dollarDrinks = 1;
	return dollarDrinks;
}

int main ()
{
	int userChoice; 
	char menuAgain;
	float bevPrice;
	float pizzasubtotal;
	
	do
	{
		cout << "Welcome To My Pizzeria\n";
		cout << "Please select an option below:\n";
		cout << "1 - Pizza\n";
		cout << "2 - Soft Drink \n";
		cout << "Your choice: \n";
		
		cin >> userChoice; // User chooses whether they want pizza or a drink
		
		switch (userChoice)
		{
			case 1: // If the user pizza
			{
				int pizzaChoice;
				int pizzaAmount;
				float total, singlePizzaCost;
				float toppings_total;
				char toppings;
				bool run = true;
				bool pizzarun = true;
				bool toppings_if_else = true;
				
				cout << "1 - Regular Slice: $1.50\n";
				cout << "2 - Sicilian: $1.75\n";
				cout << "3 - Calzone: $2.00\n";
				cout << "Your choice: \n";
				
				cin >> pizzaChoice; 
				if (pizzaChoice <= 3)  
				{
					pizzarun = true; // if the user picks a option that is less than or equal excutes the options within the if statement if (pizzaChoice <= 3) 
					
					if (pizzaChoice == 1)
					{
						singlePizzaCost = regular (); // Accesses the regular function
						cout << "The total is $" << singlePizzaCost << endl; // Displays the price of regluar slice
					}
					else if (pizzaChoice == 2)
					{
						singlePizzaCost = sicilian (); // Accesses the sicilian function
						cout << "The total is $" << singlePizzaCost << endl; // Displays the price of sicilian slice
					}
					else if (pizzaChoice == 3)
					{
						singlePizzaCost = calzone(); // Accesses the calzone function
						cout << "The total is $" << singlePizzaCost << endl;  // Displays the price of calzone slice
					}
				}
				else // If the user the picks an option greater than 3 the statement below is displayed and the user is asked to input a valid option.
				{
					cout << "Use choose a valid number.\n";
					cin >> pizzaChoice;
				}
				
				cout << "How many slices will you like to order?\n";
				cin >> pizzaAmount;
				
				total = pizzaAmount * singlePizzaCost; // Multiplies the number slices with cost of a slice
				cout << "The total is $" << total << endl;
				
				cout << "Would you like to add toppings?\n";
				cout << "Toppings are $1 extra.\n";
				cout << "[y/n]\n";
				
				cin >> toppings;
				
				if (toppings == 'y') // If the user enters y the toppings menu is displayed
				{
					run = true; 
					int toppingsChoice;
					
					cout << "1 - Pepperoni\n";
					cout << "2 - Sausage\n";
					cout << "3 - Extra Cheese\n";
					cout << "4 - Veggie\n";
					cout << "Your choice: \n";
					
					cin >> toppingsChoice; // User inputs topping choice
					
					if (toppingsChoice < 5)
					{
						toppings_if_else = true;
						toppings_total = total + 1; // One gets added to total cost of pizza.
						cout << "The total is $" << toppings_total << endl;
					}
					else
					{
						cout << "Please choose a valid number.\n";
						cin >> toppingsChoice;		
					}
				}
				pizzasubtotal = total + toppings_total;
				break;
			}
			case 2: // If the user chooses soft drinks
			{
				int drinks;
				int numDrinkAmount;
				
				cout << "Please select a beverage. Each beverage is $1.00\n";
				cout << "1 - Pepsi\n";
				cout << "2 - Sprite\n";
				cout << "3 - Snapple\n";
				cout << "4 - Water\n";
				
				cin >> drinks;
				
				float drinkPrice = softDrinks ();
				cout << "The total is $" << drinkPrice << endl; 
				
				
				cout << "How many drinks would you like?\n";
				cin >> numDrinkAmount;
				
				bevPrice = 1 * numDrinkAmount;
				
				cout << "The total is $" << bevPrice << endl;
				
				break;
			}
			default:
            cerr << "That's not a menu option!" <<endl;
            break;
		}
			cout << "Would you like to order anything else?\n";
			cout << "[y/n]\n";
			cin >> menuAgain;
	}
	while (menuAgain == 'y');
	
	float subtotal = bevPrice + pizzasubtotal;
	
	cout << "The total cost of your order is $" << subtotal << "." << endl;
	
	return 0;
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Program returning wrong answer.

#2 vividexstance  Icon User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,870
  • Joined: 31-December 10

Re: Program returning wrong answer.

Posted 12 October 2011 - 12:24 PM

What are you giving for input and what do you expect to be the output? Also, what are you getting for output that is wrong?

One thing that might be a problem is the part that asks if they want to order anything else, and you input a char, expecting it to be either 'y' or 'n'. There might be a newline('\n') still in the input stream, so right before you input menuAgain, put a cin.ignore() and see if that helps. What that does is ignore and remove the next character in the input stream. So if this is your problem, it should fix it.
Was This Post Helpful? 0
  • +
  • -

#3 fifa12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 21-September 11

Re: Program returning wrong answer.

Posted 12 October 2011 - 12:36 PM

View Postvividexstance, on 12 October 2011 - 12:24 PM, said:

What are you giving for input and what do you expect to be the output? Also, what are you getting for output that is wrong?

One thing that might be a problem is the part that asks if they want to order anything else, and you input a char, expecting it to be either 'y' or 'n'. There might be a newline('\n') still in the input stream, so right before you input menuAgain, put a cin.ignore() and see if that helps. What that does is ignore and remove the next character in the input stream. So if this is your problem, it should fix it.

When i'm done ordering it gives me the wrong answer, for example: I order 1 regular slice with toppings ($2.50) and one drink ($1.00). At the end of my order i get $5 for the total order.
Was This Post Helpful? 0
  • +
  • -

#4 Peregrinus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 19-June 11

Re: Program returning wrong answer.

Posted 12 October 2011 - 12:42 PM

You shouldn't use functions to hold just a single value. The point of functions is to do /something/ that you plan on repeating, or for organization. For example:

double add( int x, int y )
{
     int iTotal = x + y;

     return iTotal;
}



You are technically allowed to do as you did, but it's bad practice; in my opinion at least. I suggest you keep those values in the main because you aren't using them anywhere else.

As for your return values, I compiled it myself and they work just fine. Your only problem is calculating the subtotal. And with the subtotal it is that you aren't assigning the variable bevPrice any value. So, if the variable isn't assigned anything, when the subtotal is calculated it is multiplying everything else by an unassigned variable; which, consequently, has been randomly assigned by the computer a very large number.

Hope that helped.

Perry
Was This Post Helpful? 0
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 221
  • View blog
  • Posts: 594
  • Joined: 04-August 09

Re: Program returning wrong answer.

Posted 12 October 2011 - 12:55 PM

One reason you're getting wrong vaules is because you have different variables for total. i.e. pizzaAmount, pizzasubtotal, subtotal, total ... and you're adding them up incorrectly. Just have one total then add to it at each stage. Keep it simple.


You should break your program up into functions. So maybe have a function for pizza choice, another for the drink choice and another for toppings etc ... Break it into smaller pieces. It helps with finding errors. If you have lots of smaller functions, rather than one large one, it'll help you find the errors.

Your error checking won't work either. If its an invalid input you get a new input and then don't do anything with it. What if the second inputs invalid?

Also you should initialize your variables.

This post has been edited by simeesta: 12 October 2011 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1