Sams
Sams

Reputation: 197

while loop not executing

I made a while loop inside a program and the program reaches the while loop, but it doesn't execute. I feel like there's a really tiny error that I'm missing since I've been looking at the code for so long.

  int strbuf = 100;
  char string[strbuf];
  char *exit = "exit";
  while(!strcmp(string, exit)){
      printf("Enter a word to search. Enter exit to quit");
      scanf("%s", string);
      state = present(fIndex, string);
      if(state){
        printf("The word %s was found on line %d", string, state);
    }
}

EDIT: the input is from the keyboard. EDIT EDIT: NEW CODE (same problem)

int strbuf = 100;
char string[strbuf];
char *exit = "exit";

printf("Enter a word to search. Enter exit to quit\n");
scanf("%s", string);
    while(!strcmp(string, exit)){
    state = present(fIndex, string);
    if(state){
        printf("The word %s was found on line %d", string, state);
    }
    else printf("The word %s was not found", string);
}

Upvotes: 0

Views: 1994

Answers (5)

paxdiablo
paxdiablo

Reputation: 881103

Yes, it is executing. The body of the while loop may not be executing but that's because what you're doing is undefined behaviour, using string before it's initialised to anything useful.

A simple fix is to change:

char string[strbuf];

into:

char string[strbuf] = {'\0'};

And, for the sake of readability, you should expand your comparisons unless they truly are boolean values,(and, since you're hard-coding "exit" in a few places, I'm not sure why you'd have it as a variable):

while (strcmp (string, "exit") != 0) {

Upvotes: 1

Throwback1986
Throwback1986

Reputation: 5995

Furthermore, exit() is a standard C function (see stdlib.h). Something like strExit may better suit your aims.

Upvotes: 0

John3136
John3136

Reputation: 29266

If the while loop is not being executed, it means !strcmp(string, exit) is false

which means strcmp(string, exit) must be tru (non 0)

strcmp returns 0 on a match, so string is no the same as exit

The reason for this? You never put a value in string. Suggest changing it to a "do while" loop.

Upvotes: 0

AusCBloke
AusCBloke

Reputation: 18492

Read the man page for strcmp:

The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

If you have a match, strcmp will return 0, and if the two strings don't match it'll return a non-zero value.

Therefore while(!strcmp(string, exit)) is really saying, while the strings match, continue to loop.

string is also uninitialized and contains junk, causing undefined behaviour. Either initialise it first or or use a do..while loop if your loop must execute at least once.

Upvotes: 3

noMAD
noMAD

Reputation: 7844

You are not getting input for string before comparing it with exit. Put a:

printf("Enter a word to search. Enter exit to quit");
scanf("%s", string);

before your while loop.

Upvotes: 0

Related Questions