#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Salt Lake City, UT, USA
    Posts
    6
    Rep Power
    0

    Exclamation [C] Multiple Choice with loop until exit is typed


    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:

    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",&current);								    //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", &current);
    			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", &current);
    				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", &current);
    				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", &current);
    				SetConsoleTextAttribute(hConsole, 06);
    			printf("Enter second value (AMPERE-HOURS).\n");
    				SetConsoleTextAttribute(hConsole, 07);
    				scanf("%f", &ampere_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
    
    }
    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.
    Last edited by F8LERROR; December 27th, 2013 at 02:46 PM. Reason: Forgot important information
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by F8LERROR
    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.
    For the love of God get to the point! :rolleyes:

    Is there a question in here somewhere or are you just asking for a code review?

    I would start with structuring this code into functions, perhaps a menu() function that displays the menu and accepts validates and returns user input, then separate functions for each equation. At the lower level a single function for accepting and validating input.

    Rather than a "big switch-case", I would implement this with a structure array where each element is a struct containing the menu text and function pointer. That way adding new equations is simply a case of adding a new element to the array, and implementing the necessary function; the menu presentation code then never needs to change.

    For your input function, all thet "enter first value, enter second value" stuff is unnecessary nonsense - the order of entry is irrelevant. Instead of "Enter first value (VOLTS)", why not just "Enter volts"? I suggest a function with an interface such as:
    Code:
    double getValue( const char* value_name, double min, double max ) ;
    Where value_name is the name of the parameter to input, e.g. "volts", "amps" etc., and min and max define a valid range for validation. If any value is valid, you could simply pass the values DBL_MIN and DBL_MAX from <limits.h>. Or for something even more generic, just:

    Code:
    double getValue( const char* prompt, double min, double max ) ;
    where prompt is the entire prompt string.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Salt Lake City, UT, USA
    Posts
    6
    Rep Power
    0

    Question Appreciative of the assistance


    As I am looking at the code and structure I see what you are saying and will be making the necessary adjustments to my code. But for your question as to if there was a question in my post, yes there was. I need the code to return to the "MENU" after user has made a choice and followed through or exit if they wish to exit. So program starts, user makes a choice, inputs variables for the specific equation, gets the results then chooses to return to menu or exit program. Not sure how to do this, I have been messing around with code for it but cannot seem to get it right and would be pulling my hair out if I had any but I don't cause it's all shaved off. So how do I get it to return to menu or exit depending on user input 1 to return to menu or 2 to exit or something like that.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,733
    Rep Power
    1959
    To repeat a code block you will have to use loop.
    In this case you should use a do-while-loop with an option to quit the program, e.g. "enter 'q' for quit".

    Would be something like this:
    Code:
    bool runAgain = true; // bool can be used with C99 
    do {
        // show menu and get user input, with quit option
    
       // switch option , with an additional case:
       // case 'q':  runAgain = fase; break; 
    
    }while(runAgain);
    If you use C99 or C++ you can use the bool statement, otherwise you will have to use it as an integer with the values 0 and 1.

    One comment about the call to SetConsoleTextAttribute using integer as parameter.
    You can take a look at the defined Character Attributes for this function.

    Personally, I try to avoid (or at least limit) the use of what is called Magic Numbers.

    Furthermore, I would either add a comment to explain the colour result or better yet define my own colour "mix". (It is easy to forget to change the comment when changing the parameter.)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Salt Lake City, UT, USA
    Posts
    6
    Rep Power
    0

    Question C99 and bool


    That worked great, thanks. I only had to include the stdbool.h file for it to know what the heck I was calling lol, but it worked great. Now I just have to restructure it as stated earlier so it doesn't look like a programming monster barfed up code.

    One thing I am trying to figure out though is the SetConsoleTextAttribute call and the FOREGROUND_BLAH and BACKGROUND_BLAH. Not sure how to incorporate them to do what they say they are going to do. I understand that not declaring a background just leaves it black which is what I want and that the INTENSITY call just says that the color is bright not dull.

    I tried just putting it above the code like this
    Code:
    	FOREGROUND_GREEN | FOREGROUND_INTENSITY;
    	printf("What part of Ohm's law shall we calculate?\n\n");
    and it will compile like that with no error's or warnings but it doesn't change the color, I also tried putting it inside the

    Code:
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    but that just seemed to piss off the code so I knew that wasn't right either lol.

    Any help on how these are used would be great, I read the links you posted but it didn't really show how they are used in the code so i'm just kind of guessing here.
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,733
    Rep Power
    1959
    Originally Posted by F8LERROR
    but that just seemed to piss off the code so I knew that wasn't right either lol.
    Can you be more detailed what you mean with this?
    Did it compile? Did it execute without errors? with wrong colours?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Salt Lake City, UT, USA
    Posts
    6
    Rep Power
    0

    Talking Addendum


    ok I feel like an idiot now, I just looked at my code and realized I forgot something.

    I had it in there like this

    Code:
    SetConsoleTextAttribute(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    and it gave me these errors

    Code:
    Building Digital_Circuit_Calculations.obj.
    C:\Users\Joshua\Documents\Pelles C Projects\Circuit_Calculations\Digital_Circuit_Calculations.c(19): error #2140: Type error in argument 1 to 'SetConsoleTextAttribute'; expected 'void *' but found 'int'.
    C:\Users\Joshua\Documents\Pelles C Projects\Circuit_Calculations\Digital_Circuit_Calculations.c(19): error #2070: Insufficient number of arguments to 'SetConsoleTextAttribute'.
    *** Error code: 1 ***
    Done.
    And then I tried it this way

    Code:
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    And that way works and changes the color. I forgot to write it correctly. lol
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Salt Lake City, UT, USA
    Posts
    6
    Rep Power
    0

    Exclamation Another problem


    Now I am having another issue, been trying to correct it, but it's not cooperating with me. After adding the do/while loop the code works perfectly when I run it from Pelles C. But when I run the .exe outside of Pelles C it closes before you can see the result after inputting the variables.

    Been trying to change the code around and edit it to make it stay open and repeat until "q" is pressed to exit the program but it's not cooperating with me.

    Here is the code with the do/while loop in it and the "q" case as well.

    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 program
    #include <wincon.h>
    #include <stdbool.h>
    
    int main(void)
    {
     	bool runAgain = true;
    	do
    	{
    	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, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    	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("|           Choose q to exit program                                 |\n");
    	printf("|--------------------------------------------------------------------|\n\n");
    	SetConsoleTextAttribute(hConsole, 07);
    	printf("Choose an option\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 AMPS.\n");
    				SetConsoleTextAttribute(hConsole, 07);
    				scanf("%f",&current);								    //Capture first value typed on keyboard
    				SetConsoleTextAttribute(hConsole, 06);
    			printf("Enter RESISTANCE.\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", &current);
    			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", &current);
    				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", &current);
    				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", &current);
    				SetConsoleTextAttribute(hConsole, 06);
    			printf("Enter second value (AMPERE-HOURS).\n");
    				SetConsoleTextAttribute(hConsole, 07);
    				scanf("%f", &ampere_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;
    		case 'q':
    				runAgain = false;
    				break;
    			default :										//Default to nothing when no option is chosen
    			SetConsoleTextAttribute(hConsole, 07);
    			exit(0);										//Exit the program
    			break; 
    		} 
    
    	} while(runAgain);
    
    	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
    
    }
    i'm not asking for it to be done for me, just asking for some hints and information is all, I like trying to figure it out and then trying to figure it out with hints. I'm weird I know.
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > But when I run the .exe outside of Pelles C it closes before you can see the result after inputting the variables.
    If you just double-click on it in explorer, this is quite likely.

    But command line programs are typically run from a command line.
    So within a cmd.exe instance in a suitable directory, you simply type
    myprog.exe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  18. #10
  19. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    If you have selected "quit" why would you want the windows to stay open?

    It is normal (and desirable) behaviour in Windows that when a process terminates its window closes. If you run from a cmd session, it will be run as a child process of cmd so does not own the window and will not close it.

    Another solution during development is to run the code from your debugger with a break-point at the end.

    Finally you can always just use getchar(), but it will not return until enter rather than "any key" is pressed, but at least it is portable.

    With respect to having more than 10 options, perhaps using letters rather than digits is a better solution? If you want to enter numbers rather than characters use %d as the format specifier and switch on integer values rather than character constants.
    Last edited by clifford; December 28th, 2013 at 10:43 AM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Location
    Salt Lake City, UT, USA
    Posts
    6
    Rep Power
    0

    Cool Much appreciated information


    I do appreciate all the information and hints and tips, etc. I love it as I learn very quickly this way. I have made the necessary changes and corrected the code to work correctly. Thank you for all the information and advice and hints, I really appreciate it. Now if I can just figure out how to create the menu structure i'll be good. If I can't figure it out in a couple of days then i'll be back lol. But i'm going to try myself before picking anyone else's brain on here. Pick ya later... ;-) lol.

IMN logo majestic logo threadwatch logo seochat tools logo