coding C++ for class, need help on code error

im doing the box office question. my question thus far is why is the code skipping past my cin statements after the forst movie=cin.get(); line. it lets you enter the movie name the prints out the nect 2 couts but never lets you input just displays them. What am i doing wrong

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

int main()
{
char movie;
double adultickets;
double childtickets;
double adultprice;
double childprice;
double gross;
double net;
double paid2dist;

adultprice=6.00;
childprice=3.00;

cout<<"enter the name of the movie: "<<endl;
movie=cin.get();
cin.get or getline
cin.ignore();
cout<<"number of adult tickets sold: \n";
cin>>adultickets;
cout<<"number of childrens tickets sold: \n";
cin>>childtickets;

return 0;
}
Last edited on
Firstly, use string rather than char for a word, as char is one 8-bit character, while string is text.

To get a string from cin that's more than one word use getline. You give it the input-stream (cin) and string variable to put it in (movie), like this:
getline(cin, movie);

Take away the line that says "cin.get or getline" as it seems like a unmarked comment.

The problem you're describing is that the cin asks for one character and after getting that character, fills the rest of the characters into the doubles, failing as it don't understand how a bunch of letters can be a double.

cin.ignore should have the parameters (1000, '\n'), which means "ignore until 1000 chars have passed or passed a newline-char". However, in your example, it is not needed as when using getline, the cin buffer will be empty afterwards.
Last edited on
Topic archived. No new replies allowed.