why? please help

This is a discussion on why? please help within the C Programming forums, part of the General Programming Boards category; Why when i type "none" for drink type, it doesn't print BILL. Here is the code. Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    27

    why? please help

    Why when i type "none" for drink type, it doesn't print BILL.
    Here is the code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX 30
    int main()
    {
        int customerCount=0;            /*how many people for the table*/
        char input[32];                 /*buffer for input*/
        char drinks[MAX][10];           /*array for drinks type*/
        char smallLarge[MAX][6];        /*array for small or large*/
        char hotCold[MAX][5];           /*array for hot or cold*/
        int i=0;                        /*array counter*/
        int numDrink=0;                 /*number of drinks ordered*/
        int j=0;                        /*loop counter*/
    
        printf("\n          ===========================================================\n");
        printf("          ================ Welcome the CPE coffee shop ==============\n");
        printf("          ===========================================================\n\n");
    
        /*Ask how many people are at the table*/
        printf("How many people are at your table?: ");
        fgets(input,32,stdin);
        sscanf(input,"%d",&customerCount);
    
        /*While number of people is less than 0, ask again*/
        while(customerCount<0)
            {
            printf("Error! How many people are at your table?: ");
            fgets(input,32,stdin);
            sscanf(input,"%d",&customerCount);
            }
    
        /*If number of people is more than 0, ask type of drink*/
        if(customerCount>0)
        {
            printf("\nChoose what type of drink would you like.\n");
            printf("Type none if you want no more drinks.\n\n");
    
            while(customerCount!=0)
                {
                printf("coffee, tea, cocoa, or ovaltine?: ");
                fgets(input,32,stdin);
    
                /*If the type of drink match none, print bill for the table*/
                if(strcmp(input,"none")==0)
                    {
                    printf("BILL\n");
                    /*bill*/
                    }
                /*If input has upper case alphabet, change to lower case*/
                for(j=0;j<strlen(input);j++)
                    {
                    if(isupper(input[j]))
                        {
                        input[j]=tolower(input[j]);
                        }
                    }
    
                sscanf(input,"%s",drinks[i]);
    
                /*While type of drinks doesn't match coffee,tea,cocoa,ovaltine,or none, ask again*/
                while((strcmp(drinks[i],"coffee")!=0)&&(strcmp(drinks[i],"tea")!=0)&&
                (strcmp(drinks[i],"cocoa")!=0)&&(strcmp(drinks[i],"ovaltine")!=0)&&
                (strcmp(drinks[i],"none")!=0))
                    {
                    printf("Error!Type coffee, tea, cocoa, or ovaltine?: ");
                    fgets(input,32,stdin);
                    sscanf(input,"%s",drinks[i]);
    
                    }
                printf("Drink %d\n",i+1);
                printf("%s\n",drinks[i]);
    
    
                /*Ask the user, small or large*/
                printf("small or large?: ");
                fgets(input,32,stdin);
    
                /*If input has upper case alphabet, change to lower case*/
                for(j=0;j<strlen(input);j++)
                    {
                    if(isupper(input[j]))
                        {
                        input[j]=tolower(input[j]);
                        }
                    }
    
                sscanf(input,"%s",smallLarge[i]);
    
                printf("Drink %d\n",i+1);
                printf("%s\n",smallLarge[i]);
    
                while((strcmp(smallLarge[i],"small")!=0)&&(strcmp(smallLarge[i],"large")!=0))
                    {
                    printf("Error!Type small or large?: ");
                    fgets(input,32,stdin);
                    sscanf(input,"%s",smallLarge[i]);
                    }
    
                /*Ask the user, hot or cold*/
                printf("hot or cold?: ");
                fgets(input,32,stdin);
    
                /*If input has upper case alphabet, change to lower case*/
                for(j=0;j<strlen(input);j++)
                    {
                    if(isupper(input[j]))
                        {
                        input[j]=tolower(input[j]);
                        }
                    }
    
                sscanf(input,"%s",hotCold[i]);
    
                printf("Drink %d\n",i+1);
                printf("%s\n",hotCold[i]);
    
                while((strcmp(hotCold[i],"hot")!=0)&&(strcmp(hotCold[i],"cold")!=0))
                    {
                    printf("Error!Type hot or cold?: ");
                    fgets(input,32,stdin);
                    sscanf(input,"%s",hotCold[i]);
                    }
                i++;
                numDrink++;
                printf("i=%d\n",i);
                printf("numDrink=%d\n",numDrink);
                }
        }
    
        /*If the number of people equals to 0,meaning the end of the day, print summary*/
        else if(customerCount==0)
            {
            printf("SUMMARY");
            /*summary*/
            }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    If fgets() reads a newline, it stores that (assuming there's enough room). The easy way out is to compare against "none\n"; or use strchr() to see if fgets() stored a newline, and if so, replace it with 0 (the null character). Since there's no guarantee of a newline I'd recommend the latter.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Or you can do what you did everywhere else in your code, and use sscanf() after each call to fgets().
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21