Returning variables from different function

Hello,

I am trying to make a small program that gets the a value that a user inputs and then prints "Hello" that many times. I am trying to do it through different functions but I have been stuck on this for some time. Any feedback is appreciated.

Thanks!

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

#include <string>
#include <iostream>
#include <cmath>
using namespace std;


int main(int sum)
{
    int numO, numT=0;

    cout << "input two number" << endl;

    cin >> numO >> numT;
    cin.ignore();
    sum=numO+numT;
    return sum;

}

int hello()
{
    for(int i=1; i<main(sum); i++)
        cout << "Hello!" << endl;
        return 0;
}
return sum;
Returning from main terminates program.
What you want to do is to call hello function with an argument.
If you want to define some function after the main function you need to write function prototype before main.

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
#include <string>
#include <iostream>
#include <cmath>
using namespace std;

int hello(int); // function prototype

int main()
{
    int numO, numT, sum;
    cout << "input two number" << endl;
    cin >> numO >> numT;
    cin.ignore();
    sum=numO+numT;
    hello(sum); // call the function with an argument
    return 0;

}

int hello(int sum)
{
    for(int i = 1; i <= sum; i++) // we can use that argument in our function
    // or for(int i = 0; i < sum; i++) but not for(int = 1; i < sum; i++)
        cout << "Hello!" << endl;
    return 0;
}


http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Thank you very much!
Topic archived. No new replies allowed.