Thread: If Else using char

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    20

    If Else using char

    Can't get this to work properly. Output is at the bottom of what i am getting if i try to type either "p" or "s"

    Code:
    void menu(void) 
    { 
        char selection[30]; 
         
        fflush(stdin); 
        puts("\nPlease select one of the following:"); 
        printf("\n\np = Parallel Resistance"); 
        printf("\ns = Series Resistance"); 
        printf("\n\n>"); 
        scanf("%c", &selection); 
     
        if (selection =='p') 
            parallel(); 
        else if (selection =='s') 
            series(); 
        else 
        { 
            printf("\nInvalid selection. Press any key to exit, then relaunch the program"); 
        } 
    }
    
    /* 
    This program will calculate either PARALLEL RESISTANCE 
    or SERIES RESISTANCE using 2 specified resistor values 
     
    Please select one of the following 
     
     
    p = Parallel Resistance 
    s = Series Resistance 
     
    >p 
     
    Invalid selection. Press any key to exit, then relaunch the program 
     
    */

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're reading a single character, you don't need an array. That is only if you plan on using a string, which you're not. So make "selection" a simple char variable.

    If you didn't get warnings from your compiler about this, you need to increase the warning levels.

    Code:
    fflush(stdin);
    Flushing the input buffer this way is undefined behavior.

    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    FAQ > Flush the input buffer - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    And main-function should return an integer.
    Code:
    int main (void) {
    
        // code here
    
        return 0;
    }
    Other have classes, we are class

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    20
    Thanks for your responses but that still doesn't solve my problem and maybe i wasn't clear about what my problem actually is so sorry about that. When i compile the program, everything pops up fine. The problem is when i go to make a selection, imputing "p" or "s" both result in the program giving me the "invalid" statement specified in the if else statement and won't run the parallel or series function.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Re-read the first line of my response above. That should fix the problem (or so I assume based on what you've posted). Try implementing the change - if it's still not working, post your updated code.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    20
    Im such a noob...thanks it did indeed help. i for some reason i thought when you said change it to a simple char value, i thought you meant 30 to 1...then after not working again said to myself what if i just remove that altogether and now its working. Thanks again ^_^

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're very welcome. Glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  2. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  3. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  4. undefined reference to `RunSwmmDll(char*, char*, char*)'
    By amkabaasha in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2011, 12:33 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM