0

So I made this to try understanding classes and this error keeps coming up where in the command prompt tab, it doesn't ask the user to input the next required input when an integer is being asked. I added a comment in the code below for you to know where the error arises from.

#include <iostream>
using namespace std;

class Anime
{
    public:
        string Name;
        int Year;
        string Genre;

    Anime(string aName, int aYear, string aGenre)
    {
        Name = aName;
        Year = aYear;
        Genre = aGenre;
    }
};

int main()
{
    string aniName;
    int aniYear;
    string aniGenre;
    
    // This asks for the year
    cout << "Anime Year: ";
    cin >> aniYear;

    // The "Anime Name" line runs but the input for aniName is not asked and skips to the next input
    cout << "Anime Name: ";
    getline(cin, aniName);

    cout << "Anime Genre: ";
    getline(cin, aniGenre);

    Anime Anime1(aniName, aniYear, aniGenre);
    cout << Anime1.Name << endl;
    cout << Anime1.Year << endl;
    cout << Anime1.Genre << endl;
}

When i make the integer the last input, it works perfectly, but at if I don't want it to be the last one. In that case, what do I do? Any answer is greatly appreciated.

Katsuniya
  • 1
  • 2
  • Ask yourself this, when you enter the year you type a newline, but does `cin >> aniYear;` read a newline? Obviously it doesn't because a newline is not a digit and therefore is never part of an integer. That is the explanation, for what you should do about it read the duplicate. – john Jan 23 '21 at 12:32

1 Answers1

0

Try to add #include <string>

And add below code upper the input code.

    cin >> aniYear;

    // The "Anime Name" line runs but the input for aniName is not asked and 
        skips to the next input
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); 

    cout << "Anime Name: ";
    getline(cin, aniName);
Phillip
  • 26
  • 3
  • It gave me this error when i added it. C:\Users\katsu\Documents\Projects\Program\main.cpp||In function 'int main()':| C:\Users\katsu\Documents\Projects\Program\main.cpp|29|error: 'numeric_limits' is not a member of 'std'| C:\Users\katsu\Documents\Projects\Program\main.cpp|29|error: expected primary-expression before '>' token| C:\Users\katsu\Documents\Projects\Program\main.cpp|29|error: no matching function for call to 'max()'| @jamesono0223 – Katsuniya Jan 23 '21 at 12:50