0

I am having a hard time trying to figure out how to end my program as soon as the word "NOMORE" is entered.
I have researched similar threads suggesting the use of "if, break" or "if, go to".

Although the program will end after entering "NOMORE", I'd like to know if it's possible to end without processing the remaining modules (calcTicket, printInfo).

int main ()
{
string licensePlate;
double vehicleSpeed = 0, zoneSpeed = 0, ticketPrice, base = 0, fee = 0;

while (licensePlate !="NOMORE")
{
   getInfo(licensePlate, vehicleSpeed, zoneSpeed);
   if (licensePlate == "NOMORE")
      break;
   calcTicket(vehicleSpeed, zoneSpeed, ticketPrice, base, fee);
   printInfo(licensePlate, vehicleSpeed, zoneSpeed, ticketPrice);
}

return 0;
}
Prashanth Benny
  • 1,523
  • 21
  • 33
James Henrick
  • 71
  • 1
  • 13
  • And currently it doesn't? – Sami Kuhmonen Oct 24 '16 at 04:40
  • do you want to stop the whole program when its 'nomore'? – Prashanth Benny Oct 24 '16 at 04:40
  • Hello, the program will end, but only after prompting the user with calcTicket and printInfo modules. I'd like for it to end immediately and only prompt "Press any key to continue . . ." – James Henrick Oct 24 '16 at 04:42
  • what is the need of an if which checks for the 'nomore' when the while loop already prevents from calculating those statements..... the `if (licensePlate == "NOMORE") break;` will never be executed! if the `licensePlate = "NOMORE"` – Prashanth Benny Oct 24 '16 at 04:44
  • Actually, I have removed the if statement in the code above; however, even if the user enters "NOMORE", the program will end only after prompting the user with calcTicket and printInfo modules. – James Henrick Oct 24 '16 at 04:46
  • try using **continue instead of break**.... i missed to see that you get the info after the while check....... this is a terrible logic! ;) – Prashanth Benny Oct 24 '16 at 04:50
  • using the do while loop would be a better workaround! – Prashanth Benny Oct 24 '16 at 04:56

5 Answers5

4

I assume, in asking if it's "possible to end" you refer to terminating the loop, rather than the program.

If you can't change getInfo(), I'd structure as a do-while rather than a while.

do
{
     getInfo(licensePlate, vehicleSpeed, zoneSpeed);
     if (licensePlate == "NOMORE") break;
     calcTicket(vehicleSpeed, zoneSpeed, ticketPrice, base, fee);
     printInfo(licensePlate, vehicleSpeed, zoneSpeed, ticketPrice);
} while (true);

The premise is that getInfo() needs to be called at least once, the subsequent processing is required to NOT occur if "NOMORE" is entered, but otherwise the loop continues forever.

If you can change getInfo() to return bool it is possible to do ...

while (getInfo(licensePlate, vehicleSpeed, zoneSpeed))
{
    calcTicket(vehicleSpeed, zoneSpeed, ticketPrice, base, fee);
    printInfo(licensePlate, vehicleSpeed, zoneSpeed, ticketPrice);
}

This assumes getInfo() returns false if it sees "NOMORE", and that it deals appropriately with vehicleSpeed and zoneSpeed regardless. An advantage is that getInfo() doesn't actually need to read those values if the end condition is met. Obviously there will be a test inside getInfo(), but the caller needn't care about how that is done.

Peter
  • 35,646
  • 4
  • 32
  • 74
1

if i understood the question correctly try this:

int main ()
{
    string licensePlate;
    double vehicleSpeed = 0, zoneSpeed = 0, ticketPrice, base = 0, fee = 0;

    while (licensePlate !="NOMORE")
    {
        getInfo(licensePlate, vehicleSpeed, zoneSpeed);
        if (licensePlate != "NOMORE")
        {
            calcTicket(vehicleSpeed, zoneSpeed, ticketPrice, base, fee);
            printInfo(licensePlate, vehicleSpeed, zoneSpeed, ticketPrice);
        }
    }

    return 0;
}

you will always go to the cal ticket function as there is no condition not to go there in your code. in this code you check for the NOMORE condition if it is there you wont call those functions

lallous34
  • 483
  • 1
  • 4
  • 12
0

If you for some reason cannot use break, you can wrap the calcTicket and printInfo in an if statement like if(licensePlate != "NOMORE").

If you are able to use break, I would do (to only have one comparison)

while (true)
  {
  getInfo(licensePlate, vehicleSpeed, zoneSpeed);
  if (licensePlate == "NOMORE")
    break;
  calcTicket(vehicleSpeed, zoneSpeed, ticketPrice, base, fee);
  printInfo(licensePlate, vehicleSpeed, zoneSpeed, ticketPrice);
}

Maybe the problem is with your input and you are not entering your if statement at all? Are you entering "NOMORE" in all caps, for example?

Melodi
  • 1
  • 2
0

An alternative solution is to use a flag which is set when the user wants to quit, and use that flag in the loop condition:

bool userWantsToQuit = false;
while (!userWantsToQuit)
{
   getInfo(licensePlate, vehicleSpeed, zoneSpeed);
   userWantsToQuit = (licensePlate == "NOMORE");
   if (!userWantsToQuit)
   {
      calcTicket(vehicleSpeed, zoneSpeed, ticketPrice, base, fee);
      printInfo(licensePlate, vehicleSpeed, zoneSpeed, ticketPrice);
   }
}
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
-1

So, why you don't use exceptions?

void getInfo(licensePlate, vehicleSpeed, zoneSpeed)
{
    licensePlate = /*get logic*/;

    if (licensePlate == "NOMORE")
        throw std::invalid_argument("NO MORE");
}

/*..define other functions..*/

int main ()
{
    string licensePlate;
    double vehicleSpeed = 0, zoneSpeed = 0, ticketPrice, base = 0, fee = 0;

    while(true)
    {
        try
        {
            getInfo(licensePlate, vehicleSpeed, zoneSpeed);
            calcTicket(vehicleSpeed, zoneSpeed, ticketPrice, base, fee);
            printInfo(licensePlate, vehicleSpeed, zoneSpeed, ticketPrice);
        }
        catch(...)
        {
            /*exit logic*/
        }
    }

    return 0;
}
Pavel.Zh
  • 437
  • 3
  • 15
  • 2
    Exceptions aren't really appropriate for this — "NOMORE" is just the end of the input, not an error, and there are simpler (and more-efficient) ways to terminate a loop. – Wyzard Oct 24 '16 at 05:09
  • @Wyzard, I agree - 'NOMORE' is not error and using exceptions is not the most efficient way to break the loop, but: 1) Code looks more clear 2) We do not repeat useless if...goto or if...break. 3) User input operations do not need perfect speed, because they are really slow by definition, so we don't need to worry about little overhead. – Pavel.Zh Oct 24 '16 at 05:15
  • 1
    It's not about speed at all. Exceptions are semantically wrong here, because normal user input is not an exceptional situation. – Christian Hackl Oct 24 '16 at 08:52