mardi 3 mars 2015

Why does program crash after exiting do-while loop asking for input in C?

I am having trouble with correctly using a do-while loop to keep asking for another string if the one input is longer than what you want. After entering a string which is too big the program expects another but after I input an acceptable one the programs crashes instead of exiting normally, why is that? This is only part of the code of a larger program. Also, I am relatively new to C.



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRING_MAX 100

int main()
{
//Declaration of variables
char temp_input[STRING_MAX];

//Read input
do
{
scanf("%s", temp_input);
}while(strlen(temp_input)>STRING_MAX);

return 0;
}

Aucun commentaire:

Enregistrer un commentaire