Legion Daeth
Legion Daeth

Reputation: 329

Fgets() requires enter to be hit twice after reading to char array

When I enter anything that evaluates to be false in function isFloat(char array[]) I need to hit enter twice to keep the program running.

If I comment out everything but the fget() command everything requires me to hit enter twice. What could be causing this? I'm flushing stdin properly and the \n is removed by strtok(). Is the printf() function causing problems? I've read that scanf() and fgets() can cause problems when used together. But here they arent.

Problem Area

printf("first number: ");
    fgets(input, TEN_THOUSAND, stdin);
    strtok(input, "\n");
    success = isFloat(input);
    if(success)
        firstNum = atof(input);

Full Code:

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

int isFloat(char array[])
{

int m = 0;
int periodCount = 0;

for(m=0; array[m] != '\000'; m++)
    {
        if(array[m] == '1' || array[m] == '2' || array[m] == '3' || array[m] == '4' || array[m] == '5' || array[m] == '6' || array[m] == '7' || array[m] == '8' || array[m] == '9' || array[m] == '0')
            {
            }
        else
            {
                if(array[m] == '.' && periodCount == 0 && m != 0 && m+1 != '\n')
                    periodCount = 1;
                else
                    return 0;
            }
    }
return 1;
}

void eatLine()
{
    while (getchar() != '\n');
}

int main()
{

double firstNum = 0.0;
double secondNum = 0.0;
double totalNum = 0.0;
int success = 0;
int TEN_THOUSAND = 10000;
char input[TEN_THOUSAND];

//Outputs assignment header
printf("CS201 - Lab 2 - Number Adder\n\n");

printf("first number: ");
fgets(input, TEN_THOUSAND, stdin);
strtok(input, "\n");
success = isFloat(input);
if(success)
    firstNum = atof(input);

while(!success)
{
    eatLine();
    //The one is for testing purposes
    printf("-- bad input --\n");
    printf("first number: ");
    fgets(input, TEN_THOUSAND, stdin);
    strtok(input, "\n");
    success = isFloat(input);
    if(success)
        firstNum = atof(input);
}

printf("second number: ");
fgets(input, TEN_THOUSAND, stdin);
strtok(input, "\n");
success = isFloat(input);
if(success)
    secondNum = atof(input);

while(!success)
{
    eatLine();
    //The one is for testing purposes
    printf("-- bad input --\n");
    printf("second number: ");
    fgets(input, TEN_THOUSAND, stdin);
    strtok(input, "\n");
    success = isFloat(input);
    if(success)
        secondNum = atof(input);
}

//adds the numbers
totalNum = firstNum + secondNum;

//Solves ugly formatting problem by firstly including a newline 
//after the input is garnered. then it outputs firstNum and totalNum 
//in a field of 11 spaces with a newline terminator. This decrements 
//11 to 10 on the secondNum line to compensate for the space that the + takes up.
printf("\n%11.2f\n", firstNum);
printf("%s%10.2f\n", "+", secondNum);
printf("-----------\n");
printf("%11.2f\n\n", totalNum);

return 0;
}

Upvotes: 1

Views: 1088

Answers (1)

R Sahu
R Sahu

Reputation: 206607

When I enter anything that evaluates to be false in function isFloat(char array[]) I need to hit enter twice to keep the program running.

That's because you have a line of code that expects you to enter a line of text.

while(!success)
{
   eatLine(); // Culprit

Upvotes: 3

Related Questions