Ask coding questions

← Back to all posts
Trying output to read white space
moonlitdeath (0)

I have to write a code that reads chocolate ice cream when inputted and prompted to then read it back later in the program but cant get it to read the whitespace (spaces) which cause the program to crash and don't know how to user the cin.get() correctly, any siggestions/help is greatly appreciated

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

int main()
string itemName;
int quantity; // contains the amount of items purchased
float itemPrice; // contains the price of each item
float totalBill; // contains the total bill.

cout << "Please input the name of the item" << endl;
cin >> itemName;

cout << setprecision(2) << fixed << showpoint;	// formatted output 
cout << "Please input the number of items bought" << endl;

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

// Fill in the prompt to ask for the price.
cout << "Please input the the total cost of the items" << endl << "$";

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

cout << "The item you bought is: " << itemName << endl;

// Fill in the assignment statement to determine the total bill.
totalBill = quantity * itemPrice;
cout << "The total bill is: $" << totalBill;

// Fill in the output statement to print total bill,
// with a label to the screen.

return 0;


FishballNooodle (178)

Assuming this is written in C++
Consider the following:

string itemName;
cout << "Give a name to the item: ";
cin >> x;
cout << "Item Name is: " << x;
moonlitdeath (0)

@FishballNooodle while running single words such as milk the program works fine but anything more than one single word such as "chocolate ice cream" or "Chicken Taco" the program crashes and won't let me input anything else and also only registers the first word

Creatito1 (6)

What language is this in?