Thread: Can't figure out what's causing this

  1. #1
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74

    Unhappy Can't figure out what's causing this

    Hi folks,

    A long time ago I posted a program that I made on this forum about conversions, and I was asking how it could be cleaned up. Several people suggested functions, but I wasn't knowledgeable enough back then to implement them. I've (almost) finally re-written the program with a bunch of functions, but there is this one bug that I cannot figure out.

    I have it set up so the user is presented with a menu of conversion choices, and then the program enters a switch/case for whatever conversion the user wants to do. After the conversion is done, I intended for the program to start over, giving the user the option to do another conversion or quit, which it does, but for some reason for the second iteration of the program, no matter what the user enters, the program says it's invalid input. Then the user is given the option to input again (working as intended), but this 3rd time, the input will work, if within the parameters I set.

    Here is an example of this problem: http://i.imgur.com/fsxNJjb.png?1?5604

    (and by the way, that pattern keeps happening, with every other input returning the error message)

    The program:

    Code:
    #include <stdio.h>
    char get_choice(void);
    char get_first(void);
    int get_int(void);
    int f2c(int input);
    int c2f(int input);
    int b2B(int input);
    int m2k(int input);
    int k2m(int input);
    int main(void)
    {
        int choice;
        int answer;    
        int fahr, cel, mb, miles, kilometers;
    
    
    
    
        while((choice = get_choice()) != 'q')
        {
            switch(choice)
            {
                case '1': printf("\nWhat degree Fahrenheit do you want to know in Celcius? ");
                      fahr = get_int();
                      f2c(fahr);
                      break;
                case '2': printf("\nWhat degree Celcius do you want to know in Fahrenheit? ");
                      cel = get_int();
                      c2f(cel);
                      break;
                case '3': printf("\nHow many mb do you need converted? ");
                      mb = get_int();
                      b2B(mb);
                      break;
                case '4': printf("\nHow many miles do you want to know in kilometers? " );
                      miles    = get_int();
                      m2k(miles);
                      break;
                case '5': printf("\nHow many kilometers do you want to know in miles? ");
                      kilometers = get_int();
                      k2m(kilometers);
                      break;
                default: printf("\nProgram error!\n");
                     break;
            }
        }
        printf("\nBye.\n");
        
        return 0;
    }
    
    
    char get_choice(void)
    {
        int ch;
    
    
        printf("\nEnter the number of your choice:\n");
        printf("\n1. Fahrenheit > Celcius\t2. Celcius > Fahrenheit\n");
        printf("3. bits > Bytes\t4.Miles > Kilometers\n");
        printf("5. Kilometers > Miles\tq. quit\n\n");
    
    
        ch = get_first();
        
        while ((ch < '1' || ch > '5') && ch != 'q')
        {
            printf("\nPlease respond with 1, 2, 3, 4, 5 or q.\n");
            ch = get_first();
        }
        
        return ch;
    }
    
    
    char get_first(void)
    {
        int ch;
        
        ch = getchar();
        while(getchar() != '\n')
            continue;
        
        return ch;
    }
    
    
    int get_int(void)
    {    
        int input;
        char ch;
    
    
        while (scanf("%d", &input) != 1)
        {
            while((ch = getchar()) != '\n')
                printf("\n");
                putchar(ch);
            printf(" is not an integer.\nPlease inter an ");
            printf("integer value, such as 25, -178 or 3:");
        }
        
        return input;
    }
    
    
    int f2c(int input)
    {
        int answer;
    
    
        answer = ( input-32 ) / 1.8;
        printf("\n%d degrees Fahrenheit is %d degrees Celcius.\n", input, answer);
    
    
        return answer;
    }
    
    
    int c2f(int input)
    {
        int answer;
    
    
        answer = input * 1.8 + 32;
        printf("\n%d degrees Celcius is %d degrees Fahrenheit.\n", input, answer);
    
    
        return answer;
    }
    
    
    int b2B(int input)
    {
        int answer;
    
    
        answer = ((((input * 1024 * 1024) / 8) / 1024) / 1024);
        if((((input * 1024 * 1024) / 8) / 1204) < 1024)
        {
            answer = (((input * 1024 * 1024) / 8) / 1204);
            printf("\n%d mbps is %d kBps\n", input, answer);
        }
        else
        {
            printf("\n%d mbps is %d mBps\n", input, answer);
        }
        
        return answer;
    }
    
    
    int m2k(int input)
    {
        int answer;
    
    
        answer = input * 1.6;
        printf("\n%d miles is %d kilometers.\n", input, answer);
    
    
        return answer;
    }
    
    
    int k2m(int input)
    {
        int answer;
        
        answer = input *.6;
        printf("\n%d kilometers is %d miles.\n", input, answer);
    
    
        return answer;
    }
    Any ideas why on earth it's doing that? Judging by the error message, I imagine it's some issue with the get_choice() function, or the get_first() function within that... but I can't figure it out
    Last edited by SCRIPT_KITTEH; 07-27-2013 at 04:16 PM. Reason: something was commented out that I didn't mean to be

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's just the newline being left in the buffer.
    Try uncommenting
    Code:
        while(getchar() != '\n')
            ;
    in get_first and also add it just before the return in get_int.

    BTW, celsius is spelled with an s after the l, not a c.

  3. #3
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by oogabooga View Post
    It's just the newline being left in the buffer.
    Try uncommenting
    Code:
        while(getchar() != '\n')
            ;
    in get_first and also add it just before the return in get_int.

    BTW, celsius is spelled with an s after the l, not a c.
    AHHH, I love you! Yeah, the commented part was left over from something I was trying to fix the original problem, but adding those two lines into the get_int function did the trick! Thank you so much! So it's like, because get_int() used scanf, it ignored the /n, and that was just left in the std in stream, so it was the first thing the get_choice() picked up when it was called again?

    Oh, and thanks for letting me know about the spelling, too
    Last edited by SCRIPT_KITTEH; 07-27-2013 at 04:30 PM. Reason: grammar

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by SCRIPT_KITTEH View Post
    because get_int() used scanf, it ignored the /n, and that was just left in the stdin stream, so it was the first thing the get_choice() picked up when it was called again?
    That's exactly right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What might be causing this ?
    By Stiopa in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2010, 05:44 AM
  2. What is causing WM_PAINT to be sent?
    By DL1 in forum Windows Programming
    Replies: 8
    Last Post: 10-08-2009, 10:12 PM
  3. What is causing this?
    By caduardo21 in forum C Programming
    Replies: 3
    Last Post: 05-26-2005, 11:04 AM
  4. ad-hoc link causing loop
    By Waldo2k2 in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-11-2005, 10:09 PM
  5. Function causing error
    By smitsky in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2004, 07:01 PM