promotion image of download ymail app


Hello, I have a simple assignment where I need to make the "item name" value be more than 1 word. When I enter "Milk" the program runs perfect, but when I enter "Chocolate Ice Cream" it does not work and prints the entire program immediatly. I have tried so many things to fix this. Please help.

3 Answers

  • 8 months ago

    You have no loop statements, so you know that program is going to run straight through just once and return, right?

    I compiled and ran the code shown in your answer and it seems to run just fine here. This is the output:

    Please input the item name

    Chocolate Ice Cream

    Please input the number of items bought


    Please input the price of each item


    The total bill is 59.88

    Process returned 0 (0x0) execution time : 27.415 s

    Press any key to continue.

    If you plan to add a loop to input more than one item, you're going to run into a problem because you're mixing line-oriented input (getline) with token-oriented input (cin>>).

    The problem is that when you use cin>> to read one or more characters, it stops at (and does not discard) the first character than isn't part of the value being read in. In this case, that's probably the '\n' newline (aka end of line) character. If a getline() immediately follows, it will see that '\n' character in the buffer and return an empty string.

    That '\n' character isn't usually a problem if another cin>> input follows because most data types will skip over spaces, tabs before starting to read a number of a space-delimited string. Anything else will see the unread end of the line from the previous cin>> and treat it as input.

    There are a few ways to fix this. You can follow each cin>> with a call to discard the end of line. The "proper" way to do that is an awful mess:

    std::cin >> some_number;

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

    66 characters, not counting spaces (and I inserted a couple just to keep it from getting truncated), *and* you need to add a #include <limits> statement at the top! You shorten that to a "mere" 51 characters by getting rid of the std:: prefixes, but that's a reason you'll see code doing something like this instead:

    std::cin >> some_number;

    std::cin.ignore(9999, '\n');

    That will ignore up to 9,999 character to find (and discard) a newline. I don't suggest doing this in production code (or for a job interview question), but it works when the users are civilized. Anyone who auto-repeats a character ten thousand times before pressing enter deserves what they get.

    Another easy way to do this is to just do a getline on a string that you won't be using.

    std::string trash;

    std::cin >> some_number;

    std::getline(cin, trash);

    • Commenter avatarLog in to reply to the answers
  • Damien
    Lv 5
    8 months ago

    put it under inverted commas

    • Commenter avatarLog in to reply to the answers
  • 8 months ago

    // This program will read in the quantity of a particular item and its price.

    // It will then print out the total price.

    // The input will come from the keyboard and the output will go to

    // the screen.

    // Michael Orlando

    #include <iostream>

    #include <iomanip>

    #include <string>

    using namespace std;

    int main()


    int quantity; // contains the amount of items purchased

    float itemPrice; // contains the price of each item

    float totalBill; // contains the total bill.

    string itemName;

    cout << setprecision(2) << fixed << showpoint; // formatted output

    cout << "Please input the item name" << endl;

    getline(cin, itemName);

    cout << "Please input the number of items bought" << endl;

    cin >> quantity;// Fill in the input statement to bring in the quantity.

    cout << "Please input the price of each item" << endl; // Fill in the prompt to ask for the price.

    cin >> itemPrice; // Fill in the input statement to bring in the price of each item.

    totalBill = quantity*itemPrice; // Fill in the assignment statement to determine the total bill.

    cout << "The total bill is " << totalBill;// Fill in the output statement to print total bill,

    // with a label to the screen.

    return 0;


    Sorry forgot to post my code. Here it is.

    • Commenter avatarLog in to reply to the answers
Still have questions? Get answers by asking now.