Sentinel Controlled Loop

Why do I get a blank screen whenever I try to run the below code? Is it something wrong with the function at the bottom I declared?

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
#include <iostream>

using namespace 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.
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

#include <iostream>

using namespace 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace 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.
X is being passed into the function as a parameter so it doesn't get created in the printCrap function. I would HIGHLY suggest you read up on functions.
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Over thinking this IMHO.
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 <iostream>

using namespace 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.
Last edited on by closed account z6A9GNh0
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.
Topic archived. No new replies allowed.