Problems With Calling A Function

  • Thread starter NDiggity
  • Start date
  • Tags
    Function
In summary, the conversation is about a task to write a function that prints a list of 6 choices, prompts the user to choose one using numbers 1-6, and returns the chosen number or -99 if the input is invalid. The person has completed the function but is unsure how to call it without it running twice. The solution is to assign the function to a variable and then use that variable to call the function.
  • #1
NDiggity
54
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
  • #2
menu = stockMenu();
 
  • #3
Haha I can't believe I couldn't think of that. Thanks alot!
 

1. What is a function and why is it used?

A function is a block of code that can be called and executed multiple times in a program. It is used to perform a specific task or set of tasks, which helps to organize and structure the code and make it more efficient.

2. What are some common problems that can arise when calling a function?

Some common problems that can arise when calling a function include incorrect syntax, passing the wrong number or type of arguments, and not properly handling return values.

3. How can I troubleshoot problems with calling a function?

To troubleshoot problems with calling a function, you can check for errors in the function call, make sure the function is defined correctly, and ensure that the correct parameters are being passed in. You can also use debugging tools or print statements to track the flow of the code.

4. Can calling a function in a loop cause issues?

Yes, calling a function in a loop can cause issues if the function is not optimized or the loop is not properly structured. This can lead to performance issues or unexpected results.

5. How can I avoid problems with calling a function?

To avoid problems with calling a function, it is important to properly define and test the function before calling it, ensure that the correct arguments are being passed in, and handle any return values appropriately. It is also helpful to use descriptive function names and document the purpose and usage of the function for future reference.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
896
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
892
  • Engineering and Comp Sci Homework Help
Replies
2
Views
771
  • Engineering and Comp Sci Homework Help
Replies
7
Views
820
Back
Top