Search code examples
cfunctiongets

Getting input from the user- does not work


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

static struct prod_details pd;

char *getinput(char *inp)
{
    printf("Enter the amount of the product %d:\n",pd.no_prod+1);
    gets(inp);
    return inp;
}

void print()
{
    printf("........................\n");
    printf("No of Product is: %d\n",pd.no_prod);
    printf("Grant Total is : %.2f\n",pd.total);
    printf("........................\n");
}

int check(char *str) 
{
    int i;
    if(strlen(str) == 0)
        return 2;
    for(i=0;i<strlen(str);i++)
    {
        if(str[i] == '-')
            return 3;
        if(isalpha(str[i]) != 0)
            return 0;
    }
    return 1;
}

void calc(char *str)
{
    pd.array[pd.no_prod]=atof(str);
    pd.total=pd.total+pd.array[pd.no_prod];
    printf("Total is:%.2f\n",pd.total);
    pd.no_prod++;
}

int main()
{
    int chkflg,i=0,flag=0,cflag=0;
    char ch;
    char input[1024];
    printf("..................\n");
    printf("..CASE  RIGISTER..\n");
    printf("..................\n");
    //strcpy(input,getinput(i+1));
    //printf("%s\n",input);
    do
    {
        strcpy(input,getinput(input));
        chkflg=check(input);
        switch(chkflg)
        {
            case 0:
                printf("Please Enter Correctly...!!!\n");
                printf("You Have entered Wrongly.!!!\n");
                flag=0;
                break;
            case 1:
                calc(input);
                flag=1;
                break;
            case 2:
                printf("You didnt enter anything.!!!\n");
                flag=0;
                break;  
            case 3:
                printf("Coundnot Subtract the Amount..!!!\n");
                flag = 0;
                break;      
        }
        if(flag == 0)
        {
            printf("Do u want to continue(y/n)");
            ch=getchar();
            if(ch == 'y')
            {
                flag=1;
                //continue;
            }
            else if(ch == 'n')
            {
                printf("Thank u..!!!\n");
                break;
            }
            else
            {
                printf("You didn't Enter Properly...!!!\n");
                break;
            }
        }   
    }while(flag == 1);
    print();
    return 0;
}

This is the program for calculating the bill.This Program works well for correct input(eg.double). But The problem is, if we enter wrongly as string, it shows the corresponding cases, and ask whether to continue or not. if we want to continue,it produces the output as:

You Have entered Wrongly.!!!
Do u want to continue(y/n)y
Enter the Amount of the product 3:
You didn't enter anything.!!!
Do u want to continue(y/n)

It does not get further the input.I was working with gdb. But I couldn't understand why it couldn't get the input further. Please help me to resolve this problem.Thank u in Advance.


Solution

  • When you use getchar the newline you press after the y is left in the input stream. Then when you do gets it reads that newline and you have an empty line.

    One way to solve this is to use e.g. scanf with a space after the format, as this will tell scanf to eat all whitespace after the read character:

    printf("Do you want to continue(y/n)");
    scanf("%c ", &ch);
    

    Another solution is to use fgets to read the whole line, and extract the answer using e.g. sscanf.