DanielBenson DanielBenson - 1 year ago 89
C++ Question

In c++, my program automatically goes to the next CIN prompt without letting the user add input

Here's my code:

#include <iostream>

using namespace std;

const int SENIOR_PRICE = 9;
const int ADULT_PRICE = 12;
const float CHILD_PRICE = 6.95;
const float TAX_RATE = .06;

int main()
string name;
string address;
int numSeniorTickets;
int numAdultTickets;
int numChildTickets;

cout << "Enter customer name:" << endl;
cin >> name;

cout << "Enter customer address:" << endl;
cin >> address;

cout << "How many senior season tickets?" << endl;
cin >> numSeniorTickets;

cout << "How many adult tickets?" << endl;
cin >> numAdultTickets;

cout << "How many child tickets?" << endl;
cin >> numChildTickets;

return 0;

After the user enters their name, they are then prompted to enter their address. But before they can enter their address it also prompts for the number of senior season tickets. Why is it skipping a line of input? Here's what happens:

Enter customer name:
Daniel Benson
Enter customer address:
How many senior season tickets?
How many adult tickets?
How many child tickets?

Answer Source

cin >> name;

Takes the input only upto space. Therefore if you print the variable name you will get Daniel. And if you print the variable address you will get Benson. So as far as the program is concerned it has taken in two strings as input.

A proof of the above statements by printing the variables after input.

You might want to use cin.getline() for taking space separated strings as input.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download