I have to have a menu page (as a string menu, as below, which returns a string data type) if the user enters 1, 2, 3, 4 i should be returning that but as a string literal. and q or Q to quit the program but that should also be in the return type, please help me to fix this problem, i am getting an infinte loop
One way of converting a string to a number is with use of a stringstream object:
1 2 3 4 5 6 7 8 9 10 11
#include <string>
#include <sstream>
int main(int argc, char* argv[]) {
std::stringstream stream;
std::string string = "1234";
stream << string;
int i = stream.str();
stream.str("");//clear the stream string.
return 0;
}
Also, your infinite loop is due to a common thinking mistake.
On line 18, the conditions of your while loop don't do what you think they do. Essentially, the loop will execute when returnVal is not "1" or when it's not "2" or a bunch of other things. It can't be two things at once, so what you meant to use was && instead of ||.
TheIdeasMan means that you should consider replacing your long while-condition with a switch-statement.
Switch statements are conditional statements, so they evaluate the content of a variable against a number of cases.
The following example should show you enough about how they work to use them:
char menu_choice;
std::cout << "1. Press 1 for option 1.\n";
std::cout << "2. Press 2 for option 2.\n";
std::cout << "3. Press q to quit.\n";
std::cout << "Your choice: ";
std::cin >> menu_choice;
switch (menu_choice) //tell which variable we will evaluate
{
case'1' : //in case the variable == '1'
std::cout << "You chose option 1.";
break; //leave the switch statement
case'2' :
std::cout << "You chose option 2.";
break;
case'q' : //small or capital q
case'Q' :
std::cout << "You chose to quit.";
return 0; //to exit main() for instance
break;
default: //in any other case than the above
std::cout << "Sorry, your input was invalid.";
}
In order to loop the menu on invalid input, you'd have to keep a flag bool valid_option = false;, which you set to true if the option was good, or leave at false if it was not good, that way you can keep looping the menu while (valid_option == false).
This is what i did. I did not do xismn said, as i don't yet understand how that works... Can i enter that code in my string menu function?
My Menu function is not allowed to accept function calls (does not get function calls from main. And i have no idea how it works, i just don't want to blindly copy and paste it without understanding it. Thanks for your help guys this is amazing!
string Menu()
{
string fReturnVal, returnVal;
//returnVal = "errorMsg";
cout << "Special Purpose Calculator" << endl;
cout << "1) Average " << endl;
cout << "2) Factorial " << endl;
cout << "3) M to the Nth Power" << endl;
cout << "4) Range" << endl << endl;
cout << "Q) Quit "<< endl << endl;
cout << "What would you like to do? ";
cin >> returnVal;
while (returnVal != "1"&&returnVal != "2"&&returnVal != "3"&&returnVal != "4"&&returnVal != "q"&&returnVal !="Q")
{
cout << returnVal << "is an invalid selection. Try again: "; //IDHAR SE START KAR !!!
cin >> returnVal;
}
fReturnVal = returnVal;
return fReturnVal;
}
Note that Menu() now returns a char, and that I've used recursion (a function calling itself) to handle invalid input. The switch statement could also do more than return the value, it could actually call the appropriate function:
I can't change the string menu to char menu... (part of assignment)
Are you sure you are interpreting that correctly - what is the exact text of the assignment? The prompts you have in your code imply a single char as the input, I don't see why your teacher would force you all to use std::string
If you are indeed forced to use strings , use a series of if- elseif -else instead of the switch, and have the while loop work only on the boolean quit variable. That way you can avoid the horrible while condition you have at the moment.
NwN wrote:
Note that here Main() is a void function, because it doesn't need to return anything.
I am sure that is a typo, meant to be void Menu() . main is of course always int main()
@whitenite1
What you have proposed is explicitly what we are trying to get the OP NOT to do.