What is wrong?

What is wrong in this code ??

The compiler gives me "unsucssiful build"

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
#include <iostream>
using namespace std;

int x;

int main ()
{

for (int n=0; n<10; n++) 
	{
	cout << "Enter the customer age\n";
	cin >> x;
		if (x<18)
		cout << "No children";
		else if (x>=18 && x<=24)
		cout << "Junior discount";
		else if (x>24 && x<65)
		cout << "No discount";
		else (x>65)
		cout << "Seniur discount";
	}
return 0;
}


try use "else if (x>65)" instead of "else (x>65)"
It works now !!!

Thank you very much
closed account (z05DSL3A)
Yang
There would be a slight bug in doing that. The age 65 would not be caught by anything.

Wolf
In the code below, I have slightly reorganised the structure of the if…else if… to include a final else to act as a ‘default’ position.

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
#include <iostream>
using namespace std;

int x;

int main ()
{

    for (int n=0; n<10; n++) 
    {
        cout << "Enter the customer age\n";
        cin >> x;

        if (x<18)
       {
            cout << "No children";
        }
        else if (x>=18 && x<=24)
        {
            cout << "Junior discount";
        }
        else if (x>=65)
        {
            cout << "Seniur discount";
        }  
        else // Everything else handled here  
        { 
            cout << "No discount";
        }
    }
    return 0;
}

Last edited on
Thank you, a pretty good habit for coding.
Just letting you know you have "senior" misspelled. I assumed this was a homework assignment so I wanted to point it out.
Topic archived. No new replies allowed.