#include <iostream>
usingnamespace std;
void printCrap(int x, int age);
int main(){
// Creating a Sentinel Application - not knowing the amount of loops \
Ages, weights and using -1 to quit.
int age = 0;
int total = 0;
int people = 0;
void printCrap();
//cout << "Enter first persons age or -1 to quit: ";
//cin >> age;
while(age != -1){
total += age;
people++;
void printCrap();
// cout << "Enter first persons age or -1 to quit: ";
// cin >> age;
}
cout << "\n Number of people entered: " << people;
cout << "\n Average Age: " << (total/people) << endl;
return 0;
};
void printCrap(int x, int age){
cout << "Enter first persons age or -1 to quit: " << x ;
cin >> age;
}
I'll just assume that line 9 here is a typo when copy-pasting this from your IDE, instead of an actual forgotten comment that would result in compile errors anyway. Your issue here is the printCrap(int,int) function. Specifically, it requires two arguments to be passed two it- x and age. In your case, you pass neither to it (line 15). Rather, you are declaring it in main instead of calling it, due to the void specifier in front of it.
To actually have the code work, replace line 25 with:
printCrap(people,age);
Do the same with the part on line 25, and you should be golden.
#include <iostream>
usingnamespace std;
void printCrap(int x, int age);
int main(){
// Creating a Sentinel Application - not knowing the amount of loops \
Ages, weights and using -1 to quit.
// int age;
int total = 0;
int people = 0;
printCrap(x, age);
// cout << "Enter first persons age or -1 to quit: ";
// cin >> age;
while(age != -1){
total += age;
people++;
printCrap(x, age);
// cout << "Enter first persons age or -1 to quit: ";
// cin >> age;
}
cout << "\n Number of people entered: " << people;
cout << "\n Average Age: " << (total/people) << endl;
return 0;
};
void printCrap(int x, int age){
cout << "Enter first persons age or -1 to quit: " << x;
cin >> age;
}
I get a compile error saying that age isn't declared as I commented it out. Isn't it being declared in the function at the end of application? Also, it still doesn't run as supposed. Can someone copy and paste it into their IDE, run it and tell me whats wrong with it? The return values of the values should be x and age. I want them declared in the printCrap function and used throughout the program by calling them after prototyping them at start.
No, no. Don't comment out age. When it comes to functions, the arguments passed to them (int x, int age) are only inside the function itself. So yes, age is declared in the function printCrap, but it is not being declared in main.
#include <iostream>
usingnamespace std;
void printCrap(int x);
int main(){
printCrap(20); // insert parameter value for the function
return 0;
}
void printCrap(int x){
cout << "Favourite number is " << x << "!" << endl;
}
Look at the above code. It works and x isn't being declared after main.
#include <iostream>
usingnamespace std;
int main()
{
int age = 0;
int total = 0;
int people = 0;
cout << "Enter first person's age or -1 to quit: ";
cin >> age;
while (age != -1)
{
total += age;
people++;
cout << "Enter first person's age or -1 to quit: ";
cin >> age;
}
cout << "\nNumber of people entered: " << people;
cout << "\nAverage Age: " << (total/people) << endl;
return 0;
}
Really no need for a function for the sentinel program.
Speaking of which, why would someone use an Inline Function? I read the Function tutorials, I'll try to go over my program once again and see if I manage to make it work.