Converting

Edit: Thanks. Figured it out. :)
Last edited on
"1" is a pointer to a string containing '1', '\0'. That pointer is never going to be the same as your local variables. You need to use strcmp here. A better option would be to use std::string. It has operator == overloaded to work as you expect.
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
#include <iostream>
#include <string>
using namespace std;
int main()
{

}
int printMenu()
{
	while (true)
	   {
		cout << "---------------------------------------------" << endl;
		cout << "Please choose a command" << endl;
		cout << "---------------------------------------------" << endl;
		cout << "0) Exit " << endl;
		cout << "1) Convert bin to decimal" << endl;
		cout << "2) Convert hex to decimal" << endl;
		cout << endl;
		
		char str;
	   cin >> str;
		if (str == '1')
			{
			return 1;
			}
		if (str == '2')
			{
			return 2;
			}		
		else
			{
			return 0;
			}
}
I need to define it like char str[maxInputLength];
hamsterman, how would I go about using strcmp?
Last edited on
1
2
3
4
if (strcmp(str, "1") == 0)
{
	return 1;
}
Last edited on
Topic archived. No new replies allowed.