Please help in breaking into functions

I have created this program which inputs from user. The loop breaks if the user enters 0. The the program finds its total, Average and number of user inputs.

I want to break this program into atleast 2-3 functions. Please note I can't user arrays in the program. Can someone please help me? Thanks


#include <iostream>

using namespace std;


int main()
{

int input = 0;
int num = -1;
int total = 0;
int avg = 0;

do {

cout << " Input a number" <<" (Please enter 0 to stop) : ";
cin >> input;
num = num + 1;
total = total + input;
avg = total / num;

}
while (input != 0);

cout << "The total of numbers is : " << total << endl;
cout << "Average of Numbers is : " << avg << endl;
cout << "The total number enter are : " << num << endl;

return 0;
}

Okay, what do you want these two or three functions to do?

What have you tried?

Hey Jib, I want to either divide this program into separate functions for Sum, Average and Total number of inputs. I tried but cant succeed.

#include <iostream>

using namespace std;

int sum (int user_input)

{

int us_Total =0; // if i initialize here everytime with the loop it goes zero. if i don't give 0 value then it shows garbage value.
us_Total = us_Total + user_input;
cout << us_Total;
return 0;

}


//
//int average (int &sum_Total,int &total_num)
//{
//int avg = 0;
// avg = sum_Total/total_num;
// cout << avg;
//return avg;
//return 0;
//}

int main()
{

int input = 0;
int num = 0;

do {

cout << " Input a number" <<" (Please enter 0 to stop) : ";
cin >> input;
sum (input);
num = num + 1;


}
while (input != 0);

// cout << "The total of numbers is : " << sum(int &unser_input) << endl;
// cout << "The average is : " << int average (int& sum_Total,int& total_num) << endl;
cout << "The total number of inputs are are : " << num << endl;

return 0;
}

the word you seek is static.

1
2
3
4
5
6
7
8
9
int sum (int user_input)

{

static int us_Total =0; // static will retain its values (can be changed) for the duration of the program
us_Total += user_input;
cout << us_Total;
return us_Total;
}


Thank you so much for the help, This was exactly what I was looking for. Now to print the return in the main function I am using

Cout<<"Total:" <<sum (static us_Total);

and get the error Expected primary-expression before 'static'

Can you please help me how to print this return in the main function.

Thanks
roughly

main
{
for(something)
running_total += sum(inputz);
...
cout << running_total << endl;
}

you can also exploit your knowledge of the routine to get the current value via zero:

cout << sum(0) << endl;

to debug it just try this...

for(int i = 0; i < 10; i++)
sum(1);

cout << sum(0) << endl; //should be 10.
Last edited on
Cheers for this mate, This has solved alot of problems.

only the last bit left is how can I get the us_Total and the num in the average function and calculate average?

My program looks like this:

#include <iostream>

using namespace std;

static sum (int user_input)

{

static int us_Total = 0;
us_Total = us_Total + user_input;
return us_Total;

}

int average (int &us_Total,int &total_num)
{
int avg = 0;
avg = sum(0)/total_num;
cout <<avg;
return avg;
}

int main()
{


int input = 0;
int num = -1;

do {

cout << " Input a number" <<" (Please enter 0 to stop) : ";
cin >> input;
sum (input);
num = num + 1;


}
while (input != 0);

int avg(num);
// cout << "The average is : " << int average (int& sum_Total,int& total_num) << endl;
cout << "The total number are : " << num << endl;
cout << "The total number entered : " << sum (0) << endl;

return 0;
}

Am very close, have printed in the average function and it works. Buy when i return and print it in the main, it crashes my compiler for some reason. Is there anything wrong in the code below?

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
38
39
40
41
42
43
44
45
46
47
48

#include <iostream>

using namespace std;

static sum (int user_input)

{

static int us_Total = 0;
us_Total = us_Total + user_input;
return us_Total;

}

int average (int total_num)
{
int avg = 0;
avg = sum(0)/total_num;
return avg;
}

int main()
{


    int input = 0;
    int num = -1;

    do {

		cout << " Input a number" <<" (Please enter 0 to stop) : ";
		cin >> input;
        sum (input);
        num = num + 1;


		}
    while (input != 0);

     average(num);
    cout << "The average is : " << average (0) << endl;
    cout << "The total number of inputs are : " << num << endl;
    cout << "The total  is : " << sum (0) << endl;

return 0;
}
You're calling average(0) on line 42. That's setting total_num as 0, and making you divide by 0 in your average function. And your line 41 isn't doing anything, because you never use the result from it. I would delete line 41, and replace average(0) with average(num).

Also, you don't have a proper return type for your sum function. static, when used in a function signature, means access to that function is restricted to the file where it is declared. static is a very overused keyword in C++.

1
2
3
4
5
6
int sum (int user_input)
{
    static int us_Total = 0;
    us_Total = us_Total + user_input;
    return us_Total;
}
Last edited on
that may be my fault.

you can 'exploit' the sum function by calling it with zero because of what it does. Calling with zero adds zero which has no effect on the value, and then it returns the value which is unchanged by addition of zero. This is just using the knowledge of how your function works and what it does inside to get what you need from it.

Average does NOT work that way. However average does not change anything. Giving it the correct total_num each time you call it will return the same value until sum is called with something other than zero. You don't need to exploit or "do" anything to get the correct answer. You just need to call it with the correct current total num, and possibly put an "if total num == 0 return 0" or some other defense.

Topic archived. No new replies allowed.