Hi folks,
I'm still trying to master the basics of C & have just written by far the longest program that I've ever written. To most people here it probably isn't that long, but to me it is You see, a lot of the programs I've written are conversion programs (fahrenheit > celcius, bits > Bytes, etc...). So I was thinking, rather than having to remember what I named each of these programs when I want to call them up in the terminal, I could make a program that combines them all, by asking the user what type of conversion they want to do, and then have the program branch off into said type of conversion using the "switch" thing.
I got my program to work just how I want it to, and it even accounts for bad user input , but I'm wondering if there's a way to like, compress it to improve readability. Right now I only have comments in the code to deal with that, but maybe there's a way to put the code into each case into a function or something?
Here is the program:
Code:// // conversion.c // // all in one conversion program // // copyright 2013, script_kitteh // #include <stdio.h> int main () { int conversion_type; // variables seperated on different lines by category for clarity int fahr, cel; int b, mb, kb, Byte, kB, mB, gB; int miles, kilometers; char term; printf ( " \nWhat type of conversion would you like to do? "); printf ( "\n\n1. Fahrenheit > Celcius" ); printf ( "\n2. Celcius > Fahrenheit" ); printf ( "\n3. bits > Bytes" ); // programs 3-5 do not allow for negative integers. printf ( "\n4. Miles > Kilometers" ); printf ( "\n5. Kilometers > Miles\n\n"); if ( scanf ( "%i%c", &conversion_type, &term ) != 2 || term != '\n' ) { // program terminates here if anything other than 1-5 is entered. printf ( "\nPlease enter a number, 1-5.\n" ); } else { switch ( conversion_type ) { case 1: // Fahrenheit > Celcius printf ( "\nWhat degree Fahrenheit do you want to know in Celcius? "); if ( scanf ( "%i%c", &fahr, &term ) !=2 || term != '\n' ) { printf ( "\nThis program requires that you enter an integer.\n" ); } else { cel = ( fahr-32 ) / 1.8; printf ( "\n%i degrees Fahrenheit is %i degrees Celcius.\n", fahr, cel ); } break; case 2: // Celcius > Fahrenheit printf ( "\nWhat degrees Celcius do you want to know in Fahrenheit? " ); if ( scanf ( "%i%c", &cel, &term ) !=2 || term != '\n' ) { printf ( "\nThis program requires that you enter an integer.\n" ); } else { fahr = cel * 1.8 + 32; printf ( "\n%i degrees Celcius is %i degrees Fahrenheit.\n", cel, fahr ); } break; case 3: // bits > Bytes (assumes input is in megabits, as this is most commonly used in advetisements for isp speeds) printf ( "\nHow many mb do you need converted? " ); if ( scanf ( "%i%c", &mb, &term ) != 2 || term != '\n' || mb < 0 ) { printf ( "\nThis program requires that you enter a positive integer.\n" ); } else { kb = mb * 1024; b = kb * 1024; Byte = b / 8; kB = Byte / 1024; mB = kB / 1024; gB = mb / 1024; if ( kB < 1024 ) { printf ( "\n%i mbps is %i kBps\n", mb, kB ); } else { printf ( "\n%i mbps is %i mBps\n", mb, mB ); } } break; case 4: // Miles > Kilometers printf ( "\nHow many miles do you want to know in kilometers? " ); if ( scanf ( "%i%c", &miles, &term ) != 2 || term != '\n' || miles < 0 ) { printf ( "\nThis program requires that ou enter a positive integer.\n" ); } else { kilometers = miles * 1.6; printf ( "\n%i miles is %i kilometers.\n", miles, kilometers ); } break; case 5: // Kilometers > Miles printf ( "\nHow many kilometers do you want to know in miles? " ); if ( scanf ( "%i%c", &kilometers, &term ) != 2 || term != '\n' || kilometers < 0 ) { printf ( "\nThis program requires that you enter a positive integer.\n" ); } else { miles = kilometers * .6; printf ( "\n%i kilometers is %i miles.\n", kilometers, miles ); } break; // No default case because the surrounding if statement already takes care of bad input. } } printf ( "\n" ); return 0; }