Can you guys give me some pointers with this code?

Need help with a problem. Can you guys give me some pointers with my code? The question is: The Bored Auto Company has done it again. Some models of their cars may be difficult to
drive because their wheels are not exactly round. Cars with model numbers 119, 179, 189
through 195, 221, and 780 have been found to have this defect. Write a program that allows
customers to enter the model number of their car to find out whether or not it is defective.
The program should terminate when a zero is entered as a model number. The program output
should look similar to:
Enter your model number (0 for done): 127
Your car is OK.
Enter your model number (0 for done): 221
Your car is defective. Please have it fixed.
Enter your model number (0 for done): 0
Program complete.

Everything works fine except for when I press 0 to end the program. It's supposed to only say "Program complete". Instead, it says "Your car is OK" AND "Program complete".

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

using namespace std;

int main()
{
    int model;

    do {
    cout << "Enter your model number (0 to quit): " << endl;
    cin >> model;


    if (model != 119 && model != 179 && model != 189 && model != 190 && model != 191 && model != 192 && model != 193 && model != 194 && model != 195 && model != 221 && model != 780)
        cout << "Your car is OK" << endl;

    else
        cout << "Your car is defective. Please have it fixed" << endl;


    }while (model != 0);
        cout << "Program complete" << endl;

    cin.get();
    cin.get();
    return 0;
}
http://xkcd.com/138/

The issue is that before you enter 0 to quit, you're still on line 11 waiting for input. Then the rest of the code runs before your loop condition does.

Try adding an if statement to break out of the loop if model == 0, and have the while condition just be "true" so it's infinite.
Last edited on
You could add a break; command inside your do-while loop.

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

using namespace std;

int main()
{
    int model;

    do {
		cout << "Enter your model number (0 to quit): " << endl;
		cin >> model;

		if (model == 0) break;   //Added code

		if (model != 119 && model != 179 && model != 189 && model != 190 &&
			model != 191 && model != 192 && model != 193 && model != 194 &&
			model != 195 && model != 221 && model != 780) 
			cout << "Your car is OK" << endl;
		else 
			cout << "Your car is defective. Please have it fixed" << endl;
    } while (model != 0);

    cout << "Program complete" << endl;

    cin.get();

    return 0;
}


The break; command stops whatever loop you're in and continues on the next line of code after the loop in a nut-shell.

Alternatively you could change to a while loop like this:
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
#include <iostream>

using namespace std;

int main()
{
    int model;

	// Added code:
	cout << "Enter your model number (0 to quit): " << endl;
	cin >> model;

	while (model != 0) {	// Changed to while loop.
		cout << "Enter your model number (0 to quit): " << endl;
		cin >> model;

		if (model == 0) break;

		if (model != 119 && model != 179 && model != 189 && model != 190 &&
			model != 191 && model != 192 && model != 193 && model != 194 &&
			model != 195 && model != 221 && model != 780) 
			cout << "Your car is OK" << endl;
		else 
			cout << "Your car is defective. Please have it fixed" << endl;
    }

    cout << "Program complete" << endl;

    cin.get();

    return 0;
}


The only problem with changing to a while loop is that you need to get some initial input outside the loop which is a bit redundant.

A debugging tip: Step through your code while examining a specific case. In this case, model = 0. Walking through the loop with the user trying to exit (model = 0) reveals your problem!

There are probably some other solutions as well =).
Last edited on
@recursive learning
My teacher actually told me to avoid using a break unless I absolutely have to. Thanks a lot though, that really helped me out.
<redacted>
Last edited on
> My teacher actually told me to avoid using a break unless I absolutely have to.

Yes, you have a good teacher.
Hopefully, you were also told to avoid using a continue (arguably, it is worse than a break).

Without the break:

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;

int main()
{
    int model;

    while( cout << "Enter your model number (0 to quit): " &&
            cin >> model && model != 0 )
    {
        if( model != 119 && model != 179 && model != 189 && model != 190 && model != 191 &&  model != 192 &&
            model != 193 && model != 194 && model != 195 && model != 221 && model != 780 )
                cout << "Your car is OK\n" ;

        else
                cout << "Your car is defective. Please have it fixed\n" ;
    }

    cout << "Program complete\n" ;
}


Presumably, you haven't learnt the switch-case construct as yet.
(That is one construct where use of break is the norm).


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

using namespace std;

int main()
{
    int model;

    while( cout << "Enter your model number (0 to quit): " &&
            cin >> model && model != 0 )
    {
        switch(model)
        {
            case 119:
            case 179:
            case 189:
            case 190:
            case 191:
            case 192:
            case 193:
            case 194:
            case 195:
            case 221:
            case 780:
                    cout << "Your car is defective. Please have it fixed\n" ;
                    break ;

            default:
                    cout << "Your car is OK\n" ;
        }
    }

    cout << "Program complete\n" ;
}
Last edited on
Topic archived. No new replies allowed.