Ok so I'm going to start at the beginning since that would be easier, might be a little long but please bear/bare (whatever) with me.

Ok so it's Christmas break now and before Christmas break my professor says to the class "Please for the love of god write some code, program something, just don't veg out and forget everything we have learned so far...Not required but for your own good he says!" So I got bored and decided to write a program to calculate all the equations for my DC/AC theory class...well the ones I can remember off the top of my head anyway. The code works beautifully so far, it's a bit long and I know there is probably a better/cleaner way to do it, I just haven't figured it out yet, but I will.

The program has 10 choices (or equations) to choose from, I want it to do this.

user input choice 0-9

enter variables

do calculations

show results

ask if user wants to do another calculation (choose another option)

or exit

If choose another option type more

program restarts back at beginning

if exit type exit

program exits

*****Edit*****

Win32 Console

Pelles C

Windows OS (8.1)

*****End Edit*****

So with no more waiting, here is my code; cringes a little:

Feel free to comment, complain, criticize, etc, not going to offend me as I am learning still it's all a learning experience. So have at it.Code:#include <stdio.h> #include <stdlib.h> #include <math.h> //For the funky math if I decide to figure out how to use the functions within #include <windows.h> //Used to color the final result of each case (option from menu) int main(void) { HANDLE hConsole; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); char ch; float voltage, current, resistance, result, result2, force, distance, watts, time, kw, ctime, ampere_hours, rating, pout, pin; /*main menu--Pick an option and let the fun begin, er calculations!*/ SetConsoleTextAttribute(hConsole, 012); printf("What part of Ohm's law shall we calculate?\n\n"); printf("|--------------------------------------------------------------------|\n"); printf("| Please choose one of the three options below: |\n"); printf("| |\n"); printf("| Choose 1 for Voltage Calculation |\n"); printf("| Choose 2 for Current Calculation |\n"); printf("| Choose 3 for Resistance Calculation |\n"); printf("| Choose 4 for Energy Calculation |\n"); printf("| Choose 5 for kWh (killoWatthours) Calculation |\n"); printf("| Choose 6 for Power Formula P=(I*2)*R |\n"); printf("| Choose 7 for Power Formula P=V*I |\n"); printf("| Choose 8 for Power Formula P=(V*2)/R |\n"); printf("| Choose 9 for Battery life rating |\n"); printf("| Choose 0 for Power Supply Efficiency and Power Loss |\n"); printf("|--------------------------------------------------------------------|\n\n"); SetConsoleTextAttribute(hConsole, 07); printf("Choose an option or press enter to end program!\n"); scanf("%c", &ch); //Haven't figured out how to go past 0; as in double digit ex. 10, 11, 12, etc. switch (ch) { case '1': //Option 1 from menu, Voltage Calculation SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (AMPS).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f",¤t); //Capture first value typed on keyboard SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (RESISTANCE) in Ohms.\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f",&resistance); //Capture second value typed on keyboard result = current * resistance; //Do the math of the two values captured SetConsoleTextAttribute(hConsole, 014); //Change the sum of the math calculation to red printf("The voltage is %0.2f in volts.\n",result); //Show the sum of the math calculation SetConsoleTextAttribute(hConsole, 07); break; //Rinse and repeat for each option in the menu case '2': SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (VOLTS).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &voltage); SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (RESISTANCE) in Ohms.\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &resistance); result = voltage / resistance; SetConsoleTextAttribute(hConsole, 04); printf("The Current is %0.2f amps.\n",result); SetConsoleTextAttribute(hConsole, 07); break; case '3': SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (VOLTS).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &voltage); SetConsoleTextAttribute(hConsole, 06); printf("Enter Second value (AMPS).\n"); scanf("%f", ¤t); result = voltage / current; SetConsoleTextAttribute(hConsole, 04); printf("The Resistance is %0.2f ohm's.\n",result); SetConsoleTextAttribute(hConsole, 07); break; case '4': SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (FORCE).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &force); SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (DISTANCE).\n"); scanf("%f", &distance); result = force * distance; SetConsoleTextAttribute(hConsole, 04); printf("The Energy is %0.2f (W).\n",result); SetConsoleTextAttribute(hConsole, 07); break; case '5': SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (WATTS).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &watts); SetConsoleTextAttribute(hConsole, 06); result = watts / 1000; //Divide the first captured value by a specific number ie. 1000 SetConsoleTextAttribute(hConsole, 04); printf("This is the WATTS in kW %0.2f.\n",result); SetConsoleTextAttribute(hConsole, 06); printf("Enter the value of kW.\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &kw); SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (TIME).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &time); result = time / 60; //Divide the first captured value by a specific number ie. 60 SetConsoleTextAttribute(hConsole, 04); printf("This is the corrected time value %0.2f\n",result); SetConsoleTextAttribute(hConsole, 06); printf("Enter the corrected time value.\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &ctime); result = kw * ctime; SetConsoleTextAttribute(hConsole, 04); printf("The total kWh is %0.2f\n",result); SetConsoleTextAttribute(hConsole, 07); break; case '6': SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (CURRENT).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", ¤t); SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (RESISTANCE).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &resistance); result = (current * current) * resistance; //The math starts getting funky here SetConsoleTextAttribute(hConsole, 04); printf("The Power dissipated in (W) is %0.2f\n",result); SetConsoleTextAttribute(hConsole, 07); break; case '7': SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (CURRENT).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", ¤t); SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (VOLTAGE).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &voltage); result = current * voltage; SetConsoleTextAttribute(hConsole, 04); printf("The Power dissipated in (W) is %0.2f.\n",result); SetConsoleTextAttribute(hConsole, 07); break; case '8': SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (VOLTAGE).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &voltage); SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (RESISTANCE).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &resistance); result = (voltage * voltage) / resistance; //And again here SetConsoleTextAttribute(hConsole, 04); printf("The Power dissipated in (W) is %0.2f\n",result); SetConsoleTextAttribute(hConsole, 07); break; case '9': SetConsoleTextAttribute(hConsole, 06); printf("Enter first Value (CURRENT).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", ¤t); SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (AMPERE-HOURS).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &ere_hours); result = current * ampere_hours; //Do the math of the first two inputs captured SetConsoleTextAttribute(hConsole, 04); printf("The battery RATING is %0.2f\n",result); SetConsoleTextAttribute(hConsole, 06); printf("Enter RATING value here.\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &rating); result = rating / current; //Capture the result (sum) of the first two results and do more math result2 = result / current; //Here is where we combine the first result with the current for a second result SetConsoleTextAttribute(hConsole, 04); printf("This is the time the battery will last %0.2f hours\n",result2); //Output the second result SetConsoleTextAttribute(hConsole, 07); break; case '0': SetConsoleTextAttribute(hConsole, 06); printf("Enter first value (POWER SUPPLY OUTPUT).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &pout); SetConsoleTextAttribute(hConsole, 06); printf("Enter second value (POWER SUPPLY INPUT).\n"); SetConsoleTextAttribute(hConsole, 07); scanf("%f", &pin); result = pout / pin * 100; //Do some division and multiply by specific integer ie. 100 SetConsoleTextAttribute(hConsole, 04); printf("This is the Power Supply Efficiency %0.2f%%\n",result); result2 = pin - pout; printf("This is the Power Loss of the Power Supply is %0.2f Watts\n",result2); SetConsoleTextAttribute(hConsole, 07); //Change color back to standard console output break; default : //Default to nothing when no option is chosen SetConsoleTextAttribute(hConsole, 07); exit(0); //Exit the program break; } system("pause"); //Don't like this option, but it's the only thing I could think of to keep the window open return (0); //Return memory resources used back to the computer and shutdown program }

Tweet This+ 1 thisPost To Linkedin