-3

I'm trying to make a simple looping menu. I moved the member function mainMenu() above the main() class as the internet suggested

But when I try to call mainMenu() from inside a while loop, the compiler gives the following error, as noted in the comments:

'mainMenu' was not declared in this scope

Below is the header file. I've been messing with types trying to get the errors to go away, I don't know if this clears up anything. If mainMenu() isn't a member function, what is a member function?

Main.h

class main
{
    private:
    //variables
    bool quit;
    int userSelect;

    //constructor
    main();

    //methods
    mainMenu();
};

main.cpp

#include <iostream>

#include "Main.h"
using namespace std;

int main() {

bool quit = false;
int userSelect;

//mainMenu(); //** placing it here does not cause any errors **
while(!quit) {
    //Main Screen
    mainMenu(); // **actually I want it here, but I get the above error **
    cin >> userSelect;

    cin.ignore();
    switch (userSelect) {
        case '1':
            //moveInventory
            break;
        case '2':
            //viewInventory
            break;
        case '3': //user wants to exit
            //exit
            break;
        default:
            cout << "Command not recognized";
            mainMenu();
            break;
        }
    }

    return 0;
} //end of main

void mainMenu() {
    cout << "Welcome to the Simple Distribution Center.\n"
            "1) Move Inventory\n"
            "2) View Inventory\n"
            "3) Save and Exit" << endl;
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    There is no member function here. Or even a class. And what you claim is impossible. – Lightness Races in Orbit Aug 25 '15 at 18:13
  • It isn't a member function ... at least in the code you show. Please try to provide a *complete, minimal and verifiable* example. – Daniel Jour Aug 25 '15 at 18:15
  • _"freak out"_ like [in here](https://m.youtube.com/watch?v=h1qQ1SKNlgY0), or does it show some different behavior?? – πάντα ῥεῖ Aug 25 '15 at 18:19
  • @πάνταῥεῖ it seems the compiler outputting `mainMenu' was not declared in this scope` as in the comments constitutes `freaking out` - made that clearer with an edit to OP. Up to the OP to fix the misuse of `member` – Glenn Teitelbaum Aug 25 '15 at 18:23
  • I included the header file. Any hints on how to go about implementing the loop behavior I'm going for? I've been googling my brains out but its not much help. – Madeline O'Connell Aug 25 '15 at 18:58

1 Answers1

1

Do not use main as a class name, make member functions you want called public and use full signatures

userIO.h:

class userIO        // using `userIO` so its not confusing
{
private:
  // variables -- NONE

public:             // you need member functions public so they can be called
  enum choice { moveInventory=1, viewInventory=2, QUIT=3 }; // QUIT must be last

// constructor Not Needed

// static methods, since class has no data
  static void mainMenu();   // you need a full signature, *void*
  static choice userChoice();
};

And you have to tell the compiler your code belongs to your class using :: Note that all iostream is contained here, and its better to have using namespace std; isolated here as well

userIO.cpp:

#include <iostream>
#include "userIO.h"

using namespace std;

void userIO::mainMenu() {
    cout << "Welcome to the Simple Distribution Center.\n"
            "1) Move Inventory\n"
            "2) View Inventory\n"
            "3) Save and Exit" << endl;
}

userIO::choice userIO::userChoice() {
  int userSelect;

  while(1) {
    cin >> userSelect;
    cin.ignore();

    if ((userSelect < moveInventory) || (userSelect > QUIT)) {
      cout << "Command not recognized" << endl;
      mainMenu();  // Note no need for userIO:: since it's the same class
    } else {
      return choice(userSelect) ;
    }
  }
}

main.cpp:

#include "userIO.h"

int main() {
  bool quit = false;

  while(!quit) {
    userIO::mainMenu(); 
    switch (userIO::userChoice()) {
        case userIO::moveInventory :
            // code to move inventory here
            break;
        case userIO::viewInventory :
            // code to view inventory here
            break;
        case userIO::QUIT :
            quit=true;
            break;
        // NOTE: no default needed since only valid choices are returned
        }
    }

    return 0;
}
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
  • after making these changes, the constructor in the headers tells me "return type specification for constructor invalid" but changing the line to "int myMain()" doesnt help. Also, all of the calls to mainMenu() in the main() class warn "function 'mainMenu' could not be resolved'. Thank you so much for helping me! – Madeline O'Connell Aug 25 '15 at 19:25
  • What does OTHER do? right now it "cannot be resolved" Also, even if I change OTHER to, say -1, the switch line says "switch quantity is not an integer". Other than that though you have helped me so much!! Thank you! This code is beautiful – Madeline O'Connell Aug 25 '15 at 20:26
  • @MadelineO'Connell sorry fixed it - it was left over from a different idea I had. Also, its best not to comment by editting – Glenn Teitelbaum Aug 25 '15 at 20:29