TakShing
TakShing

Reputation: 145

Accepting input in relation to an array of structures

I am currently practicing C++ and I am doing this question from the textbook C++ Primer Plus, and I am stuck at this very last step. Basically I am to make an array out of a structure that contains info a car and the issue that I am having trouble with is recording the user's input.

My code:

#include <iostream>
#include <string>

struct car{
    std::string make;
    int year;
};

int main()
{
    std::cout << "How many cars do you wish to catalog? ";
    int lim;
    std::cin >> lim;
    car* info = new car[lim];
    for(int i = 0; i<lim; i++)
    {
        std::cout << "Please enter the make: ";
        getline(std::cin, info[i].make); // problem here..
        std::cout << "Please enter the year made: ";
        std::cin >> info[i].year; // problem here as well :(
    }
    std::cout << "here is your collection:\n";
    while(int i = 0 < lim)
    {
        std::cout << info[i].make << " " << info[i].year << std::endl;
        //std::cout << "here is your collection:\n"
        i++;

    }
    return 0;
}

Can someone help explain why it isn't working?

Specifically, my issue is that it is not getting my input correctly, and my exe file seems to skip the input of the "make" question and jumps to the year..Then it crashes into oblivious..possibly a segmentation fault.

Upvotes: 0

Views: 72

Answers (3)

R Sahu
R Sahu

Reputation: 206687

After you read numbers using

std::cin >> lim;

and

    std::cin >> info[i].year;

a newline character is left on the stream, which is picked up by getline as valid input.

You need to add code to ignore the rest of the line.

std::cin >> lim;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

and

    std::cin >> info[i].year;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

See documentation on istream::ignore.

Also, change

while(int i = 0 < lim)
{
    std::cout << info[i].make << " " << info[i].year << std::endl;
    //std::cout << "here is your collection:\n"
    i++;
}

to

for(int i = 0; i < lim; ++i)
{
    std::cout << info[i].make << " " << info[i].year << std::endl;
    //std::cout << "here is your collection:\n"
}

Upvotes: 2

shawnjzlee
shawnjzlee

Reputation: 23

#include <iostream>
#include <string>

struct car{
    std::string make;
    int year = 0;
};

int main()
{
    int i = 0; //increment value

    std::cout << "How many cars do you wish to catalog? ";
    int lim;
    std::cin >> lim;
    car* info = new car[lim];
    for(i;  i < lim; i++)
    {
        std::cout << "Please enter the make: ";
        std::cin >> info[i].make; // change to cin, just like the one for year
        std::cout << "Please enter the year made: ";
        std::cin >> info[i].year; // this was fine
    }
    std::cout << "here is your collection:\n";
    i = 0; //resets the increment value
    while(i < lim)
    {
        std::cout << info[i].make << " " << info[i].year << std::endl;
        //std::cout << "here is your collection:\n"
        i++;
    }
    return 0;
}

Upvotes: 2

Coding Orange
Coding Orange

Reputation: 542

combining cin and getline is ... funky. cin never read the newline character from the first string, so your first getline call is going to pretty much just return a blank string. What I normally do when I have an issue like this is perform a throwaway getline() call after my cin.

combining getline and cin is not normally very friendly. Maybe you should switch to all getlines and do a little bit of string manipulation?

Upvotes: 0

Related Questions