Convert Strings To Numbers

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

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
string menu ();
.
.
.

  string Menu()
{
	string fReturnVal, returnVal;

	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: "; \
		cin >> returnVal;
	}
fReturnVal = returnVal;
return fReturnVal
}
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 ||.
@Hasnain Attarwala

I personally hate dislike constructs like you have on line 18 (even if corrected logically). IMHO they are ugly, error prone and non-scalable.

Here is a much better way of doing a menu:

http://www.cplusplus.com/forum/beginner/99203/#msg534078


You can choose whether you use char or int in the switch, just don't use strings.

Good luck!

EDIT: char is better, so you can have a 'Q' option.
Last edited on
Could you explain how this works please?
closed account (o3hC5Di1)
Hi there,

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:

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
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).

All the best,
NwN
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;
}
closed account (o3hC5Di1)
Hi there,

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
char Menu()
{
char returnVal;


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;

switch (returnVal)
{
    case '1':
    case '2':
    case '3':
    case '4':
    case 'Q':
    case 'q':
        return returnVal;
        break;

    default:
        return Menu();
}


} 


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:

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
void Menu()
{
char returnVal;


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;

switch (returnVal)
{
    case '1':
        average();
        break;

    case '2':
        factorial();
        break;

//etc

    default:
        Menu();
}


} 


Note that here Main() Menu() is a void function, because it doesn't need to return anything.

Hope that helps.

All the best,
NwN
Last edited on
I can't change the string menu to char menu... (part of assignment)
@Hasnain Attarwala

Change all of the or symbols ( || ) here, to and symbols ( && ).

while (returnVal != "1"||returnVal != "2"||returnVal != "3"||returnVal != "4"||returnVal != "q"||returnVal !="Q")
Hasnain Attarwala wrote:
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- else if -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.



Last edited on
closed account (o3hC5Di1)
TheIdeasMan wrote:
I am sure that is a typo

Yes, my apologies, helping several people at the same time can confuse me sometimes.

Hasnain Attarwala wrote:
I can't change the string menu to char menu... (part of assignment)


Little trick:

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
std::string Menu()
{
char returnVal;

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;

switch (returnVal)
{
    case '1':
    case '2':
    case '3':
    case '4':
    case 'Q':
    case 'q':
        return std::string(1,returnVal);  //use std::string(size_t, char) constructor
        break;

    default:
        return Menu();
}


}


All the best,
NwN
@NwN

No need for apologies, I always have respect for what you have to say - cheers.
Topic archived. No new replies allowed.