-1

I've created a database using a switch statement and functions and I believe I've gotten it working but for whatever reason case B only works after the second try. It's probably a simple fix but any help is appreciated. Thank you.

#include <iostream> 
#include <string>

int main() {
    using namespace std;
    char getMenuChoice();
    void addAlbm(string alb[], int rls[], int& itemIndex);
    void lstAlbms(string alb[], int rls[], int size);

    string albms[10];
    int rleseYear[10], index = 0;
    do {
        switch (toupper(getMenuChoice())) {
        case 'A':
            addAlbm(albms, rleseYear, index);
            break;
        case 'B':
            lstAlbms(albms, rleseYear, index); //works after 2 tries
            break;
        case 'Q':
            cout << "\n""Goodbye" << endl;
            exit(0);
        default:
            cout << "\n""**Invalid choice**""\n" << endl;
        }
    } while (toupper(getMenuChoice()) != 'Q');
    return 0;}
void addAlbm(std::string alb[], int rls[], int &itemIndex) {
    using namespace std;
    string newAlbm;
    int newRleseYear;
    cout << "\n""Enter album: ";
    cin.ignore(50, '\n');
    getline(cin, newAlbm);
    cout << "\n""Enter year: ";
    cin >> newRleseYear; cout << "\n";
    alb[itemIndex] = newAlbm;
    rls[itemIndex] = newRleseYear;
    itemIndex++;}
void lstAlbms(std::string alb[], int rls[], int size) {
    using namespace std; cout << endl;
    for (int y = 0; y < size; y++)
        cout << alb[y] << "\t" << rls[y] << endl;
    cout << endl;}
char getMenuChoice() {
    using namespace std;
    char choice;
    cout << "--Main Menu--" << endl;
    cout << "\n""A. Add items" << endl;
    cout << "B. List items" << endl;
    cout << "Q. Quit" << endl;
    cout << "\n""Choose an option""_ _ _ _ _ _ _ ";
    cin >> choice;
    return choice;}
sans
  • 1
  • Please, check out an actual style guide and get an auto formatter of some form. – SuperStormer May 10 '21 at 02:25
  • 1
    Likely a duplicate of [this one](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). It talks about `getline`, but reading a `char` has the same problem, as it doesn't eat whitespace. If you add `cout << int(choice);` right after `cin >> choice;`, I predict it'd print 10, the ASCII code of `\n`, the line feed. – Igor Tandetnik May 10 '21 at 02:30
  • 2
    Very basic debugging techniques / investigative skills would solve your issue here. If you think the switch statement isn't working, then look at the actual value it's testing -- either view it in the debugger or print its integer value to the console. Then you'll see exactly what's happening and if you still don't understand why, at least you'll have some actual information with which to form a specific question. – paddy May 10 '21 at 02:49
  • Why include code for the other options in your [mre] when, according to the question, all one has to do is select the `B` option twice? (If you need to select the `A` option first, better mention that.) The less code you have to demonstrate the error, the easier debugging is. For that matter, why not reduce `lstAlbms()` down to a single output line to demonstrate that it was called? – JaMiT May 10 '21 at 03:32

0 Answers0