1

Why doesn't the while loop break after I enter the symbols that should terminate the loop. (C)

 printf("Enter a sentence: ");
do
{
    message[i] = getchar();
    i++;
}while(message[i - 1] != '.' || message[i - 1] != '!' || message[i - 1] != '?');

if I put . ? or ! at the end it just keeps running. Whats wrong?

TacoCat
  • 459
  • 4
  • 21
  • That condition is always true. It will always *not* be one of those, as it cannot be *all* of those *simultaneously*, which is the only way the condition becomes false. – WhozCraig Dec 29 '14 at 02:15

5 Answers5

2

the condition for your while loop is trivially true since the character cannot be all three at once.. what you're looking for is

printf("Enter a sentence: ");
do
{
    message[i] = getchar();
    i++;
}while(message[i - 1] != '.' && message[i - 1] != '!' && message[i - 1] != '?');
  • while that fixes your bug, i'm not sure reading command line input using getchar() makes any sense. – softwarenewbie7331 Dec 29 '14 at 02:16
  • It works with && but before I was sure that if you used && everything had to true, if u used || then it required only one of the statements to be true. – TacoCat Dec 29 '14 at 02:18
  • Why wouldn't it make sense to use getchar()? – TacoCat Dec 29 '14 at 02:18
  • he also changed it to have `!=` so it means do this while it does not equal . ! or ? , so once it does, then it cant be true – Colum Dec 29 '14 at 02:20
  • you are checking that char is not '.' AND not '!' AND not '?' before reading the next character. The alternative with || is similar to what Colum's answer has, although you could just call 'break;' in the IF statement. – softwarenewbie7331 Dec 29 '14 at 02:21
  • take a look at [this other post](http://stackoverflow.com/questions/7119470/int-c-getchar) getline() function, a textbook example of how to use getchar() to read a sentence. the additional code ensures the resulting char is '\0' terminated and the getchar() stops if EOF is reached. – softwarenewbie7331 Dec 29 '14 at 02:27
1

Change the or's to and's in your while statement.

For "or", as long as there is a "True", the whole statement evaluates to True. Therefore your original 'while' condition always evaluates to "True".

'While' only terminates when the condition evaluates to False. However, in your original condition, that will never happen because:

  1. All three conditions cannot happen(be False) at the same time. The value cannot be '.', '!', and '?' at the same time.

  2. This means: At least two of your three conditions always evaluate to True. Consequently, the 'while' will never evaluate to False, because any permutation of True||False||False will evaluate to True.

Jobs
  • 3,317
  • 6
  • 26
  • 52
  • Thanks, why doesn't the 'or' statement work? why doesn't it require for all three to be true? – TacoCat Dec 29 '14 at 02:15
  • `cond1 || cond 2` to the best of my knowledge, means that if one is false then the other must be true? someone might need to clarify that though. – Colum Dec 29 '14 at 02:18
0

If you are entering this interactively, message[ i - 1 ] is almost certainly \n. Check the value of message[ i - 2 ]

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • This didn't work, when I changed the Or's to And's it works perfectly, still not sure why but it does. – TacoCat Dec 29 '14 at 02:16
0

try this:

printf("Enter a sentence: ");
int run = 1;
do {
    message[i] = getchar();
    i++;
    if(getchar() != '!' && getchar() != '?' && getchar() != '.'){
       run = 1;
    } else {
       run = 0;
    }
}while(run == 1);

or , i think this might work :/

printf("Enter a sentence: ");
int run = 1;
do {
    message[i] = getchar();
    i++;
    if(getchar() == '!' || getchar() == '?' || getchar() == '.'){
       break;
    }
}while(run == 1);
Colum
  • 996
  • 2
  • 8
  • 23
0

You need to swap your OR's with ANDS's.

Whilst message[i - 1] is equal to '.' that period is not equal to '!' and '?', hence your loop will always be true.

try something like...

}while(message[i - 1] != '.' && message[i - 1] != '!' && message[i - 1] != '?');

(untested code off the top of my head)

damichab
  • 162
  • 10