Using Char For Menu Selection

Hi everyone. I'm basically making a "logger" program that takes data input by the user and creates a log of it. The program displays a menu to the user like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void menu()
{
	int decision=0;

	cout << "Menu: " << endl;
	cout << "\tOption 1: View your data log" << endl;
	cout << "\tOption 2: Append your data log" << endl;
	cout << "\tOption 3: Quit the program" << endl;
	cout << "Option: ";
	cin >> decision;

	if (decision == 1)
		fileReader();
	else if (decision == 2)
		eventLogger();
	else
		quit(); //thank user for using program, exit;
}//menu 


That part works. Let's say the user inputs 2 so we jump to eventLogger(). The program is for a game, so it asks the user if they want to count kills. This is where it doesn't work.

I'm not sure how to correctly read/identify characters. I want the program to be able to recognize if a character isn't Y for yes and isn't N for no. It should also be able to recognize if it is Y and if it is N.

Finally, I'd also like to be able to use a while loop for while a character isn't Q, so the user has an easy way to break the loop and get back to a menu without being prompted every time.

Here's my code for eventLogger():

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
void eventLogger()
{
	char value[800];
	char killCount;
	char choice[10];
	char drops[600];
	char monster[200];
	char item[200];

	int kill=0;



	

	time_t rawtime;
	struct tm * timeinfo;
	char timeData[100];

	output.open("log.txt");

	cout << "Enter Q at any time to return to menu." << endl;
	cout << "You may enter any data in any format you wish.  Date/Time is automatic." << endl;
	cout << endl;
	cout << "Would you like the program to count kills? (Y/N): ";
	cin >> killCount;

	if (killCount != 'Y' && killCount != 'N')
	{
		cout << "Invalid choice." << endl;
		eventLogger();
	}//if invalid*/

	else if (killCount == 'Y')
	{
		kill=0;
		while (monster != 'Q' && drops != 'Q')
		{
			time ( &rawtime );
			timeinfo = localtime ( &rawtime );

			strftime (timeData,100,"%I:%M:%S%p",timeinfo);
			output << "    " << timeData;

			cout << "Monster Name: ";
			cin >> monster;
			output << "   " << monster;
			kill++;
			output << "   Kill #" << kill;

			cout << "Drops: ";
			cin >> drops;
			output << "       " << drops;
			cout << "\tData has been logged." << endl;
		}//while
	}//else if killCount = yes

	else if (killCount == 'N')
	{
		cout << "Are you logging Monster drops or Regular items? (Monster/Regular): ";
		cin >> choice;

		if (choice == 'Monster')
		{
			cout << "Monster Name: ";
			cin >> monster;
			output << monster;

			while (drops != 'Q')
			{
				time ( &rawtime );
				timeinfo = localtime ( &rawtime );

				strftime (timeData,100,"%I:%M:%S%p",timeinfo);
				output << "    " << timeData;

				cout << "Drops: ";
				cin >> drops;
				output << "       " << drops;
				cout << "Data has been logged." << endl;
			}//while
		}//if monster
		
		else if (choice == 'Regular')
		{
			while (value != 'Q')
			{
				time ( &rawtime );
				timeinfo = localtime ( &rawtime );

				strftime (timeData,100,"%I:%M:%S%p",timeinfo);
				output << "    " << timeData;

				cout << "Item Name: ";
				cin >> item;
				output << "   Item:" << item;
				cout << "Value: ";
				cin >> value;
				output << "      Value: " << value;
				cout << "Data has been logged." << endl;
			}//while
		}//else if regular
	}//else if kill count = no

	/*else
	{
		cout << "Invalid choice." << endl;
		eventLogger();
	}*/

	cout << "Data log complete.  Returning to menu." << endl;
	separator();
	menu();
}//eventLogger 


These are the types of errors I get:


1>c:\users\kingtrunks\documents\runescape\runescapelogger\runescapelogger.cpp(127) : error C2446: '!=' : no conversion from 'int' to 'char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\kingtrunks\documents\runescape\runescapelogger\runescapelogger.cpp(127) : error C2040: '!=' : 'char [200]' differs in levels of indirection from 'int'
1>c:\users\kingtrunks\documents\runescape\runescapelogger\runescapelogger.cpp(127) : error C2446: '!=' : no conversion from 'int' to 'char *'


I've looked up string comparisons, how to identify letters in Strings, and how to use while loops with chars, but haven't been able to find anything to fix my problem. I usually code in Java, so I'm having a little trouble with C++ it seems as far as chars/Strings go.

Any help would be appreciated. I guess I mostly just need to know how to go about finding out if the char is Q, is not Q, etc.

Thanks,

-KT
Last edited on
From the looks of the errors, you are comparing an int to a array of characters.
But the problem is not in the code you posted
It would seem that way, yes, but I seem to get the error when I use the != operator to compare an array of characters. The first error I receive is on line 127 of the overall code, which translates to line 37 of the second set of code I posted:

 
while (monster != 'Q' && drops != 'Q')


I do not receive the error when using the != operator with a regular char. I believe I got the program running at one point and when I tried to debug it, it would not read the killCount value correctly at all. I overhauled the entire function and that's when I got flagged for a bunch of errors. I was able to correct most of them, but I really am having trouble finding information on string/char matching. I've not used arrays of any sort before, so I'm just going based on what I've seen.

Thanks for the response!

-KT
if monster and drops are std::strings then try using double quotes.
if they are chars use single quotes.

while (monster != "Q" && drops != "Q")

Also using structures such as this can put a strain on the system (if not at the least hard to read and debug), there's a much easier way to do what you are trying to achieve and that is by using a loop:

27
28
29
30
31
32
33
34
cout << "Would you like the program to count kills? (Y/N): ";
	cin >> killCount;

	if (killCount != 'Y' && killCount != 'N')
	{
		cout << "Invalid choice." << endl;
		eventLogger();
	}//if invalid*/ 


27
28
29
30
31
32
33
34
do {
   cout << "Would you like the program to count kills? (Y/N): ";
   cin >> killCount;

   if (killCount != 'Y' && killCount != 'N')
      cout << "Invalid choice. Try again..." << endl;
 
} while (killCount != 'Y' && killCount != 'N') ;


edit: is there any reason why your using char* instead of std::string ? the string class is much easier to manage I would suggest changing to it.

edit2: line 20, is output a global ofstream variable?

edit3: if you must use char arrays there are some functions in the <cstring> library which could help: http://www.cplusplus.com/reference/clibrary/cstring/memcmp/
Last edited on
You can't do:
if (str1 != "String")
because C++ isn't able to compare strings like that, or even with ==.

You would need to use a function like strcmp(), or string compare, contained in the string.h header. Here's some information on string handling.
http://www.cprogramming.com/tutorial/lesson9.html.

Also, File I/O (input/output), if you need it.
http://www.cprogramming.com/tutorial/lesson10.html.
Last edited on
@yottaflop,
He can, actually. The std::string class has overloaded those operators; so in C++ he can.

Besides that, because you've put full stops ("periods") after those URLs, they produce 404s and you have to remove the full stop and reload the page.
If he's using std:string he can... but it looks like he's using char arrays, so he'd have to use strcmp.
Oh yeah; so he is. My mistake.
Thanks guys. The double quotes/single quotes were my initial error. However, I did not know how to use strings in C++. I have "using namespace std" in my code. I thought it was pretty odd that Visual Studio didn't light the words "String" or "string" up when I typed them in, so I just assumed char arrays had to be used. It wasn't until today that I finally got the hang of using strings (thanks to a file reading assignment for my C++ class), so I'm sure I'll get the issue resolved once I get back to the program I posted on here.

Thanks again everyone, I appreciate all the helpful posts =)

-KT
Last edited on
Topic archived. No new replies allowed.