0

I have a code that is supposed to use user input to take an order of a pizza.

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

int main()
{
double totalCosts = 0;

// PIZZA

cout << "What size is your first pizza?\n"
        "small:    $8.00\n"
        "medium:  $10.00\n"
        "large:   $12.00\n";
string currentSize = "";
getline(cin, currentSize);
cout << "\n";
if(currentSize == "small")
{
    totalCosts += 8;
}
else if(currentSize == "medium")
{
    totalCosts += 10;
}
else if(currentSize == "large")
{
    totalCosts += 12;
}
else
{
    cout << "invalid size: " << currentSize;
    return 0;
}

string pizzaToppings = "";
cout << "Choose from the following toppings:\n"
        "onions:    $1.00\n"
        "peppers:   $1.20\n"
        "ham:       $1.50\n"
        "hamburger: $1.25\n"
        "pepperoni: $1.55\n"
        "salami:    $1.63\n"
        "sausage:   $1.44\n";
getline(cin, pizzaToppings);
cout << "\n";
if(pizzaToppings.find("onions") != string::npos)
{
    totalCosts += 1;
}
else if(pizzaToppings.find("peppers") != string::npos)
{
    totalCosts += 1.2;
}
else if(pizzaToppings.find("ham") != string::npos)
{
    totalCosts += 1.5;
}
else if(pizzaToppings.find("hamburger") != string::npos)
{
    totalCosts += 1.25;
}
else if(pizzaToppings.find("pepperoni") != string::npos)
{
    totalCosts += 1.55;
}
else if(pizzaToppings.find("salami") != string::npos)
{
    totalCosts += 1.63;
}
else if(pizzaToppings.find("sausage") != string::npos)
{
    totalCosts += 1.44;
}
else
{
    cout << "Choose at least one valid topping";
    return 0;
}

// DELIVERY AND FINAL PRINTOUT

string needToDeliver = "";
cout << "Do you want delivery? yes/no\n";
getline(cin, needToDeliver);
cout << "\n";
if(needToDeliver == "yes")
{
    totalCosts += 3;
    cout << "What is your address?";
    getline(cin, needToDeliver); // address doesn't matter
}

cout << "Your total cost is: $" << totalCosts << ". Thank you for buying C++ Pizza.";
return 0;
}

This code gives the following output:

What size is your first pizza?

small: $8.00

medium: $10.00

large: $12.00

invalid size:

What I think is happening is the first getline function is not waiting for any user input, and is instead inputing an empty string. Why is it doing this?

i .
  • 485
  • 2
  • 10
  • 23
  • 3
    Please reduce your program to a minimal example that reproduces your issue. It shouldn't be more than 20 lines of code in this case. Along the way you might even figure out the solution yourself! – John Zwinck Apr 28 '13 at 03:28
  • seems working fine here if you input small: http://ideone.com/VYxaxP (no topping is selected) – taocp Apr 28 '13 at 03:29
  • Are you sure you don't have any `cin >> x` anywhere? – chris Apr 28 '13 at 03:38
  • 1
    In what kind of computer are you compiling and running this program? It works just fine on my computer. Are you using Visual Studio or any other IDE? – ILikeTacos Apr 28 '13 at 03:51
  • I'm using compile online. – i . Apr 28 '13 at 17:32

0 Answers0