| JRimmer (6) | |
|
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; } | |
|
|
|
| L B (2737) | |||
|
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:
| |||
|
Last edited on
|
|||
| JRimmer (6) | |||
|
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.
Thanks again for the help! | |||
|
|
|||
| L B (2737) | |||
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:
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
|
|||
| WhiteWind (84) | |||||||||||
|
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:
This way, you can use if/elseif/else statements to check what they entered:
or, to improve readability, use a switch(case):
2) The string way:
Then, again using if/elseif/else statements, you can check what the user inputted and perform the correct code:
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
|
|||||||||||
| JRimmer (6) | |||
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.
| |||
|
|
|||