Problems With Calling A Function

  • Thread starter Thread starter NDiggity
  • Start date Start date
  • Tags Tags
    Function
Click For Summary
SUMMARY

The discussion centers on implementing a function in programming that presents a menu of six choices and handles user input effectively. The user initially struggled with calling the function without executing it multiple times. The solution provided involves storing the function's return value in a variable, specifically using the syntax menu = stockMenu();, which prevents redundant calls and allows for proper handling of the output. This approach ensures that the function is executed only once, streamlining the process of checking the user's choice.

PREREQUISITES
  • Understanding of basic programming concepts, specifically functions.
  • Familiarity with conditional statements in programming.
  • Knowledge of user input handling in programming languages.
  • Experience with variable assignment and return values in functions.
NEXT STEPS
  • Research best practices for function calls in programming languages like Python or Java.
  • Learn about error handling techniques for user input validation.
  • Explore how to create interactive menus in programming applications.
  • Study the concept of scope and lifetime of variables in functions.
USEFUL FOR

Beginner programmers, software developers, and anyone interested in improving their skills in function implementation and user input handling.

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)
count<<"You did not choose a stock"
else
count<<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 a lot!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K