Hi,
If using a
char
inside your loop works, then yes, you could. I used
int
because my compiler refused an
std::string
in the switch statement, so I used an
int
because I knew it worked. Sorry for causing confusion with that.
choice=getch();
means that when the user press a key (the
getch()
part, it is stored in the variable
choice
. It's just like writing
choice=5
, or
choice=0
, but it takes the
return
value of the function
getch()
and puts it into the variable. Normally functions return an integer, or
true
/
false
; for an integer return value, you need an integer variable to hold it. Likewise, for a boolean return value (true/false, yes/no, on/off) you need a bool variable. That's the same for any function, so if you make a function and have it return a number, you can write
name of variable=name of function();
, and whatever the function returns will be stored in the variable.
Also, if I may elaborate as to why
not to use
goto
, it is because it makes code hard to read; imagine you make a program 1000 lines long, and when you read it through, you have to keep going back to search for the
goto
link you created earlier! It makes it harder to understand where the program is going. I use them occasionally when I'm really stuck as to how to do something, but they should be used as little as possible, preferably never. Instead, you should use loops well, to create the same desired effect, as they are far easier to understand as long as you lay your code out correctly, which it looks like you already do, having seen your code.
I hope I've helped you understand functions and getch() a little better.
Have a look at this article: I'm sure it explains this topic far better than I:
http://www.cplusplus.com/doc/tutorial/functions/
Please tell me if I've made a mistake.
Thanks,
hnefatl