Game Console Menu Class Problem

I need to make a class menu of a game console. There should appear three options in it that are:
1) Create a User (It should ask for the information of at least three users, Example, username, age and fav. color)
2) View all Users (It should display the info entered by the user)
3) Play a game (This option will display "option being developed")
4) Exit

I have 0 idea what I'm supposed to do now, a little bit of help would be thank.

this is my code until now:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//HEADER*******************************************
#include<iostream>
#include<string>
using namespace std;
#ifndef USER_H
#define USER_H
class USER{
public:
	USER(); //default constructor
	USER(string anewuser, string anewgender, string anewcolor); //overload constructor
	void setuser(string anewuser);
	void setgender(string anewgender);
	void setcolor(string anewcolor);

private:
	string newuser;
	string newgender;
	string newcolor;
};
#endif


//SOURCE******************************************* 
#include"USER.h"
USER::USER() {
	setuser("");
	setgender("");
	setcolor("");
}

USER::USER(string anewuser, string anewgender, string anewheight) {
	setuser(anewuser);
	setgender(anewgender);
	setcolor(anewheight);
}

void USER::setuser(string anewuser) {
	newuser = anewuser;
}

void USER::setgender(string anewgender) {
	newgender = anewgender;
}

void USER::setcolor(string anewcolor) {
	newcolor = anewcolor;
}

//MAIN*********************************************
#include<iostream>
#include<string>
#include"USER.h"
using namespace std;
void enterNewProfile();
void showUsers();
//Instancia de mi clase
USER user_profiles[100];
USER user_gender[100];
USER user_color[100];
int main() {
	string newuser, newgender, newheight;
	int option;
	USER something(newuser, newgender, newheight);
	do {
		cout << "::GAME CONSOLE::"
			<< "\n1) Create a User"
			<< "\n2) View All Users"
			<< "\n3) Play a Game"
			<< "\n4) Exit Game"
			<< "\n\n Select an option: ";
		cin >> option;
		switch (option) {

		case 1:
			system("cls");
			enterNewProfile();
			system("pause");
			system("cls");
			break;
		case 2:
			break;
		case 3:
			break;
		case 4:
			break;
		}
	} while (option != 1);
}

void enterNewProfile() {
	string usernameTemp;
	string usergenderTemp;
	string usercolorTemp;
	cout << "***Enter the user details***\n" << endl;
	for (int x = 0; x < 3; x++) {
		//setusername
		cout << "Enter the username:";
		cin >> usernameTemp;
		user_profiles[x].setuser(usernameTemp);

		//setgender
		cout << "Enter the gender: ";
		cin >> usergenderTemp;
		user_gender[x].setgender(usergenderTemp);

		//setPrefferedColor
		cout << "Enter the color: ";
		cin >> usercolorTemp;
		user_color[x].setcolor(usercolorTemp);
	}
}

//NEED TO FIX THIS	
void showUsers() {
	user_profiles
}

void displayAll() {
	//For loop y vas a llamar la funcion display de USER
	//userProfile[i].display();
}

Last edited on
Please post again using [code]/* your code here */[/code]
Last edited on
closed account (E0p9LyTq)
An old menu system I wrote several years ago, easy to add to a project and modify to suit your needs:

The heart of the menu engine (menu.hpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef __MENU_HPP__
#define __MENU_HPP__

#include <string>
#include <vector>

namespace MTK // (Menu Toolkit)
{
   unsigned short Menu(const std::vector<std::string>& stringArray);
}

inline unsigned short MTK::Menu(const std::vector<std::string>& stringArray)
{
   unsigned short response = 0;
   size_t         size     = stringArray.size() - 1;

   std::cout << stringArray[0] << '\n';

   while (response < 1 || response > size)
   {
      for (size_t i = 1; i <= size; i++)
      {
         std::cout << " " << i << ") " << stringArray[i] << '\n';
      }
      std::cout << ": ";
      std::cin >> response;
   }

   return response;
}

#endif 


The test chassis program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>

#include "menu.hpp"

int main()
{
   std::vector<std::string> example = { "Your choices are:", "Attack", "Retreat" };

   unsigned short choice = MTK::Menu(example);

   std::cout << "\nYou chose: " << example[choice] << '\n';
}

Your choices are:
 1) Attack
 2) Retreat
: 5
 1) Attack
 2) Retreat
: 1

You chose: Attack

The menu system has problems if the user enters anything other than a number. Checking and rejecting such an input is on my "to do" list.
The unhacky way to solve invalid input is to put the input into a string and use stoi (string to int), which will throw an exception.

If you hate exceptions you can use the Cstyle way of strtol, but this uses errno (it is not very pretty).

Otherwise you can do something like this:
https://en.cppreference.com/w/cpp/io/basic_istream/ignore

You can also use !(cin >> input) instead of checking failbit, but you need to clear and ignore like above to fix it.

Or something like this, and you have to fix it if you want to use it again.
https://en.cppreference.com/w/cpp/io/basic_ios/exceptions

The reason why I consider these hacky is because you need to fix cin every time.

Also that was completely off the context of OP's question, since OP already has a functional input system (at least I think).
OP, just create a function that prints the info for the USER class, and loop through it to print all the private strings.
Also don't use arrays, use std::list or vector, if you did, you wouldn't need to set every option, just call the constructor setting it all in one, and you don't need 3 lists for every property, just 1 since every user has all 3 properties. And you don't need to loop through a for loop 3 times to enter a user, and why do you check if the option is not equal to 1 when you should be checking if the option is not equal to 4... what are you thinking? Also always do ++x never x++, you will thank me later.
THANK YOU!! after I read about vectors, everything was super easier. FINALLY FINISHED IT
closed account (E0p9LyTq)
after I read about vectors, everything was super easier.

The C++ container libraries make writing programs less of a chore. :)

After pasting for you my old menu system, and mentioning it has problems when the user doesn't enter a number, I revised it. Still more than a bit hackish, using try/catch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// menu utility

#ifndef __MENU_HPP__
#define __MENU_HPP__

#include <string>
#include <vector>

namespace MTK // (Menu Toolkit)
{
   unsigned short Menu(const std::vector<std::string>& stringArray);
}

inline unsigned short MTK::Menu(const std::vector<std::string>& stringArray)
{
   unsigned short response = 0;
   size_t         size     = stringArray.size() - 1;
   std::string    input    = "";

   std::cout << stringArray[0] << '\n';

   while (response < 1 || response > size)
   {
      for (size_t i = 1; i <= size; i++)
      {
         std::cout << " " << i << ") " << stringArray[i] << '\n';
      }
      std::cout << ": ";

      std::getline(std::cin, input);

      try
      {
         response = std::stoi(input);
      }

      catch (...)
      {
         continue;
      }

   }
   return response;
}

#endif 


That is quite a lot of code to inline, but today's compilers see inline as a suggestion, not a requirement. I inlined the function so I didn't have to have a separate .cpp file.

I'm kinda lazy that way. :)
Your code creates the exact same result as before. No point in refactoring if you don't have an end result to extend its capabilities.

Cin will always return 0 if there is a blank or invalid conversion, which does exactly what you want in your circumstance.

The point of exceptions is to allow info to be given to the user, and if you don't like users seeing things like _M_copy_from_string you could just overload specific exceptions like std::invalid_argument and put in a plain "Invalid Argument: "<<input<<" (expected int)". Also make sure you print an error for your argument range as well.

Note that if you have any usage of cin inside your program, make sure you call cin.ignore before you call any getline. Cin will screw up getline.

But I don't blame you if you just don't care, this isn't something that is really important or complex, and it is trivial for a user to figure out how to enter a number.
Topic archived. No new replies allowed.