• 0

C++ getline help


Question

Hello, I am learning c++ in school and I'm a bit ahead of the class but I'm having trouble with getline which we haven't covered yet. The problem is that I'm using getline(cin, aAttraction); three times. The first time works but after that the prompts for getting a different string for aAttraction are skipped. Here is my code followed by a run of the program.

//This program calculates the number of tickets a user needs to purchase

//in order to ride 3 attractions at an amusement park. It outputs to the

//user, the number of tickets, ticket books, left of tickets, and total

//cost.

//Begin

#include<iostream>

#include<string>

using namespace std;

int numRides; //Total number of rides on all attractions

int aTimes; //Number of rides per atraction, input by user

int numTickets;

int numLeft;

float numBooks; //Number of books needed

const int PERRIDE = 4; //Number of tickets needed per ride

const int PERBOOK = 10; //Number of tickets in each book

const float BOOKCOST = 7.95; //Cost of each book

string aAttraction; //Name of attraction input by user

int main()

{

//Get Data

numRides = 0; //Prepares numRides to store total number of rides

cout << "What attraction would you like to ride? ";

getline(cin, aAttraction, '\n') ; //Get name of attraction from user

// cin >> aAttraction;

cout << endl << "How many rides on the " << aAttraction << "? ";

cin >> aTimes; //Get number of rides on attraction from user

numRides = numRides + aTimes; //Keep track of total number of rides on all attractions

cout << "What attraction would you like to ride? ";

getline(cin, aAttraction, '\n'); //Get name of attraction from user

// cin >> aAttraction;

cout << "How many rides on the " << aAttraction << "? ";

cin >> aTimes; //Get number of rides on attraction from user

numRides = numRides + aTimes; //Keep track of total number of rides on all attractions

cout << "What attraction would you like to ride? ";

getline(cin, aAttraction, '\n'); //Get name of attraction from user

cout << "How many rides on the " << aAttraction << "? ";

cin >> aTimes; //Get number of rides on attraction from user

cout << endl;

numRides = numRides + aTimes; //Keep track of total number of rides on all attractions

//Do The Math

numTickets = numRides * PERRIDE; //Calculate total number of tickets needed

numBooks = numTickets / PERBOOK; //Calculate total number of books needed

if (numTickets % PERBOOK != 0) //Check for the need of partial books

numBooks++; //Add one book if a partial book is needed

numLeft = (numBooks * PERBOOK)

- numTickets; //Calculate tickets left after riding

//Output Results

cout << "Number of tickets needed is " << numTickets << endl;

cout << "Number of ticket books to purchase is " << numBooks << endl;

cout << "Total cost of ticket books is $" << numBooks * BOOKCOST << endl;

cout << endl << "Number of tickets left after riding is " << numLeft << endl << endl;

return 0;

}

-------------------------------------------------------------------------

What attraction would you like to ride? Scrambler

How many rides on the Scrambler? 1

What attraction would you like to ride? How many rides on the ?

-------------------------------------------------------------------------

Why is it skipping the next prompt for the name of the attraction? Thanks

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
Here is a good article showing a solution: http://www.cplusplus.com/forum/articles/6046/

Thank you for the post. It helps me to understand what's going on but it's still too far ahead of the class. I'm not even supposed to use conditionals for this program. And we're supposed to use cin for a string with spaces. I don't know how we are supposed to do that.

Link to comment
Share on other sites

  • 0
Thank you for the post. It helps me to understand what's going on but it's still too far ahead of the class. I'm not even supposed to use conditionals for this program. And we're supposed to use cin for a string with spaces. I don't know how we are supposed to do that.

I suppose you could just cin >> var1 >> var2 and then join the strings together, or treat them separately dependant on what you're trying to do. Although I'll admit its a pretty bad solution, especially if you don't know how many spaces are contained in the input.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.