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;
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;
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;
int sum (int user_input)
{
staticint 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;
}
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;
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?
#include <iostream>
usingnamespace std;
static sum (int user_input)
{
staticint 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++.
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.