Do...while loop?

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.

How do I make it so it will repeat until you input 0?

This is what I have. Thanks!

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

int main()

{
    int M;
    const int GiveUp=0;


        cout<<"Enter your model number ("<<GiveUp<<" for done): ";
        cin>>M;

        if(M == GiveUp)
            cout<<"Program complete.";

        else if (M == 119 || M == 179)
            cout<<"Your car is defective. Please have it fixed.";

        else if (M >= 189 && M <= 195)
            cout<<"Your car is defective. Please have it fixed.";

        else if (M == 221 || M == 780)
            cout<<"Your car is defective. Please have it fixed.";

        else if(M>GiveUp)
            cout<<"Your car is OK.";

    return(0);
}
Last edited on
closed account (48T7M4Gy)
Use a while loop. while( m != 0) etc
closed account (48T7M4Gy)
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
#include <iostream>

int main()

{
    int M = 0;
    int GiveUp = 0;
    
    while (std::cout << "Enter your model number (" << GiveUp << " for done): " and std::cin >> M and M !=0)
    {
        if (M == 119 || M == 179)
            std::cout<<"Your car is defective. Please have it fixed.\n";
        
        else if (M >= 189 && M <= 195)
            std::cout<<"Your car is defective. Please have it fixed.\n";
        
        else if (M == 221 || M == 780)
            std::cout<<"Your car is defective. Please have it fixed.\n";
        
        else if(M >GiveUp)
            std::cout<<"Your car is OK.\n";
    }
    
    std::cout<<"Program complete.\n";
    
    return 0;
}
Enter your model number (0 for done): 780
Your car is defective. Please have it fixed.
Enter your model number (0 for done): 195
Your car is defective. Please have it fixed.
Enter your model number (0 for done): 194
Your car is defective. Please have it fixed.
Enter your model number (0 for done): 0
Program complete.
Program ended with exit code: 0
Thank you so much! I'm pretty new to this, I was having a lot of trouble. I can't seem to figure out while and do..while loops. Ugh. Thank you so much though!
Also, may I ask why you got rid of

 
using namespace std;


And put it before every cout and cin?
closed account (48T7M4Gy)
It's a long story and one you can google when u have spare time

There's nothing wrong with using namespace for exercises as it saves typing, but std:: etc makes it crystal clear where the particular function, cout etc is coming.
Ah, I see. I tried switching what you gave back to using namespace std;
But, now I'm getting an error and can't figure it out.. ):
That was the way I was taught, so I'm a little confused on what is going on.. haha
closed account (48T7M4Gy)
I understand, stick to what you are comfortable with but rest assured the code will work with using namespace std, so take your pick.

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

using namespace std;

int main()

{
    int M = 0;
    int GiveUp = 0;
    
    while (cout << "Enter your model number (" << GiveUp << " for done): " and cin >> M and M !=0)
    {
        if (M == 119 || M == 179)
            cout<<"Your car is defective. Please have it fixed.\n";
        
        else if (M >= 189 && M <= 195)
            cout<<"Your car is defective. Please have it fixed.\n";
        
        else if (M == 221 || M == 780)
            cout<<"Your car is defective. Please have it fixed.\n";
        
        else if(M >GiveUp)
            cout<<"Your car is OK.\n";
    }
    
    cout<<"Program complete.\n";
    
    return 0;
}


Enter your model number (0 for done): 119
Your car is defective. Please have it fixed.
Enter your model number (0 for done): 221
Your car is defective. Please have it fixed.
Enter your model number (0 for done): 190
Your car is defective. Please have it fixed.
Enter your model number (0 for done): 0
Program complete.
Program ended with exit code: 0
Thank you so much! You're a big help!
Topic archived. No new replies allowed.