Functions

Why do I get a blank screen?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>

using namespace std;

void message(int age);


int main(){

    void message(int);


    return 0;

}

void message(int age){
    cout << "Enter your age: ";
    cin >> age;
}
Your function main does nothing. It only contains a declaration of function message and a return statement.
How can I make the function get displayed in main?
change line 10 to

message(4);

or any other integer and remove 'void' from that line.

then we'll go from there :)
For example the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

void message();


int main(){

    message();


    return 0;

}

void message(){
    int age;
    cout << "Enter your age: ";
    cin >> age;
}



Or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int message();


int main(){

    cout << "You are " << message() << " years old." << endl;


    return 0;

}

int message(){
    int age;
    cout << "Enter your age: ";
    cin >> age;

    return age;
}


Last edited on
I want an input from the user not by myself.
vlad's solution is from the user.
Why is the average giving a wrong output? For example, I enter 10, 20 and 30 and the value given is 16 where it supposed to be 20.

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;

int message();


int main(){
    // Creating a Sentinel Application - not knowing the amount of loops \
        such as Age, Weights and using -1 to quit.

    message();

    return 0;

}

int message(){

    int age;
    int total = 0;
    int counter = 0;
    int average = 0;

    cout << "Enter your age or -1 to quit: ";
    cin >> age;

    while(age != -1){

        cout << "Enter your age or -1 to quit: ";
        cin >> age;

        total += age;
        counter++;
        average = (total / counter);

    }

    cout << "\n The total of people entered is " << counter << " people and their average is " << average << "." << endl;

    return average;
}

Your calculations correspond to the following expression

( 20 + 30 - 1 ) / 3

that gives 16.
Topic archived. No new replies allowed.