0

So I want to loop through infinitely and take a user string as input and then sort it. I want to continuously take input until the user enters 'EXIT'. That part of the code works. However, It won't loop infinitely properly. (This is also in strict C).

It should ask the user to input a string, sort it, print it, ask the user to input a string, sort it, etc. etc. However, what it does, is it asks the user to input a string, sorts it, prints it, then it prints it, prints it, prints it, etc. What is wrong here? Thanks.

int main(){
    //Allocate a 100 character long array in the heap which will be passed and altered in place and outputted.
    //I'm not fully sure how to allocate dynamically the appropriate amount of space for strings without creating a whole class for it.
    //But seeing as this is c and not c++ then I'm not sure. Cause I need to have a length of how long the input is, but I need the memory allocated
    //to store the string ti find the length of.
    char* input = (char*)malloc(100);
    //Scanf allows input for any characters including white space.
    while(1){
        printf("Enter an input string: ");
        scanf("%[^\n]", input);

        int len = strlen(input);
        //Checks for exit condition. Input string being 'EXIT'
        if(len == 4 && input[0] == 'E' && input[1] == 'X' && input[2] == 'I' && input[3] == 'T'){
            break;
        }
        else{
        //Function calls
            remove_duplicates(input);
            sort(input);

            printf("\"%s\"\n", input);
        }
    }
    free(input);
    input = NULL;

    return 0;
}
Paulie
  • 11
  • 2
  • 1
    First of all, you shouldn't really [cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Secondly, why use `malloc` and `free` in the first place, why not use a plain and simple array (as in `char input[100];`)? – Some programmer dude Sep 21 '19 at 20:58
  • Oh and why use the character-by-character comparison instead of a single [`strcmp`](https://en.cppreference.com/w/c/string/byte/strcmp) call? – Some programmer dude Sep 21 '19 at 20:59
  • You don't check the return value of `scanf, that would give a hint. The hint is: you read until a newline. What happens when the user sends a newline in the end of the line and you never read it? – Sami Kuhmonen Sep 21 '19 at 20:59

1 Answers1

0

Many format specifiers for scanf skip leading while-space (like newlines), but not "%[".

So the newline that is added by you pressing Enter in the first input will be the the first thing that the next call to scanf will read. And with your format a newline (or really, any space) will tell scanf to stop reading, which means nothing more will ever be read. Over and over again.

I would rather recommend you use fgets to read lines, as that also included automatic buffer-overflow protection (granted you pass the correct buffer size).

And if you're worried about the newline added by fgets it's easy to remove with the strcspn function.

So better do something like

char input[100];
while (fgets(input, sizeof input, stdin) != NULL)
{
    input[strcspn(input, "\n")] = 0;  // Remove possible trailing newline

    if (strcmp(input, "EXIT") == 0)
        break;  // Exit
    else
    {
        // Rest of code
    }
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621