-1

(I'm just a guy trying to start programming so don't be too harsh, English is not my main language too) I was trying to make a simple program about storing book into a library (with class since I started learning them) and the array won't show up in a different case other than the one in which I initialized it, the IDE doesn't show up any error too

Code:

#include <iostream>
using namespace std;

class MainMenu {
public:
  int choice, ID, IDC;
  string Name[500];
  string Genre[500];
  string Book[500];

  void Menu() {
    cout << "choose an action:" << endl;
    cout << "1)\tadd a book\n2)\tsee my book" << endl;
    cin >> choice;
    switch (choice) {
    case 1:
      cout << "ID:" << endl;
      cin >> ID;
      cout << "name:" << endl;
      cin >> Name[ID];
      cout << "Genre" << endl;
      cin >> Genre[ID];
      cout << "> " << endl;
      cin >> Book[ID];
      cout << "<" << endl;
      cout << "book successfully registered" << endl;
      // assign a value about the book (name and genre) in each array stored
      // with the id
      break;
    case 2:
      cout << "ID?" << endl;
      cin >> IDC; // IDC stands for ID Check, if ID == IDC then it should show
                  // the specs of the book + its content
      cout << Name[IDC] << endl
           << Genre[IDC] << endl
           << "--> " << Book[IDC] << endl
           << endl;
      break;
    } // Switch
  }   // Menu
};    // Class

int main() {
  int i = 0;
  while (i == 0) // creating a loop to keep showing the menu without the end of
                 // the program
  {
    MainMenu giorgio;
    giorgio.Menu(); // summoning menu
  }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    What is the input? – 273K Aug 14 '22 at 16:58
  • Have you tried running your code line-by-line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Aug 14 '22 at 17:00
  • 1
    You create a different `MainMenu` each time you run the loop. The new `MainMenu` has no idea about any changes made in the already dead `MainMenu` – Yksisarvinen Aug 14 '22 at 17:00
  • 1
    Move `MainMenu giorgio;` outside, and above, the `while` loop in `main`. – WhozCraig Aug 14 '22 at 17:00

1 Answers1

0

When you create a Object, all atributes are initialized, as a result, you create a emptiy arrays of your Object for each iteration, you can resolve this whit this code:

#include <iostream>
using namespace std;

class MainMenu {
public:
int choice, ID, IDC;
string Name[500];
string Genre[500];
string Book[500];

void Menu() {
  cout << "choose an action:" << endl;
  cout << "1)\tadd a book\n2)\tsee my book" << endl;
  cin >> choice;
  switch (choice) {
  case 1:
    cout << "ID:" << endl;
    cin >> ID;
    cout << "name:" << endl;
    cin >> Name[ID];
    cout << "Genre" << endl;
    cin >> Genre[ID];
    cout << "> " << endl;
    cin >> Book[ID];
    cout << "<" << endl;
    cout << "book succesfully registered" << endl;
    break;
  case 2:
    cout << "ID?" << endl;
    cin >> IDC; 
    cout << Name[IDC] << endl
       << Genre[IDC] << endl
       << "--> " << Book[IDC] << endl
       << endl;
    break;
    }
  }
};

  int main() {
  int i = 0;
  MainMenu giorgio;
  while (i == 0)
  {
    giorgio.Menu(); // summoning menu
  }
}