Trouble returning value with function

Hello everyone I am lost with functions. The only part I am stuck on is the functions. I can't seem to grasp the concept. Any help would be greatly appreciated!. Here is the goal of this exercise:

Write a program that prompts the user to enter two integer values. Display every whole number that falls between these values. (This is deceptively simple. Don't over think it! :))

Modify the program to use a function. The main () program should just prompt the user for the values and the function should display the results. (Do not use global variables.)

Here's the code I came up with to put into the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main(){

int num1, num2;

cout << "This Program displays the numbers between the numbers entered" << endl;

cout << "Enter a starting Number:  ";
cin >> num1;
cout << "Enter Your HIGHER Second Number: ";
cin >> num2;
cout << endl;
cout << "The numbers you Entered are: " << num1 << " & " << num2 << endl;

cout << "\nNow magically the numbers between " << num1 << " & " << num2 << " are: " << endl;
for (int i = num1; num1 <= num2; i++)
{
    cout << i << ", ";
    if (i == num2)
    {
    break;
    }
}
cout << endl;
system("pause");
return 0;
}


I know it's a little jacked up but I am still learning.

Here is my attempt at putting it in a function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int PleaseWork()
{
int num1, num2;
    for (int i = num1; num1 <= num2; i++)
    {
        cout << i << ", ";
            if (i == num2)
            {
                break;
            }
            return (i);
    }
}

int main(){

int number1, number2;

cout << "This Program displays the numbers between the numbers entered" << endl;

cout << "Enter a starting Number:  ";
cin >> number1;
cout << "Enter Your HIGHER Second Number: ";
cin >> number2;
cout << endl;
cout << "The numbers you Entered are: " << number1 << " & " << number2 << endl;

cout << "\nNow magically the numbers between " << number1 << " & " << number2 << " are: " << endl;
cout << PleaseWork(number1, number2) << endl;
cout << endl;

system("pause");
return 0;
}
I don't think you want to return anything from the function. I can't see anything in the task that specifies you need to return a value.

The problem here is that you want to pass two numbers into your function, but you're not doing so.

Your function should look something like this:
1
2
3
4
void PleaseWork(int a, int b)
{
   // Code here
}


Within the scope of your function, that is everything between the curly braces, a and b now represent the numbers passed in. So if you want to work out every number between those two, you need to do a for loop, similar to the oneyou have only starting at a + 1 and ending at b - 1.

Hope this helps.

EDIT: C++ is a soulless mistress. Naming your function PleaseWork won't persuade her to work for you. Might be better off giving it a more meaningful name. :-)
Last edited on
EDIT: C++ is a soulless mistress. Naming your function PleaseWork won't persuade her to work for you. Might be better off giving it a more meaningful name. :-)


LOL I sure will

Thanks for the input Hutch I think I can solve this one now! I had to step away for a minute and come back to it.
I had to step away for a minute and come back to it.

Sometimes that's the best approach. :-)
Topic archived. No new replies allowed.