Hi. I am very new to c++ but have some coding experience. As an exercise I tried to make a function that would use an enum variable to return an integer that could be used in a switch. Notwithstanding this a terribly messy way to do it (it was just an exercise), I encountered the following:
Not being able to pass the text input directly into the enum, I made an if loop to do it. This is inside a while(true) loop to make a 'error: try again' part.
What I have found is that the integer variable to be returned works inside the if conditions but reverts to the initialsed value 0 when I break out of the while loop.
The results should be that 'right' returns 0 and 'left' returns 1 but the returned value is always 0; even though in the if condition for 'left' it shows that the variable has been changed to 1 - as soon as the while loop breaks it goes back to 0. Also I have noticed that if I replace 'break;' in the left if-condition with 'return which_way;' does return 1 and the whole thing works properly. So I suppose i could put 'return which_way;' instead of break; in all the if-conditions but that seems like repeating the code (DRY). I have put the full code in below (sorry if that's overkill) and what I get running the program for 'left' look like (my comments with <-):
Initial user way = 0
Initial choice is :
Which way to go: >left
Choice is: left
User choice is passed to function.
In function:
which_way before WHILE : 0
You chose left - in WHILE which_way = 1 <- left returns 1 in if/while loop which I can return to main if I do it specifically
After WHILE which_way = 0 <- after while loop it's back to 0
which_way is returned to main
Your choice is: left, user_way = 0 <- choice
Process returned 0 (0x0) execution time : 4.437 s
Press any key to continue.
TIA - George the confused
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
|
#include <iostream>
// function to return integer from user choice right/left
int user_choice(std::string choice)
{
enum direction{right, left};
int which_way = 0;
std::cout << "In function:\nwhich_way before WHILE : " << which_way <<std:: endl;
while (true)
{
if (choice == "right")
{
direction which_way = right;
std::cout << "You chose right - in WHILE which_way = " << which_way << std::endl;
break;
}
else if (choice == "left")
{
direction which_way = left;
std::cout << "You chose left - in WHILE which_way = " << which_way << std::endl;
break; //This is where I can put return which_way; and it will work
}
else
{
std::cout << "Sorry, that is not a valid option. Try again.\n" << std::endl;
}
}
std::cout << "After WHILE which_way = " << which_way << std::endl << "which_way is returned to main\n" << std::endl;
return which_way;
}
int main()
{
// get user input (right/left) and pass to function to return integer (0/1)
int user_way = 0;
std::string choice = "";
std::cout << "Initial user way = " << user_way << std::endl;
std::cout << "Initial choice is : " << choice << std::endl;
std::cout << "Which way to go: >";
std::cin >> choice;
std::cout << "\nChoice is: " << choice << std::endl << "User choice is passed to function.\n" << std::endl;
user_way = user_choice(choice);
std::cout << std::endl << "Your choice is: " << choice << ", user_way = "<< user_way << std::endl;
return 0;
}
|