Like Tree2Likes
  • 1 Post By CommonTater
  • 1 Post By quzah

if statement not working Properly

This is a discussion on if statement not working Properly within the C Programming forums, part of the General Programming Boards category; In my program I have 4 options 1. enter letter and number combination 2. display all combination in alphabetical order ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    if statement not working Properly

    In my program I have 4 options
    1. enter letter and number combination
    2. display all combination in alphabetical order
    3. look up specific letter and number combination by entering in letter only it should display letter number combo
    4. quit

    my problem is when I enter in data by clicking 1 then entering letter and number combination lets say a55 and then hit 3 to look up specific letter and then enter in a. instead of seeing a55, it will loop back to menu like in photo attached
    How do I make it so that it displays a55 when I enter in a?

    Code:
    #include <stdio.h>
    #include "string.h"
    
    
    void addCode(char, int,char[],int[]);
    
    
    int main (void)
    {
        
        int input=0;
        char codeLetter[26] = {'\0'};
        int codeNumber[26]={0};
        char temp;
        int tempNumber;
        int index = 0;
        int i;
       int quit=1;
       int pass; 
       int letter;
    
    
       while (quit != 0){
        printf("************************************************************\n");
        printf("'1' = Enter CODE/UNIT Combination\n'2' = Display The Contents of ALL CODE/UNIT Values Entered\n");
        printf ("'3' = Look Up Single Code\n'4' = Quit");
            printf("\n************************************************************\n");
        scanf("%d", &input);
    
    
           switch (input){//beginning switch
                case 1:
                     fflush(stdin);
    printf("Please Enter CODE/UNIT Combination\n");
    scanf ("%c%d", &temp,&tempNumber);
    addCode(temp, tempNumber, codeLetter, codeNumber);
    
    
                    break;
                
                case 2:  
                    for (i=0;i<26;i++){
                    printf("%c %d\n", codeLetter[i],codeNumber[i]);
    
    
                    
                    
                    }//end for loop
                     
                         
               break;
               case 3:
                   fflush(stdin);
                 printf("Please Enter The Letter You Want To Look Up\n");
                  scanf ("%c", &letter);
                 if (letter=='a'){
                 printf("%c%d\n", codeLetter[0],codeNumber[0]);
                   }
                    if (letter=='b'){
                 printf("%c%d\n", codeLetter[1],codeNumber[1]);
                   }
                    if (letter=='c'){
                 printf("%c%d\n", codeLetter[2],codeNumber[2]);
                   }
                    if (letter=='d'){
                 printf("%c%d\n", codeLetter[3],codeNumber[3]);
                   } if (letter=='e'){
                 printf("%c%d\n", codeLetter[4],codeNumber[4]);
                   }
                    if (letter=='f'){
                 printf("%c%d\n", codeLetter[5],codeNumber[5]);
                   } if (letter=='g'){
                 printf("%c%d\n", codeLetter[6],codeNumber[6]);
                   }
                    if (letter=='h'){
                 printf("%c%d\n", codeLetter[7],codeNumber[7]);
                   } if (letter=='i'){
                 printf("%c%d\n", codeLetter[8],codeNumber[8]);
                   }
                    if (letter=='j'){
                 printf("%c%d\n", codeLetter[9],codeNumber[9]);
                   } if (letter=='k'){
                 printf("%c%d\n", codeLetter[10],codeNumber[10]);
                   }
                    if (letter=='l'){
                 printf("%c%d\n", codeLetter[11],codeNumber[11]);
                   }
                    if (letter=='m'){
                 printf("%c%d\n", codeLetter[12],codeNumber[12]);
                   }
                    if (letter=='n'){
                 printf("%c%d\n", codeLetter[13],codeNumber[13]);
                   }
                    if (letter=='o'){
                 printf("%c%d\n", codeLetter[14],codeNumber[14]);
                   }
                    if (letter=='p'){
                 printf("%c%d\n", codeLetter[15],codeNumber[15]);
                   } if (letter=='q'){
                 printf("%c%d\n", codeLetter[16],codeNumber[16]);
                   }
                    if (letter=='r'){
                 printf("%c%d\n", codeLetter[17],codeNumber[17]);
                   } if (letter=='s'){
                 printf("%c%d\n", codeLetter[18],codeNumber[18]);
                   }
                    if (letter=='t'){
                 printf("%c%d\n", codeLetter[19],codeNumber[19]);
                   } if (letter=='u'){
                 printf("%c%d\n", codeLetter[20],codeNumber[20]);
                   }
                    if (letter=='v'){
                 printf("%c%d\n", codeLetter[21],codeNumber[21]);
                   } if (letter=='w'){
                 printf("%c%d\n", codeLetter[22],codeNumber[22]);
                   }
                    if (letter=='x'){
                 printf("%c%d\n", codeLetter[23],codeNumber[23]);
                   } if (letter=='y'){
                 printf("%c%d\n", codeLetter[24],codeNumber[24]);
                   }
                    if (letter=='z'){
                 printf("%c%d\n", codeLetter[25],codeNumber[25]);
                   }
                    break;
                    case 4:
                         printf ("\nGoodbye\n");
                         quit = 0;
                    break;
    
    
                default:
    
    
                    printf("\nThat is not a valid entry:\n"); 
    
    
    
    
                }//End Switch
    }//end do while
       
        system ("pause");
        return 0;
        
        
        } 
    
    
    void addCode(char toAdd, int numAdd, char letter[],int numbers[]){
    int index;
        if(toAdd>90){
            toAdd=toAdd-32;
        }
        index=toAdd-65;
        letter[index]=toAdd;
        
        numbers[index]+=numAdd;
    
    
    }
    Attached Images Attached Images  
    Last edited by noname88; 09-29-2011 at 05:43 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    if statement not working Properly
    Dear god... did you break ANOTHER one?

    all this...
    Code:
    			   fflush(stdin);
    			 printf("Please Enter The Letter You Want To Look Up\n");
    			  scanf ("%c", &letter);
    			 if (letter=='a'){
    			 printf("%c%d\n", codeLetter[0],codeNumber[0]);
    			   }
    			    if (letter=='b'){
    			 printf("%c%d\n", codeLetter[1],codeNumber[1]);
    			   }
    			    if (letter=='c'){
    			 printf("%c%d\n", codeLetter[2],codeNumber[2]);
    			   }
    			    if (letter=='d'){
    			 printf("%c%d\n", codeLetter[3],codeNumber[3]);
    			   } if (letter=='e'){
    			 printf("%c%d\n", codeLetter[4],codeNumber[4]);
    			   }
    			    if (letter=='f'){
    			 printf("%c%d\n", codeLetter[5],codeNumber[5]);
    			   } if (letter=='g'){
    			 printf("%c%d\n", codeLetter[6],codeNumber[6]);
    			   }
    			    if (letter=='h'){
    			 printf("%c%d\n", codeLetter[7],codeNumber[7]);
    			   } if (letter=='i'){
    			 printf("%c%d\n", codeLetter[8],codeNumber[8]);
    			   }
    			    if (letter=='j'){
    			 printf("%c%d\n", codeLetter[9],codeNumber[9]);
    			   } if (letter=='k'){
    			 printf("%c%d\n", codeLetter[10],codeNumber[10]);
    			   }
    			    if (letter=='l'){
    			 printf("%c%d\n", codeLetter[11],codeNumber[11]);
    			   }
    			    if (letter=='m'){
    			 printf("%c%d\n", codeLetter[12],codeNumber[12]);
    			   }
    			    if (letter=='n'){
    			 printf("%c%d\n", codeLetter[13],codeNumber[13]);
    			   }
    			    if (letter=='o'){
    			 printf("%c%d\n", codeLetter[14],codeNumber[14]);
    			   }
    			    if (letter=='p'){
    			 printf("%c%d\n", codeLetter[15],codeNumber[15]);
    			   } if (letter=='q'){
    			 printf("%c%d\n", codeLetter[16],codeNumber[16]);
    			   }
    			    if (letter=='r'){
    			 printf("%c%d\n", codeLetter[17],codeNumber[17]);
    			   } if (letter=='s'){
    			 printf("%c%d\n", codeLetter[18],codeNumber[18]);
    			   }
    			    if (letter=='t'){
    			 printf("%c%d\n", codeLetter[19],codeNumber[19]);
    			   } if (letter=='u'){
    			 printf("%c%d\n", codeLetter[20],codeNumber[20]);
    			   }
    			    if (letter=='v'){
    			 printf("%c%d\n", codeLetter[21],codeNumber[21]);
    			   } if (letter=='w'){
    			 printf("%c%d\n", codeLetter[22],codeNumber[22]);
    			   }
    			    if (letter=='x'){
    			 printf("%c%d\n", codeLetter[23],codeNumber[23]);
    			   } if (letter=='y'){
    			 printf("%c%d\n", codeLetter[24],codeNumber[24]);
    			   }
    			    if (letter=='z'){
    			 printf("%c%d\n", codeLetter[25],codeNumber[25]);
    			   }
                    break;
    can be reduced to...

    Code:
         printf("Please Enter The Letter You Want To Look Up\n");
        scanf (" %c", &letter);
        letter = tolower(letter);
        printf("%c%d\n", codeLetter[letter -'a'],codeNumber[letter-'a']);
    And... as a bonus, this version is also case insensitive, it will work with A or a, with equal ease.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    That did not work. These are variables that have already been stored and could be any letter that was stored (see new pic) I'm not just using 'a', which is why I had an if statement. When I used your code it crashes after I hit the letter(a-z) I am looking up.

    updated
    Code:
    #include <stdio.h>#include "string.h"
    
    
    void addCode(char, int,char[],int[]);
    
    
    int main (void)
    {
        
        int input=0;
        char codeLetter[26] = {'\0'};
        int codeNumber[26]={0};
        char temp;
        int tempNumber;
        int index = 0;
        int i;
       int quit=1;
       int pass; 
       int letter;
    
    
       while (quit != 0){
        printf("************************************************************\n");
        printf("'1' = Enter CODE/UNIT Combination\n'2' = Display The Contents of ALL CODE/UNIT Values Entered\n");
        printf ("'3' = Look Up Single Code\n'4' = Quit");
            printf("\n************************************************************\n");
        scanf("%d", &input);
    
    
           switch (input){//beginning switch
                case 1:
                     fflush(stdin);
    printf("Please Enter CODE/UNIT Combination\n");
    scanf ("%c%d", &temp,&tempNumber);
    addCode(temp, tempNumber, codeLetter, codeNumber);
    
    
                    break;
                
                case 2:  
                    for (i=0;i<26;i++){
                    printf("%c %d\n", codeLetter[i],codeNumber[i]);
    
    
                    
                    
                    }//end for loop
                     
                         
               break;
               case 3:
                   fflush(stdin);
                
                printf("Please Enter The Letter You Want To Look Up\n");
    scanf (" %c", &letter);
    letter = tolower(letter);
    printf("%c%d\n", codeLetter[letter -'a'],codeNumber[letter-'a']);
                    break;
                    case 4:
                         printf ("\nGoodbye\n");
                         quit = 0;
                    break;
    
    
                default:
    
    
                    printf("\nThat is not a valid entry:\n"); 
    
    
    
    
                }//End Switch
    }//end do while
       
        system ("pause");
        return 0;
        
        
        } 
    
    
    void addCode(char toAdd, int numAdd, char letter[],int numbers[]){
    int index;
        if(toAdd>90){
            toAdd=toAdd-32;
        }
        index=toAdd-65;
        letter[index]=toAdd;
        
        numbers[index]+=numAdd;
    
    
    }
    Attached Images Attached Images  

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int index;
        if(toAdd>90){
            toAdd=toAdd-32;
        }
        index=toAdd-65;
        letter[index]=toAdd;
         
        numbers[index]+=numAdd;
    }
    There's a reason Tater didn't just use magic numbers.

    What's 166 - 32? (Hint: Bigger than 65 + 26).

    Also:
    Code:
               fflush(stdin);
    You don't flush a faucet.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    I don't know how
    Code:
    int index;
        if(toAdd>90){
           toAdd=toAdd-32;    }
        index=toAdd-65;    
    letter[index]=toAdd;       
    numbers[index]+=numAdd; }
    this is relevant? I just want to pull up using the letter the letter+number combination assigned Like if I enter a44 and enter 3 to look up Code/Unit Combination and enter a then a44 should display or if I enter c33 and enter 3 and enter c then c33 should appear etc... which is why I used the if statement because I can pull up the stored information using the letters place in the array set up
    Last edited by noname88; 09-29-2011 at 06:25 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't know what your screenshot is supposed to be telling us, because it looks like it work just fine according to your latest description.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Don't feel bad... I'm still trying to get a handle on what he's trying to do...

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The only thing I can figure is he wants to first initally set up his alphabet as numbers:

    a33
    b108
    c22
    ...
    z2

    Then he wants to be able to enter a letter, and have it give him the number:

    in: a
    out: 33

    Or he wants to be able to give it a number and have it get the letter:

    in: 33
    out: a

    I guess. I don't know what any of that has to do with his magic number math there though.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    It doesn't work because after I record a letter and number by using option 1 and choose option 3. As soon as I enter letter the program crashes
    The only thing I can figure is he wants to first initally set up his alphabet as numbers:

    a33
    b108
    c22
    ...
    z2

    Then he wants to be able to enter a letter, and have it give him the number:

    in: a
    out: 33

    Or he wants to be able to give it a number and have it get the letter:

    in: 33
    out: a

    I guess.
    I want it to not only display number but letter as well so after I enter 'a' it should output a33
    Attached Images Attached Images  
    Last edited by noname88; 09-29-2011 at 08:29 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right, so you were already told how to do this:
    Code:
    index = tolower( letter ) - 'a';
    value = storedhere[ index ];
    What's the problem?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by noname88 View Post
    I don't know how
    Code:
    int index;
        if(toAdd>90){
           toAdd=toAdd-32;    }
        index=toAdd-65;    
    letter[index]=toAdd;       
    numbers[index]+=numAdd; }
    this is relevant? I just want to pull up using the letter the letter+number combination assigned Like if I enter a44 and enter 3 to look up Code/Unit Combination and enter a then a44 should display or if I enter c33 and enter 3 and enter c then c33 should appear etc... which is why I used the if statement because I can pull up the stored information using the letters place in the array set up
    Code:
    void addCode(char toAdd, int numAdd, char letter[],int numbers[]){
    
        letter[tolower(toAdd) - 'a' ] = toAdd;
        numbers[tolower(toAdd) - 'a' ] = numAdd;
    }
    In all truth, with what you appear to be doing you don't even need the codeLetters array at all... everthing seems to be using the letter as an index and storing numbers... you could simplify your code enormously...
    quzah likes this.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To add onto what Tater said, and what my previous post says in a less clear manner, all you need is an array of 26 elements. Stick numbers in it, call it a day.


    Quzah.
    CommonTater likes this.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen not working properly?
    By darkmagic in forum C Programming
    Replies: 7
    Last Post: 05-15-2011, 11:42 PM
  2. Not Working properly
    By Tejas Sanap in forum C Programming
    Replies: 7
    Last Post: 05-10-2011, 06:52 AM
  3. if / else if / else statement not functioning properly
    By Beowolf in forum C Programming
    Replies: 8
    Last Post: 09-12-2007, 02:09 AM
  4. Replies: 5
    Last Post: 01-18-2006, 07:59 AM
  5. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 12:59 AM

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