Assistance is appreciated

I am very new to programming and still don't totally understand how everything works, but I'm posting on this forums hoping I might find some help.

In the preceding lines of code I am attempting to create a menu where the user can type an input, and using if and else if statements, have different parts of the program run. An example would be if they were to type “Year” or “year” into the program I want the program to understand (Programs line of thought) “Oh! They typed Year that means they want to run the Leap year program.”
Thanks.

Note: When I run the program it gives me a run time error. I’m running this as a win32 console and I’m using Microsoft Visual Studio 2010 as my compiler.

Code:

#include <iostream>

using namespace std;

int main()
{
//Tried to declare it like this so my if statement would understand, it’s not working.
char mystr1[5] = "Year";
char mystr2[5] = "year";
char mystr3[5] = "Odd";
char mystr4[5] = "odd";
char mystr5[5] = "Even";
char mystr6[5] = "even";
char MenuOption[5];

//My Menu displayed when the program is first run
cout << " -------"<< endl;
cout << "| MENU |" << endl;
cout << " ------ " << endl;
cout << " Type (Year) if you want to find out if a particular year is a leap year." << endl;
cout << " Type (Odd) or (Even) to run the odd/even program." << endl;
cout << " Input anything else to exit the program." << endl;
cin >> MenuOption[5];

// If they type Year or year run this section of code
if (MenuOption[5] == mystr1[5] || mystr2[5])
{
cout << "Leap Year Program" << endl;
cout << "--------------------------------------" << endl;
cout << "Enter the year that you wish to find out if it is a leap year or not." << endl;
return main();

}

//If they type Odd, odd, Even, even run this section of code
else if (MenuOption[5] == mystr3[5] || mystr4[5] || mystr5[5] || mystr6[5])
{
cout << "Even/Odd Program" << endl;
cout << "--------------------------------------"<< endl;
cout << "Enter a number to find the other" << endl;
return main();
}
//If they don't type any of the above selections, print this message and return 0
//to main fuction to end the program.
else if (MenuOption[5] != mystr1[5] || mystr2[5] || mystr3[5] || mystr4[5] || mystr5[5] || mystr6[5])
{
cout << "Thanks for using this program." << endl;
return 0;
}

return 0;
}



You have multiple logic errors.

1. You can't compare character array strings like that, because character arrays are character pointers, and so you're comparing two memory addresses instead of two sets of memory. Try using std::string so you can do MyStringA == "Hello!" and the likes.
http://www.cplusplus.com/reference/string/string/

2. Doing if(a == b || c) doesn't do what you think it does. In C++, this means "if the expression 'a == b' is true OR if the expression 'c' is true"; anything that is 'not zero' is considered true, so if c is not 0, then the if statement is true. To fix, do: if(a == b || a == c)

3. Array indexes start at zero, not one. So an array of 5 elements has indexes zero to four. This means that an index of five, like you have just about everywhere in your program, is going to be random other memory that probably won't want to be changed or accessed.

4. Never call main()! Just don't! Instead, using a looping structure like do-while to repeat your program at the user's request.

5. MenuOption should be an int, not a character array.

6. Fix these problems and repost your code again if you have problems, but make sure you put it in code tags like this:
[code]
int main()
{
    cout << "Hello, world!" << endl;
}
[/code]
and it turns into this:
1
2
3
4
int main()
{
    cout << "Hello, world!" << endl;
}
Last edited on
Thanks for the quick reply; I’m still having a problem understand this I guess. I went ahead and changed all of my “mystr1 – mystr6” to strings. Then I input those strings like you stated in answer 2 and am still having no luck. If I understand correctly the problem is when I use things like == or || it only works with numbers. This may be asking a lot, but is there any way you could give me an example of how I can integrate strings and numbers. I looked at that link you put up about strings and played around with the compare string command, but I still don’t seem to be getting any headway.

I want to again clarify what I’m attempting to do. When the User of this program types, ‘Year’ or ‘year’ I want this first if statement to be run; When the user types ‘Odd’ or ‘odd’ or ‘Even’ or ‘even’ I want the second else if statement to run. Lastly, the third else if statements is there to close the program. So I’m trying to get an input of text, have the computer look at that text, see if it matches any of my strings, and do the appropriate command.

I have a lot of code, I would like to emphasize this first If statement if we could, if I could get that to run, I’m sure I could get the rest of the program to run.

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
#include <iostream>
#include <string>

using namespace std; 

int main()
{
	//Tried to declare it like this so my if statment would understand, its not working.
	std:: string mystr1 ("Year");
	std:: string mystr2 ("year");
	std:: string mystr3 ("Odd");
	std:: string mystr4 ("odd");
	std:: string mystr5 ("Even");
	std:: string mystr6 ("even");
	int MenuOption;

	// I don't know how to integrate numbers and strings
	mystr1 = 1;
	mystr2 = 2;

	//My Menu displayed when the program is first run
	cout << " -------"<< endl;
	cout << "| MENU |" << endl;
	cout << " ------ " << endl;
	cout << " Type (Year) if you want to find out if a particular year is a leap year." << endl;
	cout << " Type (Odd) or (Even) to run the odd/even program." << endl;
	cout << " Input anything else to exit the program." << endl;
	cin >> MenuOption;
	
	// If they type Year or year run this section of code
	if (MenuOption == mystr1 || MenuOption == mystr2)
	{
		cout << "Leap Year Program" << endl;
		cout << "--------------------------------------" << endl;
		cout << "Enter the year that you wish to find out if it is a leap year or not." << endl;
		

	}

	//I tried to make MenuOption a string and compare it to the other strings, didn't work. I left this so you might have an idea of what I've trie . 
	else if (MenuOption.compare(mystr3) != 0 || MenuOption.compare(mystr4) != 0 || MenuOption.compare(mystr5) != 0 || MenuOption.compare(mystr6) != 0)
	{
		cout << "Even/Odd Program" << endl;
		cout << "--------------------------------------"<< endl;
		cout << "Enter a number to find the other" << endl;
		
	}
	//If they don't type any of the above selections, print this message
	else if (MenuOption != 1)
	{
		cout << "Thanks for using this program." << endl;
		return 0;
	}

	return 0;
}



Thanks again for the help!
1. To integrate strings and numbers, you'll want to convert between the two. C++ has a built-in way to do this with, shockingly, another class: stringstream. Just like cin and cout, it works with both trings and numbers:
1
2
3
4
5
6
7
8
9
10
11
12
13
int StrToNum(const std::string &from)
{
    std::istringstream ss (from); //make a stringstream from the string
    int x; //make an integer
    ss >> x; //take the integer out
    return x;
}
std::string NumToStr(int x)
{
    std::ostringstream ss; //make a stringstream
    ss << x; //put the number into it
    return ss.str(); //convert it to a string
}


2. On line 31, Why not just do if(MenuOption == "Year" || MenuOption == "year")? std::string is smart and lets you compare it directly like this, which is nice :)

3. I hope this helps you solve the rest... ;)
Last edited on
Instead of having a bunch of variables that may or may not be used (all of your strings), you could create this program using just one variable. Two basic ways to do this are:

1) The int way:
1
2
3
4
int choice;
// Remember that in c++, "text" << endl; is the same as "text\n";.
cout << "Menu:\n\n1) Option 1\n2) Option 2 3) Option 3\n\n> ";
cin >> choice;

This way, you can use if/elseif/else statements to check what they entered:
1
2
3
4
5
6
7
8
9
10
if(choice == 1){
    cout << "Welcome to choice 1";
    // ...
} else if(choice == 3){
    cout << "Choice 3 code here";
} else {
    // Error code usually goes in else:
    cout << "\nInvalid choice. Exiting program.";
    return 0;
}

or, to improve readability, use a switch(case):
1
2
3
4
5
6
7
8
9
10
11
12
13
switch(choice)
{
    case 1:
        cout << "Welcome to choice 1";
        break;
    case 3:
        cout << "Choice 3 code here";
        break;
    default: // This is your 'else' section
        cout << "\nInvalid choice. Exiting program.";
        return 0;
        break;
}


2) The string way:
1
2
3
string choice;
cout << "What would you like to see?\n\nTime\nDate\nSeason\n> ";
cin >> choice;

Then, again using if/elseif/else statements, you can check what the user inputted and perform the correct code:
1
2
3
if(choice == "Time" || choice == "time"){
    cout << "\nThe time is 05:25PM";
}

The problem here is that if the user were to input "tIme" or "SeASOn" for whatever reason, they wouldn't be directed to the right code as it doesn't match any of the options. You can't tackle this problem without the use of loops etc, but to get the program to work on a basic level, that doesn't matter.


EDIT: For future reference, if you use using namespace std;, you don't need the std:: here:
std:: string mystr2 ("Year");
Last edited on
Thanks to the replys, I managed to get my menu working ! This is a different program than i wrote before, but it shows my completed menu.

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

#include <iostream>
#include <string>


using namespace std;

	double temp;
	double celbase;
	double fahtemp;
	string choice;
	string choice2;

int main()
{
	do{

	cout << "Temperature Conversions" << endl;
	cout << "Type (C) or (Celsius) if you want to run the Celsius --> Fahrenheit." << endl;
	cout << "Type (F) or (Fahrenheit) if you want to run the Fahrenheit --> Celsius." << endl;
	cout << "Type (E) or (Exit) if you want to end the program." << endl;
	cin >> choice;

	if (choice == "C" || choice == "c" || choice == "Celsius" || choice == "celsius")
	{
		cout << "Celsius --> Fahrenheit"<< endl;
		cout << "Enter your temperature in celsius: ";
		cin >> celbase;

		temp = 32 + 1.8*(celbase);

		cout << "Your Temperatue in fahrenheit: " << temp << " degrees fahrenheit" << endl;
	}
		else if(choice == "F" || choice == "f" || choice == "Fahrenheit")
		{
			cout << "Fahrenheit --> Celsius" << endl;
			cout <<"Enter your temperatue in fahrenheit: ";
			cin >> fahtemp;

			temp = (fahtemp - 32) / 1.8;

			cout << "Your Temperature in celsius: " << temp << " degrees celsius" << endl;
		}
		else if(choice == "exit" || choice == "Exit"|| choice == "e" || choice == "E")
		{
			return 0;
		}
			else
			{ 
			cout << "Invalied selection, would you like to restart the program and try again?\n";
			cout << "Please type (Yes) or (No): ";
			cin >> choice2;
			cout << endl;

			}
	} while (choice2 == "Yes" || choice2 == "yes");
	

	return 0;
}
Topic archived. No new replies allowed.