1

The content of my program does not matter for this question, but pretty much we're using classes and objects to save information. My code is as follows:

#include <iostream>
using namespace std;

class Car
{
private:
    int carNumber;
    bool loaded;
    string reportingMark, kind, destination;
public:
    void setUp(string reportingMark, int carNumber, string kind, bool 
loaded, string destination);
    void output();
};

void input(string &reportingMark, int &carNumber, string &kind, bool 
&loaded, string &destination);

/* ******************** main ********************
 */
int main()
{
    Car *ptr;

    string reportingMark, kind, destination;
    int carNumber;
    bool loaded;

    Car c = *new Car();
    ptr = &c;
    input(reportingMark, carNumber, kind, loaded, destination);
    ptr->setUp(reportingMark, carNumber, kind, loaded, destination);
    ptr->output();
    return 0;
}
/* ******************** Member Functions ********************
 */

/* ******************** setUp ********************
 Takes 5 parameters by value: reporting mark, car number, kind, loaded, 
and destination
 Inputs these paramaters into the Car class' variables
 */
void Car::setUp(string rm, int cn, string k, bool l, string d)
{
    reportingMark = rm, carNumber = cn, kind = k, loaded = l, 
destination = d;
}

/* ******************** output ********************
 Prints the reporting mark, car number, kind, loaded, and destination 
in a neat format
 */
void Car::output()
{
    cout << "\n\nReporting Mark: " << reportingMark << "\nCar Number: " 
<< carNumber << "\nKind: " << kind << "\nCar is loaded: " << loaded << 
"\Destination: " << destination << endl;
}
/* ******************** Global Functions ********************
 */

/* ******************** input ********************
 Takes the reporting mark, car number, kind, loaded, and destination as 
reference parameters
 Asks the user to input these values
 */
void input(string &reportingMark, int &carNumber, string &kind, bool 
&loaded, string &destination)
{
    do
    {
        cout << "What is the Reporting Mark? (Between 2-4 characters) ";
        getline(cin, reportingMark);
    if(reportingMark.length() < 2 || reportingMark.length() > 4) cout << "invalid input, try again\n";
} while(reportingMark.length() < 2 || reportingMark.length() > 4);
cout << "What is the Car Number? ";
cin >> carNumber;
int carNumResponse;

do
{
    cout << "What is the Kind? (Choose '1' for business, '2' for maintenance, or '3' for other) ";
    cin >> carNumResponse;
    if(carNumResponse < 1 || carNumResponse > 3) cout << "invalid input, try again\n";
} while(carNumResponse < 1 || carNumResponse > 3);
if(carNumResponse == 1) kind = "business";
if(carNumResponse == 2) kind = "maintenance";
if(carNumResponse == 3) kind = "other";

int loadedResponse;

do
{
    cout << "Is the car loaded? (Type '1' if true and '2' if false) ";
    cin >> loadedResponse;
    if(loadedResponse < 1 || loadedResponse > 2) cout << "invalid input, try again\n";
} while(loadedResponse < 1 || loadedResponse > 2);
if(loadedResponse == 1) loaded = true;
if(loadedResponse == 2) loaded = false;

cout << "What is the Destination? ";
getline(cin, destination);
}

The last two lines of my program:

cout << "What is the Destination? ";
getline(cin, destination);

Doesn't let me input anything for some reason. Am I using getline incorrectly? It's used the same way in other parts of the program. Thanks to anyone that helps!

  • what does "don't run" mean ? is the cout being written to the screen ? does the getline fail ? do both fail ? are you getting an error ? – xyious Oct 30 '17 at 20:04
  • I edited it, basically I would see the message "What is the destination?" but it would not let me input a response. – Ronald Johnson Oct 30 '17 at 20:08
  • Why are you dynamically allocating variables? C++ != Java, you don't need to use dynamic memory to allocate instances of classes. – Thomas Matthews Oct 30 '17 at 20:26
  • @ThomasMatthews it's in the instructions of the assignment to use a pointer to the class – Ronald Johnson Oct 30 '17 at 20:43
  • Perhaps try 'std::cin.getline' - you'd need to reserve() space for destination beforehand. – kvr Oct 30 '17 at 20:43

1 Answers1

0

The problem here is that you had a cin>> before doing the getline operation. What cin>> does is that it extracts all the characters from the stream, but leaves behing the newline character.

For getline the default delimiter is the newline character. So it skips input, as it recieves a newline character left behind in the stream.

Solution, you must do a cin.ignore (), this will flush the stream,i.e., the newline character is removed. I recommend doing cin.ignore () before using getline() function.

Attributes: Help from post: How to use cin.getline in blocking mode

Vedant
  • 125
  • 13