Thread: case sensitivity problem

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    5

    case sensitivity problem

    I didn't post all of the code since this where the problem lies. whenever I read in 'Y' the program skips everything and go to the end but everything works well when I read 'y'... the code is below


    insert
    Code:
    printf("ENTER CODE CORRESPONDING TO THE TYPE OF APPAREL DESIRED: ");
        fflush(stdin);
        scanf(" %c",&code);
        switch(code)
        {
            case 'T':
            case 't':
           printf("ENTER QUANTITY OF T-SHIRT YOU WISH TO ORDER: ");
                fflush(stdin);
                scanf(" %d", &quantity);
                printf("DO YOU WANT ANY DESIGN? Y/N: ");
                fflush(stdin);
                scanf(" %c", &choice);
                system("cls");
    
    
                if ((quantity>=25 && quantity<=50) && (choice=='y' || choice=='Y'))
                    {
                        cost = quantity * 320 + MOULD;
                        discount = 0.0;
                        GCT = 0.165 * cost;
                        total = cost + GCT;
                        test= test+1;
                    }
    Attached Files Attached Files

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Don't flush stdin.

    I think the problem is elsewhere, probably further down.

    Btw, these lines don't do what you think they do. Remember that && takes precedence over ||.
    Code:
    (quantity>=25) && (quantity<50) && (choice=='y') || (choice=='Y')
    (quantity>50) && (choice=='y') || (choice=='Y')
    (quantity>50) && (choice=='n') || (choice=='N')
    (quantity>=25) && (quantity<50) && (choice=='N') || (choice=='n')
    Last edited by GReaper; 10-22-2017 at 11:25 PM.
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Just a few of the mistakes in here.
    I added tolower to fix your bracket issue of malformed bracket separation
    between && and || after I fixed the malformed bracket separation.

    your use of default in your switch is wrong, plus it has no break in it.
    read red comment inside of your code.


    Code:
    //#include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    //#include <conio.h>
    #include <ctype.h> // tolower
    //#include <math.h>  // not needed, you're not calling any math functions
    
    #define MOULD 3500
    
    int main()
    {
        int quantity, test=0;
        float cost, discount, Dis_Prize, total, GCT;
        char ComName[12], Fname[10], Lname[15], PHnum[17], choice, code;
    
        printf("APPAREL CODE    APPAREL TYPE      MINIMUM ORDER QUANTITY      COST OF ITEM  \n");
        printf("    T           T-SHIRT                     25                     $320     \n");
        printf("    B           BAGS                        10                     $250     \n");
        printf("    C           CAPS                        25                     $170     \n");
        printf("\nENTER CODE CORRESPONDING TO THE TYPE OF APPAREL DESIRED: ");
        fflush(stdin);
        scanf(" %c",&code);
    
    
        switch(code)
        {
            case 'T':
            case 't':
                printf("ENTER QUANTITY OF T-SHIRT YOU WISH TO ORDER: ");
                fflush(stdin);
                scanf(" %d", &quantity);
    
               printf("DO YOU WANT ANY DESIGN? Y/N: ");
                fflush(stdin);
                scanf(" %c", &choice); // whence you answer this question
                system("cls"); // it no longer has any reason to stay in the switch
            // other than finishing your totaling of values in your block of code 
         // in this case part, then your if staments fail as well for being able to 
        // to check that answer so it  then drops
         // down to your    if(test==1) check then finishes just like
       // it is being told to. your code works, because it is doing
      // what you are telling it to do, it is your logic that needs fixing to make
    // code do what you want it to do. Not what you're presently telling it to do. 
    
                if ((quantity>=25) && (quantity<50) && (tolower(choice) =='y') ) //|| (choice=='Y'))
                {
                cost = quantity * 320 + MOULD;  // here choice = y so I get to see no choice of design to select same.
                discount = 0.0;
                GCT = 0.165 * cost;
                total = cost + GCT;
                test= test+1;
                }
                else if ((quantity>50) && (tolower(choice) =='y') ) // || (choice=='Y'))
                {
                    cost = quantity * 320 + MOULD;
                    discount = cost * 0.05;
                    Dis_Prize = cost - discount;
                    GCT = 0.165 * Dis_Prize;
                    total = Dis_Prize + GCT;
                    test= test+1;
                }
                else if ((quantity>=25) && (quantity<50) && (tolower(choice) =='n') ) //|| (choice=='n'))
                {
                    cost = quantity * 320; 
                    discount = 0.0;
                    GCT = 0.165 * cost;
                    total = cost + GCT;
                    test= test+1;
                }
                else if((quantity>50) && (tolower(choice) =='n') ) // || (choice=='N'))
                {
                    cost = quantity * 320;
                    discount = cost * 0.05;
                    Dis_Prize = cost - discount;
                    GCT = 0.165 * Dis_Prize;
                    total = Dis_Prize + GCT;
                    test= test+1;
                }
                else if ( (choice!='y') && (choice!='Y') && (tolower(choice) !='n') ) // !='N') && (choice!='n'))
                {
                    printf("THE OPTIONS FOR DESIGN IS EITHER YES(Y) OR NO (N)\n");
                }
                else if (quantity < 25)
                    printf("YOU ARE NOT ALLOWED TO ORDER LESS THAN 25 SHIRTS\n");
                break; // BREAKS HERE
            case 'B':
            case 'b':
                printf("ENTER QUANTITY OF BAGS YOU WISH TO ORDER: ");
                scanf("%d",&quantity);
                system("cls");
                if(quantity>=10 && quantity<50)
                {
                    cost = quantity * 250 + 3500;
                    discount = 0.0;
                    GCT = 0.165 * cost;
                    total = cost - GCT;
                }
                else if (quantity>50)
                {
                    cost = quantity * 250 + 3500;
                    discount = cost * 0.05;
                    Dis_Prize = cost - discount;
                    GCT = 0.165 * Dis_Prize;
                    total = Dis_Prize - GCT;
                }
                else
                    printf("You are NOT allowed to order less than 10 BAGS\n");
                break;
            case 'C':
            case 'c':
                printf("ENTER QUANTITY OF CAPS YOU WISH TO ORDER: ");
                scanf("%d",&quantity);
                system("cls");
                if(quantity>=25 && quantity<50)
                {
                    cost = quantity * 170 + 3500;
                    discount = 0.0;
                    GCT = 0.165 * cost;
                    total = cost - GCT;
                }
                else if (quantity>50)
                {
                    cost = quantity * 170 + 3500;
                    discount = cost * 0.05;
                    Dis_Prize = cost - discount;
                    GCT = 0.165 * Dis_Prize;
                    total = Dis_Prize - GCT;
                }
                else
                    printf("You are NOT allowed to order less than 25 CAPS\n");
                break;
            default:
                printf("INVALID INPUT\n");
                }
    
            if(test==1)
            {
                printf("\nSUMMARY OF OUR TRANSACTION\n");
                printf("APPAREL CODE YOU SELECTED: %c\n", code);
                printf("ORDER QUANTITY: %d\n", quantity);
                printf("CUSTOMER'S NAME: %s %s\n", Fname, Lname);
                printf("CUSTOMER'S COMPANY: %s\n", ComName);
                printf("CUSTOMER'S PHONE NUMBER: %s\n", PHnum);
                printf("VALUE OF ORDER IS: $%f\n", cost);
                printf("DISCOUNT: $%f\n", discount);
                printf("GCT: $%f\n",GCT);
                printf("TOTAL AMMOUNT TO BE PAID IS: $%f\n", total);
    
            }
            else
                printf("GOODBYE");
        return 0;
    }
    results:
    Code:
    userx@slackwhere:~/bin
    $ ./quantity
    APPAREL CODE    APPAREL TYPE      MINIMUM ORDER QUANTITY      COST OF ITEM  
        T           T-SHIRT                     25                     $320     
        B           BAGS                        10                     $250     
        C           CAPS                        25                     $170     
    
    ENTER CODE CORRESPONDING TO THE TYPE OF APPAREL DESIRED: t
    ENTER QUANTITY OF T-SHIRT YOU WISH TO ORDER: 34
    DO YOU WANT ANY DESIGN? Y/N: y
    sh: cls: command not found
    
    SUMMARY OF OUR TRANSACTION
    APPAREL CODE YOU SELECTED: t
    ORDER QUANTITY: 34
    CUSTOMER'S NAME:  
    CUSTOMER'S COMPANY: 
    CUSTOMER'S PHONE NUMBER:     
    VALUE OF ORDER IS: $14380.000000
    DISCOUNT: $0.000000
    GCT: $2372.699951
    TOTAL AMMOUNT TO BE PAID IS: $16752.699219
    It'd might help if you write down your logic first in the steps of what you need to take place first then write your code to reflect same. logic board,
    Code:
    make menus
    print menu(s)
    
    get input
    if input good
    do this
    if bad do that / ask again // requirement of a loop to get it to go back to asking again? 
    (which logic could look like this)
    
    get input
    if ( input != to what I want)
     ask again
    else
    store input, then 
    go to next line (operation to get more input) 
    
    next line:
     get more input
    if ( input != to what I want)
     ask again
    else
    store input then
    go to next line (operation to get more input)
    
    next line (operation to get more input)
    //maybe
    check to see if user wants to change mind, 
    if ( input == changed mine)
    start all  over again
    else 
    total, print out totals. 
    
    what would be the default behavior, will their be any default behavior to determine how to
     use the default check in a switch, do I even need a switch? if yes, how many?
    what is required to get that done, a loop maybe? if yes how many and what type or types of loops would I need?
    how many variables do I need?
    what types of data will I be using, int char and any variation of same?

    something like that perhaps.
    the using of the key words continue and break in a loop, they are their for a reason. using break to get out of a loop when the conditions are present
    is valid coding.
    Last edited by userxbw; 10-23-2017 at 09:11 AM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you may have disappeared , even forgotten you posted this question.

    but this is what I came up with, I am a good 99.999999999 & sure their is a better way to do menus then this way, as I do not do menus in this manner, or I'd a posted ( a much better one) it.

    this might help you to learn to deal with loops and getting information within that same said loop, and a better understanding of switches. it is a shell of a program so yes it needs lots more before it can be said to be compete.
    Code:
    //#include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    //#include <conio.h>
    #include <ctype.h> // tolower
    //#include <math.h>
    
    #define MOULD 3500
    
    int main()
    {
        //int quantity, test=0;
        //float cost, discount, Dis_Prize, total, GCT;
        //char ComName[12], Fname[10], Lname[15], PHnum[17], choice, code;
        int flag = 0, tee_shirts = 0, what = 0, ducks = 1, frogs = 1,puppies = 1, amount_bags = 0;
        char y_n = '\0';
    
        while (flag == 0)
        {
        printf("APPAREL CODE    APPAREL TYPE      MINIMUM ORDER QUANTITY      COST OF ITEM  \n");
        printf("    T           T-SHIRT                     25                     $320     \n");
        printf("    B           BAGS                        10                     $250     \n");
        printf("    C           CAPS                        25                     $170     \n");
        printf("\nENTER CODE CORRESPONDING TO THE TYPE OF APPAREL DESIRED: ");
        scanf(" %c",&y_n);
    
        if ( tolower(y_n) == 't')
        {
            printf("how many?\n");
            scanf("%d",&tee_shirts);
        }
        getchar();
        printf("Would you like a design? y/n\n");
            scanf("%c", &y_n);
    
        if (tolower(y_n) == 'y')
        {
            printf("pick a selection\n"
                    "1. ducks\n"
                    "2. frogs\n"
                    "3. puppies\n"
                    "enter n for no selection\n");
            scanf("%d",&what);
            //doesn't need to be taken care of like this/
            // just showing the use of  switch's.
            // Then number is asinged to 'what' depending
            // on that number you can deal with it at the end as well
            //using the same varitable, depends on how many different orders
            //you're dealing with at the same time, perhaps. this does not really have much effect other than 
            // showing you how to use a switch. with the means to keep tally. 
            switch(what)
            {
                case 1:
                    ducks += ducks;
                    break;
                case 2:
                    frogs += frogs;
                    break;
                case 3:
                    puppies += puppies;
                    break;
                default: // no default behvior,just break it.
                    break;
            }//end switch
    
        }
        getchar();
        printf("so you want a get a bag?\n");
        scanf("%c", &y_n);
    
        if(tolower(y_n) == 'y')
        {
    
            printf("how many bags?\n");
            scanf("%d", &amount_bags);
        }
    
            getchar();
            printf("would you like to change your order? y/n\n");
            scanf("%c",&y_n);
            if (tolower(y_n) == 'y')
            {
                continue;
            }
            else
            flag = 1;
        }//end while
    
        printf("tee shirts ordered: %d\n", tee_shirts);
        printf("type of tee shirt\n");
        switch(what)
        {
            case 1:
                printf("ducks amount %d\n", ducks);
                break;
            case 2:
                printf("frogs amount %d\n", frogs);
                break;
            case 3:
                printf("puppies amount %d\n",puppies);
                break;
            default:
                printf("you did not specify a\n " // if no selection, default
                "type of tee shirt\n"); // behavior here is to let someone know.
                break;
            }
    printf("\nyou ordered %d bags\n", amount_bags);
    
        return 0;
    }
    Last edited by userxbw; 10-23-2017 at 04:44 PM.

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    5
    i want to say thanks to everyone for the help. I REALLY appreciate everything. However i used a string compare to solve my problem and modify my program a little.
    if (quantity>=25 && quantity<=50)
    {
    if ((strcmp(choice, "Y") == 0) || (strcmp(choice, "y") == 0))
    {
    I learnt a lot just by reading the help I get from you guys. I really appreciate it again. Thank you all

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    5
    I think if i had implement this
    if( tolower(y_n) == 't')
    everything would have worked out good

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Case Sensitivity
    By bernt in forum General Discussions
    Replies: 6
    Last Post: 10-17-2011, 02:12 PM
  2. Case Sensitivity Help!!!
    By Master Ali in forum C Programming
    Replies: 3
    Last Post: 01-21-2011, 04:38 AM
  3. Case Sensitivity... matching words in a string.
    By braden87 in forum C Programming
    Replies: 5
    Last Post: 03-13-2010, 11:04 PM
  4. case sensitivity in GtkTextView
    By MK27 in forum Linux Programming
    Replies: 2
    Last Post: 08-07-2008, 03:16 PM
  5. strcmp without case sensitivity
    By AmazingRando in forum C Programming
    Replies: 8
    Last Post: 03-16-2005, 08:40 AM

Tags for this Thread