Problems With Calling A Function

  • Thread starter Thread starter NDiggity
  • Start date Start date
  • Tags Tags
    Function
AI Thread Summary
The discussion revolves around creating a function that presents a menu of choices and returns a selected number or -99 for invalid inputs. The user initially struggles with calling the function without executing it multiple times. A solution is found by storing the function's return value in a variable before checking it, thus avoiding redundant calls. The final approach simplifies the process and ensures the function only runs once. This highlights the importance of managing function calls effectively in programming.
NDiggity
Messages
53
Reaction score
0

Homework Statement


I have to write a function that first prints a list of 6 choices, tells a user to choose one of the six choices using the number 1-6, then either return the number they chose or -99 if they chose a number less than one or greater than 6. Then, I have to call this function and if they chose a number between 1 and 6, report this number, but if they chose outside of the range, print you did not choose a stock.

The Attempt at a Solution


I have the function done. I just don't know how to go about calling it. What I have now is:

if(stockMenu() ==-99)
cout<<"You did not choose a stock"
else
cout<<stockMenu()

Obviously you can see the problem with this way, as the function runs twice. How can I do this?
 
Last edited:
Physics news on Phys.org
menu = stockMenu();
 
Haha I can't believe I couldn't think of that. Thanks alot!
 

Similar threads

Replies
2
Views
3K
Replies
2
Views
2K
Replies
13
Views
3K
Replies
1
Views
2K
Replies
4
Views
5K
Replies
16
Views
11K
Replies
6
Views
3K
Back
Top