sentinel controlled loop problem

Guys i need your help. When i enter sentinel value, why it still give output from else? Can you help me? Thank you.


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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
using namespace std;

int main()
{
	double engine, counter, payment, totalpayment;
	char category;
	counter=0;
	totalpayment=0;
	int sentinel='X';
	
	cout<<"Category		Year registered		Engine capacity (cc)	Roadtax	"<<endl;
	cout<<"   A			1995 or earlier		1000 or less		50.00	"<<endl;
	cout<<"   						1001 - 2000		70.00	"<<endl;
	cout<<"   						More than 2000		90.00	"<<endl;
	cout<<"   B			1996 or later		1500 or less		150.00	"<<endl;
	cout<<"   						More than 1500		300.00	"<<endl;
	
	do
	{
		totalpayment=totalpayment+payment;
		counter++;
		cout<<"\nEnter car category (A|B) or X to stop: ";
		cin>>category;
		
		if(category=='A')
		{
			cout<<"Enter engine capacity of the car: ";
			cin>>engine;
			if(engine<=1000)
			{
				payment=50.00;	
			}
			
			else if(engine>1000 && engine<=2000)
			{
				payment=70.00;	
			}
			
			else if(engine>2000)
			{
				payment=90.00;	
			}
			
			cout<<"\nCar category: "<<category<<endl;
			cout<<"Engine capacity of the car: "<<engine<<endl;
			cout<<"Amount of the road tax: "<<payment<<endl;	
		
		}
		
		else if(category=='B')
		{
			cout<<"Enter engine capacity of the car: ";
			cin>>engine;
			if(engine<=1500)
			{
				payment=150.00;	
			}
			
			else if(engine>1500)
			{
				payment=300.00;	
			}
			
			cout<<"\nCar category: "<<category<<endl;
			cout<<"Engine capacity of the car: "<<engine<<endl;
			cout<<"Amount of the road tax: "<<payment<<endl;	
		}
		
		else
		{
			payment=0;
			cout<<"Invalid category."<<endl;
		}
		
	}while(category!=sentinel);
	
		cout<<"\nTotal number of cars that have been processed: "<<counter-1<<endl;
		cout<<"Total of their payments: "<<totalpayment<<endl;
		
	return 0;
	
	
}
Last edited on
Give your whole code.

Put it in code tags.

It doesn't check the category against the sentinel until the loop has finished ... by which time it MUST have output results for either A or B or (the default) else. It is probably easiest to precede your first if in the loop by a check against the sentinel and break out of the loop at that point, rather than checking at the end.
Last edited on
already edited my post. can you check it for me?
On line 70 your else block is called when category is neither 'A' nor 'B'.
You need to check if category == sentinel
The quickest (but not the best) change would be to change line 70 from
else
to
else if ( category!=sentinel )

Since this means the test is written in two places it would be better to have a formulation like
1
2
3
4
5
6
7
8
do
{
   // prompt for, and enter, your category

   if ( category == sentinel ) break;

   // rest of loop
}



BTW, it doesn't make much sense to have category a char and sentinel an int.
Also, you should update totalpayment at the end of the loop, not the start, as payment wouldn't have been set on the first pass.
Last edited on
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
using namespace std;

int main()
{
    double engine, counter, payment{0}, totalpayment; // <--
    char category{'C'}; // <-- completely arbitrary initializer
    counter=0;
    totalpayment=0;
    //int sentinel='X'; // <--
    
    cout<<"Category        Year registered        Engine capacity (cc)    Roadtax    "<<endl;
    cout<<"   A            1995 or earlier        1000 or less        50.00    "<<endl;
    cout<<"                           1001 - 2000        70.00    "<<endl;
    cout<<"                           More than 2000        90.00    "<<endl;
    cout<<"   B            1996 or later        1500 or less        150.00    "<<endl;
    cout<<"                           More than 1500        300.00    "<<endl;
    
    while(category != 'X')
    {
        totalpayment=totalpayment+payment;
        counter++;
        cout<<"\nEnter car category (A|B) or X to stop: ";
        cin>>category;
        
        if(category=='A')
        {
            cout<<"Enter engine capacity of the car: ";
            cin>>engine;
            if(engine<=1000)
            {
                payment=50.00;
            }
            
            else if(engine>1000 && engine<=2000)
            {
                payment=70.00;
            }
            
            else if(engine>2000)
            {
                payment=90.00;
            }
            
            cout<<"\nCar category: "<<category<<endl;
            cout<<"Engine capacity of the car: "<<engine<<endl;
            cout<<"Amount of the road tax: "<<payment<<endl;
            
        }
        
        else if(category=='B')
        {
            cout<<"Enter engine capacity of the car: ";
            cin>>engine;
            if(engine<=1500)
            {
                payment=150.00;
            }
            
            else if(engine>1500)
            {
                payment=300.00;
            }
            
            cout<<"\nCar category: "<<category<<endl;
            cout<<"Engine capacity of the car: "<<engine<<endl;
            cout<<"Amount of the road tax: "<<payment<<endl;
        }
        
        else if(category == 'X') // <--
        {
            cout << "Exit\n";
            return 0;
        }
        
        else
        {
            payment=0;
            cout<<"Invalid category."<<endl;
        }
        
    }
    
    cout<<"\nTotal number of cars that have been processed: "<<counter-1<<endl;
    cout<<"Total of their payments: "<<totalpayment<<endl;
    
    return 0;
    
    
}
Topic archived. No new replies allowed.