Please Help

I wrote this little if...else statement but dont know where I got it wrong



/* Question : Write your code to assign value -1 to 5 to the variable 'ageResult', depending on the value of variable 'age'.
int age : Age of a person
int ageResult: Store the resultant value in this variable */


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
 void detectAgeResult(int age, int &ageResult) {

    cin >> age;

    if (age < 0 || age > 101){
        cout << "-1";
    }
    else if (age >= 0 && age <= 5){
        cout << "0";
    }
    else if (age >= 6 && age <= 12){
        cout << "1";
    }
    else if (age >= 13 && age <= 19){
       cout <<  "2";
    }
    else if (age >= 20 && age <= 50){
       cout <<  "3";
    }
    else if (age >= 51 && age <= 60){
        cout << "4";
    }
    else if (age >= 61 && age <= 101){
        cout << "5";
    }
    else{
       cout << "Am not part of you";
    }
   return 0;
}
Last edited on
Store the resultant value in this variable
Why do do print the value when you are supposed to store it in the variable?
It should be like this:
1
2
3
4
5
6
7
8
9
if (age < 0 || age > 101)
{
   ageResult = -1;
}
else if (age >= 0 && age <= 5)
{
   ageResult = 0;
}
// and so on 
ageResult: Store the resultant value in this variable


Remove every cout and cin from your function.

Q: What is wrong here:
1
2
3
void foo() {
  return 42;
}

A: a mismatch; the function promises to return no value (void), yet it tries to return an integer (42).


1
2
3
4
5
6
7
if ( foo < 42 ) {
  // something
}
else  if ( // Q: What do we know at this point?
// A: We would not reach this else branch if foo were less than 42
// Therefore, we do not need to test whether 42<=foo, because it must be
)
Thanks so much.
Topic archived. No new replies allowed.