1

I want the do while loop to check to see if the entered input is R OR P. I think it is checking for both and when I get to that part when I run, it pauses for a minute and then I get "CPU Limit Exceeded (core dumped). On another related note, am I in danger of breaking something?

/************************************************/
/* Name: servcode                               */
/* Description: Get service type                */
/* Parameters: N/A                              */
/* Return Value: servcode                       */
/************************************************/

char servcode()
{
  char servcode = 'a';   // Define variable for service code
  char serviceyn = 'n';  // Define variable for user verify
  int i = 1;             // Define variable for sentinel loop

  do {
    cout << "\n" << "\n" << "Please enter your service code, [R]egular or [P]remium: " << "\n";
    cin >> servcode;
    while ((servcode != 'R', 'P') && (i < 3));
    {
        cout << "\n" << "Error - invalid service code, please try again.";
        cout << "\n" << "Please enter your service code: ";
        cin >> servcode;
        i++;
        if (i == 3)
        {
            cout << "\n" << "Too many invalid attempts, program terminating." << "\n"
             << "Have a nice day. " << "\n" << "\n";
            exit (0);
        } //end if
    } //end while
    cout << "\n" << "You entered: " << servcode << "\n"
     << "Is that correct? [y,n]";
    cin >> serviceyn;
  } while (serviceyn != 'y'); // end do/while loop

  return servcode;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam LaManna
  • 425
  • 2
  • 7
  • 15
  • A canonical question is *[Multiple conditions in a C 'for' loop](https://stackoverflow.com/questions/16859029/)*. But there must be one from 2008 or 2009. – Peter Mortensen Sep 01 '22 at 22:54

5 Answers5

7

The correct syntax is:

while (servcode != 'R' && servcode != 'P' && i < 3)

Note the expanded comparison and the removal of the semicolon at the end:

  1. (servcode != 'R', 'P') is valid C++ but doesn't do what you're expecting it to do;
  2. the semicolon makes the statement into a loop with an empty body, so it continues executing forever since the loop condition never changes.
NPE
  • 486,780
  • 108
  • 951
  • 1,012
4

Change:

while ((servcode != 'R', 'P') && (i < 3));

to:

while ((servcode != 'R') && (servcode != 'P') && (i < 3))

Note the removal of an unwanted semicolon.

Paul R
  • 208,748
  • 37
  • 389
  • 560
0
(servcode != 'R', 'P')

Should be:

(servcode != 'R') && (servcode != 'P')

Kudos to PaulR for correction. My brain is not in gear.

SE Does Not Like Dissent
  • 1,767
  • 3
  • 16
  • 36
0

You need to do something like this:

while(something != 'a' && something != 'b')

You're using the comma operator, which discards the results of every expression except the last, so this:

while(something != 'a', 'b')

Will compare something with a, ignore the result, and use 'b' as the condition of the loop. 'b' is a non-zero value, so it's always true and the loop goes on forever (or until memory runs out, or something else stops it).

Staven
  • 3,113
  • 16
  • 20
0

Let's break down this statement and see what it's doing. I've put in some extra punctuation that does not change the meaning of the statement.

while (((servcode != 'R'), ('P')) && (i < 3)) { };

The comma operator separates two different expressions and returns the value of the second one. The first part probably does what you expect, but the second part is just a literal character 'P' which is always true!

The semicolon at the end of the statement marks the body of what will be executed as the while loop. It's a do-nothing which does not change the value of servcode or i, so obviously once you enter the loop you will never leave it. This is a common mistake.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622