Confused with functions

I am attempting to write a program which uses 2.cpp files and a header file. So i am writing a function to gather a bet(this is a simple bet program). I want the function to ask the user what they want to bet and to return the bet value. the function is below:
int getbet() {

int bet;
cout << "Enter the amount you would like to bet: " << endl;
cin >> bet;
return bet;

}
i have in the header file:
int getbet();

In the main file i call:
getbet();

The program works and will ask the bet amount. But when i try to see what the bet is within the main program it tells me 0. How do I keep the amount I selected for the bet within the function to stay within the main program??

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Are you assigning the return value of your function to anything when it is called?
If you have a function:

int get_num() {
  int num = 5;
  return num;
}

you still have to assign that value to something when you call the function:

#include <stdio.h>

int get_num() {
  int num = 5;
  return num;
}

int main() {
  int x = get_num();  // Assign the value of get_num to variable x
  printf("%d", x);
}
1 Like

Thank you so much… I kept playing with that but I was putting the function first and not the variable… meaning i was writing it get_num() = bet and I kept getting errors.

1 Like