6

I can't seem to figure out what's wrong with this code:

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


#define MAX 100
#define TRUE 1
#define FALSE 0

char sect_cat;
char customer_name[MAX];
char customer_number[MAX];      /* error handling is easier */

int prev_unit = 0;
int current_unit = 0;
int consumed = 0;
int set = FALSE;

float init_bill;
float tax;
float total_bill;


    void get_userinfo()
    {

            printf("Enter sector category: ");
            scanf("%c", &sect_cat);
        printf("Enter customer name: ");
        fflush(stdin);
        scanf("%sn", &customer_name);

        set = FALSE;
        while (set == FALSE)
        {
            printf("Enter customer number: ");
            fflush(stdin);
            scanf("%s", customer_number);

            int i;
            int error;
            for (i=0, error=0; i<strlen(customer_number); i++)
            {
                if (isdigit(customer_number[i]))
                {
                }
                else
                {
                    error = 1;
                }
            }
            if (error == 0)
            {
                set = TRUE;
            }
            else
                printf("ERROR: Only numbers are allowed\n");
        }

        printf("Enter previous unit: ");
        fflush(stdin);
        scanf("%d", &prev_unit);

        set = FALSE;
        while (set == FALSE)
        {
            printf("Enter current unit: ");
            fflush(stdin);
            scanf("%d", &current_unit);

            if (prev_unit > current_unit)
            {
                printf("ERROR: Current unit must be larger than previous unit\n");
            }
            else
                set = TRUE;
        }
        consumed = current_unit - prev_unit;
    }



int main()
{


/* Introduce program to users */

        printf("\nThis program computes your electric bill based on these sector categories\n\n");

    printf("\tResidential(R)\n");
    printf("\tIndustrial(I)\n");
    printf("\tCommercial(C)\n\n");

    printf("Press any key to continue...");
    fflush(stdin);
    getchar();  
#################### edit

Applying templatetypedef's solution, the program now waits for user input for the customer_name. However entering a string with a space leads to an error, and the program assumes that the word after the space is input for the next prompt.

Enter sector category: r
Enter customer name: George of the Jungle
Enter customer number: ERROR: Only numbers are allowed
Enter customer number: ERROR: Only numbers are allowed
Enter customer number:
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 8
    `fflush(stdin)` is undefined behavior. – Mysticial Feb 03 '12 at 01:30
  • 4
    In the future, rather than dump a huge program (with no line numbers or indication of where the relevant info is) try to post a code example that demonstrates the same problem with as little extraneous code as possible, so that we can more clearly help you. (Also, you may even end up solving it yourself!) – Chris Lutz Feb 03 '12 at 01:44

3 Answers3

10

The fflush function does not flush data out of an input stream; it is instead used to push data buffered in an output stream to the destination. This is documented here. As seen in this earlier SO question, trying to use fflush(stdin) leads to undefined behavior, so it's best to avoid it.

If you want to eat the newline from the return character entered when the user finished typing in their character, instead consider the following:

scanf("%c%*c", &sect_cat);

This will eat the newline rather than leaving it in stdin.

Hope this helps!

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    I'd rather the OP consider using `fgets` instead of `scanf`, but anyone trying to `fflush(stdin)` has a ways to go yet, so I'll settle. – Chris Lutz Feb 03 '12 at 01:41
  • 3
    It didn't work as expected. After the input letter, the program waits for another input then proceeds. – Karen Langres Bague Feb 03 '12 at 02:08
  • While `fflush(stdin)` (or on any reading stream) is undefined per ISO C, POSIX does define the behavior in the case where the file is seekable. It's still undefined for unseekable streams though, and `stdin` is normally unseekable (e.g. if it's a terminal or pipe). – R.. GitHub STOP HELPING ICE Feb 03 '12 at 02:43
  • @KarenLangresBague That's because a whitespace character in the format string of `scanf` instructs `scanf` to scan *any* number of whitespace characters including none, **until the first non-whitespace character**. – Spikatrix Jul 25 '15 at 15:36
0

I think that you meant to write fflush(stdout) instead of fflush(stdin).

asaelr
  • 5,438
  • 1
  • 16
  • 22
  • 2
    I don't think this is what the OP meant. I think the OP intended `fflush(stdin)` to clear the newline character out of `stdin` so that it isn't picked up by later read operations. – templatetypedef Feb 03 '12 at 01:33
-1

fflush should work with an output stream, see docs here

pjhades
  • 1,948
  • 2
  • 19
  • 34