0

I'm messing around with enums and have this program almost working 100%.

The only issue is when I call my getFavMusic() function during the second iteration of the do-while loop, cin.getline skips my input entirely.

I've read things about the newline character messing with the input stream but i've tried running it without the \n in my cout statement to no avail.

Why does it skip and use a "" as input?

#include <iostream>
#include <cstring>
using namespace std;

enum musicGenres { RAP, HIPHOP, ROCK, INDIE, CLASSICAL, ALTERNATIVE };

void getFavMusic(musicGenres &);
void showFavMusic(musicGenres);

int main() {

    musicGenres currFav;
    char cont;

    do {
        getFavMusic(currFav);  //Ask Matt for his favorite music genre today and store it as a musicGenres (enum) type
        showFavMusic(currFav); //Output Matt's favorite music genre today by converting the musicGenres enum type to a cstring

        cout << "Enter another genre? (y or n): ";
        cin >> cont;
        cont = tolower(cont);
    } while (cont == 'y');

    return 0;
}

void getFavMusic(musicGenres &currFav) {

    char fav[20]; //Where input is stored

    cout << "Hey Matt! What's your favorite music today?\n";
    cin.getline(fav, 20); //Keeps skipping here after first entry

    for (int i = 0; i < strlen(fav); i++) { //Makes input all lowercase to simplify cstring checking in switch statement
        fav[i] = tolower(fav[i]);
    }

    if (!strcmp(fav,"hiphop") || !strcmp(fav,"hip hop")) // Compare cstring 'fav' with lowercase music genre names and assign musicGenres type values to currFav
        currFav = HIPHOP;
    else if (!strcmp(fav,"rap"))
        currFav = RAP;
    else if (!strcmp(fav,"rock"))
        currFav = ROCK;
    else if (!strcmp(fav, "indie"))
        currFav = INDIE;
    else if (!strcmp(fav, "classical"))
        currFav = CLASSICAL;
    else if (!strcmp(fav, "alternative"))
        currFav = ALTERNATIVE;
}

void showFavMusic(musicGenres currFav) {
    char fav[20];
    switch (currFav)
    {
    case HIPHOP:
        strcpy(fav, "Hip Hop");
        break;
    case RAP:
        strcpy(fav, "Rap");
        break;
    case ROCK:
        strcpy(fav, "Rock");
        break;
    case INDIE:
        strcpy(fav, "Indie");
        break;
    case CLASSICAL:
        strcpy(fav, "Classical");
        break;
    case ALTERNATIVE:
        strcpy(fav, "Alternative");
    }

    cout << "Matt's favorite music genre today is " << fav << '!' << endl;
}
Matt Corby
  • 8,826
  • 3
  • 10
  • 16
  • I put cin.ignore(1000, '\n') between "Hey Matt!" and cin.getline which works, but I have to enter the first genre twice before it reads it. – Matt Corby Jan 06 '17 at 18:47
  • Please help I tried reading the article provided and other ones but I can't figure this out – Matt Corby Jan 06 '17 at 18:48
  • Okay I figured it out by making a new musicGenres type called NONE so I could check if it was the first entry or not. Then I could use an if statement to tell it to use cin.ignore() or not. – Matt Corby Jan 06 '17 at 18:59

0 Answers0