Reputation: 3
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
//Function Prototypes
void getInfo(string, double, int, int);
double getAverage(double, int);
void displayTable(string, double, int, int, double);
int main()
{
int i;
int size;
double average, sum;
size = 10;
double income[9];
int members[9];
string names[9];
getInfo(names[i], income[i], members[i], size);
getAverage(income, size, average, sum, i);
displayTable(names, income, members, size, average, i);
}
//getInfo function definition
void getInfo(string names[], double income[], int members[], int size, int i)
{
cout << "Please enter the name of the Head of Household, the total income, and the number of household members";
for (i = 0; i < 10; i++)
cin >> names[i] >> income[i] >> members[i];
}
//getAverage function definition
double getAverage(double income[], int size, double average, double sum, int i)
{
sum=0;
for (i = 0; i < 10; i++)
sum += income[i];
average = sum / size;
}
//displayTable function definition
void displayTable(string names[], double income[], int members[], int size, int i, double average)
{
cout << "Household Names" << setw(20) << "Annual Income" << setw(20) << "Household Members" << endl;
cout << "-------------------------------------------------------------------------------------------------------------------------" << endl;
for (i = 0; i < 10; i++)
cout << names[i] << setw(20) << setprecision(2) << income[i] << setw(20) << members[i] << endl;
cout << "Average Income" << setw(20) << average << endl;
}
Sorry this is such a messy/basic code. I'm just a beginner and I'm taking an online class out-of-state so I don't have access to help, other than the internet. I'm getting the following error messages: "getAverage function does not take 5 arguments" and "displayTable does not take 6 arguments". I'm using c++ and I've written the code in MS Visual Studio. Thanks to those who choose to help
Upvotes: 0
Views: 215
Reputation: 44329
I think your problem is that you have added the variable i to all definitions. That variable should be local like this:
void getInfo(string names[], double income[], int members[], int size) // Removed int i
{
int i; // Added int i
cout << "Please enter the name of the Head of Household, the total income, and the number of household members";
for (i = 0; i < 10; i++)
cin >> names[i] >> income[i] >> members[i];
}
Do the same for displayTable, i.e. don't pass the variable i but make it local.
EDIT: Looking more at your code I see that getAverage have more problems. It should be:
double getAverage(double income[], int size) // Remove double average, double sum, int i
{
double average; // Make them local variables
double sum;
int i;
sum=0;
for (i = 0; i < 10; i++)
sum += income[i];
average = sum / size;
return average; // Remember to return the result.
}
Notice the return statement. In c++ (and c) a function can return 0 or 1 values. The keyword void means 0 return values and you don't need the return statement. Any other keyword before a function means 1 return value and your code must do:
return valueOfCorrectType;
at the end of the function.
p.s. If a function needs to update several variables you'll have to pass references or pointers. But that's next step.
Upvotes: 0
Reputation: 853
void getInfo(string, double, int, int);
double getAverage(double, int);
void displayTable(string, double, int, int, double);
The prototypes should have the same arguments as your function definitions below, as in your main(), it assumes that you are calling the function declared in the protoype.
Should be instead:
double getAverage(double[], int, double, double, int);
void displayTable(string[], double[], int[], int, int, double);
Upvotes: 1