Reputation: 63
Hello I work on a project for university which has to contain classes. I can't paste them here because it would take a lot to read hundreds of rows, but I have a class USER which works perfectly, but I also have a MainMenu() function written right before main(),which is the only one I call in main(), made with switch, which is supposed to redirect the console to classes' submenus or to show objects for every class, it depends on class.
Ok, from this MainMenu when I choose to go to Users List option, I want the console to show me the list of users, that means, all user class objects I created in main(). I thought of creating a new function with reference to class, but I don't know how to use it in this situation when I don't call it in main, and however i need to reffer to all objects in that class not to mention a specific object..
How can I do this because I only call that MainMenu() in main(), not write it here to be able to use the objects directly?
Be kind to me, I'm a beginner and I never dealt with this type of requisites. I would be grateful if you could help me solve this.
Have a nice day.
#include<iostream>
using namespace std;
class user{};
class B{};
class C{};
void MainMenu()
{
cout << " Main Menu" << endl;
cout << endl;
int chosenoption;
cout << "1.Users List" << endl;
cout << "2.Class B Submenu" << endl;
cout << "3.Class C submenu" << endl;
cout << "Type here the number of the option you want to choose: ";
cin >> chosenoption;
system("pause");
switch (chosenoption)
{
case 1:
system("cls");
cout << "There should be shown the list of users, here the only users i created in main are u1 and u2, and i need to make them and their attributes appear in this screen" << endl;
break;
case 2:
system("cls");
ClassBSubmenu();
break;
case 3:
system("cls");
ClassCSubMenu();
break;
default:
cout << endl;
cout << "Choose one of the available options only!" << endl;
cout << "Type here: " << endl;
}
}
void main()
{
User u1(...);
User u2(..);
}
ClassBSubmenu and ClassCSubmenu are functions created before MainMenu, but I haven't edited them too much
Upvotes: 0
Views: 85
Reputation: 63
You might want to have an array of Users which you pass to the MainMenu() function.
User u[] = { User(...), User(..) };
When you pass an array, you are actually passing a pointer to the first element in the array, so the Users could be modified in MainMenu() and the changes would exist in main().
void MainMenu( User users[] )
{
...
users[0]
MainMenu( u );
Upvotes: 1