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