-1

When I enter the string "NOMORE" it continues to the next line where it asks me for the vehicle speed. It only stops the loops when it finishes the for loop. How can I make it so that it stops immediately when "NOMORE" is entered? Thanks for any help. I truly appreciate it. And sorry for wasting your time if I did :(

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    string plate;
    int totalCount;
    int ticketCount = 0;
    double speed;
    double base = 150;
    double limit;
    double ticket;
    double overspeed;

    for (totalCount = 0; plate != "NOMORE"; totalCount++)
    {
        cout << "Enter a license plate number  --> ";
        cin >> plate;
        cout << "Enter current vehicle's speed --> ";
        cin >> speed;
        cout << "Enter speed limit in the zone --> ";
        cin >> limit;

        overspeed = speed - limit;

        if (overspeed >= 5 && overspeed <= 20)
        {
            ticket = base + 5 * overspeed;
            cout << "A ticket of " << setprecision(2) << fixed << ticket << " is issued to " << plate << "\n\n";

            ticketCount++;
        }
        else if (overspeed > 20  && overspeed <= 50)
        {
            ticket = base + 10 * overspeed;
            cout << "A ticket of " << setprecision(2) << fixed << ticket << " is issued to " << plate << "\n\n";

            ticketCount++;
        }
        else if (overspeed > 50)
        {
            ticket = base + 1000 + (10 * overspeed);
            cout << "A ticket of " << setprecision(2) << fixed << ticket << " is issued to " << plate << "\n\n";

            ticketCount++;
        }
        else
            cout << "No ticket is issued to " << plate << ".\n\n";
    }

    cout << ticketCount << " out of " << totalCount << " times\n";

    return 0;
}
  • 4
    `cin >> plate; if (plate == "NOMORE") break;` – Igor Tandetnik Oct 09 '16 at 22:44
  • 2
    The reason why your loop does that is because that's what you wrote your program to do. – Sam Varshavchik Oct 09 '16 at 22:45
  • Does that mean that I don't need a sentinel then? – vincelam1998 Oct 09 '16 at 22:45
  • @Sam Varshavchik How exactly did I write it so that it would do that? I thought that once the sentinel value was entered, the entire loop would stop? – vincelam1998 Oct 09 '16 at 22:46
  • 1
    @vincelam1998 If you want to exit the loop right after you `cin` the sentinel, then you need to tell your program to do that. See @IgorTandetnik's comment. The condition in a `for` loop is only checked in between iterations, not after every statement. – qxz Oct 09 '16 at 22:48
  • 1
    The loop condition is checked **once every iteration**, so if you enter the sentinel first thing in an iteration, the check won't happen again until the entire loop body is executed. There's no magical way for the loop construct to suddenly check the condition mid way. – StoryTeller - Unslander Monica Oct 09 '16 at 22:48
  • @qxz ah okay, my professor never told me that. He only told us that if the sentinel value was entered, it would stop the loop. Thank you! – vincelam1998 Oct 09 '16 at 22:50
  • You need to read up on how loops work if you're going to be writing loops. – juanchopanza Oct 09 '16 at 22:50
  • 1
    Your professor needs a slap upside the head! – Lightness Races in Orbit Oct 09 '16 at 22:54
  • You wrote exactly that: prompt for a license plate number, enter it, prompt for the speed, and enter it. There's nothing in your code that stops the program when the sentinel is entered. The very next statement after the license plate is entered is to prompt for the next input. Like I said: the computer will do exactly what you wrote the program to do. – Sam Varshavchik Oct 09 '16 at 22:57
  • Did your professor actually say it would *immediately* stop the loop, or is that what you inferred? I can honestly see both things happening. – StoryTeller - Unslander Monica Oct 09 '16 at 22:57
  • @SamVarshavchik That has already been established... No need to be rude/unhelpful by saying "well, that's what you _told_ it to do" – qxz Oct 09 '16 at 22:58
  • @qxz, Sam isn't being rude. It's a common novice mistake that we all had to make and learn from. – StoryTeller - Unslander Monica Oct 09 '16 at 23:00
  • @vincelam1998 well, it would stop the loop but not immediately after cin return NOMORE – marcinj Oct 09 '16 at 23:01
  • @StoryTeller He said that the sentinel loop would continue as long as the sentinel value is not entered. If the sentinel value is entered, the loop will stop. That's exactly what he said. – vincelam1998 Oct 09 '16 at 23:04
  • No worries, please be as rude as you want haha, just wanted to figure out why it was doing that and learn. – vincelam1998 Oct 09 '16 at 23:06
  • @vincelam1998, everything he said is true. And he didn't seem to say it would stop immediately, so I guess it was (mistakenly) inferred. – StoryTeller - Unslander Monica Oct 09 '16 at 23:06
  • @StoryTeller Yeah, when he said the loop will stop, I interpreted that as it would immediately stop. My bad I guess! – vincelam1998 Oct 09 '16 at 23:07

1 Answers1

1

As @IgorTandetnik commented, add this right after you get the user input:

cout << "Enter a license plate number  --> ";
cin >> plate;
if (plate == "NOMORE") break; // add this
cout << "Enter current vehicle's speed --> ";
...

The condition in a for loop is only checked between iterations, not after every statement. This flowchart sums up how they behave:

For loop flowchart

qxz
  • 3,814
  • 1
  • 14
  • 29