0

Hi I'm trying to get my C console program to flash between different colors to indicate a warning to the user. But each time I launch the program, the next fgets crashes the whole thing.

char vitesseOverdrive[5];
int vitesse = 0;
system("cls");
    printf("\n\n------------------------- Warning ! ------------------------\n");
    for (i=0;i<5;i++)
    {
        system("color 0c");
        Sleep(300);
        system("color 0a");
        Sleep(300);
    }
vitesse = SaisieNombre(vitesseOverdrive,sizeof(vitesseOverdrive)/sizeof(char));

What could cause the program to crash ? Is there a better way to do it or could it be bypassed ?

info : the program works fine as soon as i put these lines in comments

Thanks !

Edit : here is the fgets() function

int SaisieNombre(char *chaine,int a)
{
    int i;
    int j;
    int nombre = 0;
    int work = 0;
    int marche = 0;
    int reussi = 0;
    char *endptr = NULL;
    char *place = NULL;

    do
    {
        work = 0;
        reussi = 1;
        marche = 1;
        for (i=0;i<a;i++)
        {
            chaine[i] = 0;
        }
        if (fgets(chaine,a,stdin) != NULL)
        {
            if (strchr(chaine,'\n') != NULL)
            {
                place = strchr(chaine,'\n');
                *place = '\0';
                for (j=0;j<a;j++)
                {
                    if (isdigit(chaine[j]) != 0 || chaine[j] == '\0' )
                    {

                    }
                    else
                    {
                        reussi = 0;
                    }
                }
            }
            else
            {
                printf("\nLe nombre est trop long\n");
                printf("\nVeuillez entrer un nombre : ");
                marche = 0;
                viderBuffer();
            }
        }
        else
        {
            viderBuffer();
            Sleep(2000);
            exit(0);
        }
        if (reussi != 1)
        {
            printf("\nLa donnee n'est pas un nombre\n\n");
            printf("Veuillez entrer un nombre : ");
        }

        nombre = strtol(chaine,&endptr,10);

        if (*endptr == '\0')
        {
            work = 1;
        }
        else
        {
            work = 0;
        }
    }while (work != 1 || reussi != 1 || marche != 1);
    return nombre;
}
Polar Bear
  • 354
  • 3
  • 15
  • those lines of code work fine for me – Keith Nicholas Nov 29 '12 at 21:43
  • Why not to use WinApi to works with console? For example `SetConsoleTextAttribute` or `WriteConsoleOutputAttribute`? – Maximus Nov 29 '12 at 22:53
  • What compiler? Have you debugged to see that it's really the fgets() call? And as already asked, what's the context for fgets()? For what it's worth, I tried the above code plus a subsequent fgets() on stdin using MinGW GCC and it worked fine for me. – David Duncan Nov 30 '12 at 08:08
  • I don't know what WinApi is but i'm looking it up. as for my fgets() i've posted it in the question. – Polar Bear Dec 03 '12 at 14:27
  • and sorry i don't know how to use the debugguer ... i've started learning by myself about a week ago ... i could link the whole code if it's possible. Or if you have any tutorial for it, it would be great (i'm using code block) – Polar Bear Dec 03 '12 at 14:34
  • 1
    You changed your code while I was going to answer, but it still works fine for me even with your expanded current code. Verify that vitesse is defined as a char pointer and vitesseOverdrive has allocated memory, e.g., `char vitesseOverdrive[100];`. – David Duncan Dec 03 '12 at 15:23
  • vitesse is defined as an int like my function is returning. should i change the output to be a pointer ? – Polar Bear Dec 03 '12 at 15:41
  • well looks like it wanted to work today ... i changed other aspects of the code but didn't touch that part, i've tried compiling again with the faulty code and now it works. I don't understand what was wrong before but at least it's working now ! Thanks a lot for taking time to respond ! – Polar Bear Dec 03 '12 at 15:47
  • @Émile: Sorry, as I'm sure you've figured, my previous comment was directed to the old code when SaisieNombre() was returning a char pointer, not an int. Glad you figured it out! Mildly related to the return value and hugely for what it's worth, strtol() returns a long, which could be (but is not necessarily) larger than an int. – David Duncan Dec 04 '12 at 03:30

0 Answers0