C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-22-2009, 11:36 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 1
Series and Parallel Circuit Calculator

I am modifying this code to calculate ohms law for series and parallel circuits. I want to add another menu above the current one to allow the user to select between Series, Parallel, or Exit. I want this screen to show first, the go to the
screen to choose, voltage, current, and resistance. Can someone please let me know how to add this to this circuit program.

Thanks



Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h> 

int main()
{
char ch;
float voltage , current , resistance , result;
printf("Ohms law calculator.\n");
printf("Please choose from following calculcations.\n");
printf("1. choose 1 to calculate the voltage.\n");
printf("2. choose 2 to calculate the current.\n");
printf("3. choose 3 to calculate the resistance.\n");
printf("Anything else to quit.\n");

scanf("%c",&ch);
switch(ch)
{
case '1' :
printf("please enter the current in amps.\n");
scanf("%f",&current);
printf("Now enter the resistance in ohms.\n");
scanf("%f",&resistance);
result = current * resistance;
printf("The voltage is %0.3f volts.\n",result);
break;

case '2' :
printf("please enter the voltage in volts.\n");
scanf("%f",&voltage);
printf("Now enter the resistance in ohms.\n");
scanf("%f",&resistance);
result = voltage / resistance;
printf("The current is %0.3f amps.\n",result);
break;

case '3' :
printf("please enter the voltage in volts.\n");
scanf("%f",&voltage);
printf("Now enter the current in amps.\n");
scanf("%f",&current);
result = voltage / current;
printf("The resistance is %0.3f ohms.\n",result);
break;
default :
exit(0);
break;

}
return 0;
}

Last edited by LOBH; 11-22-2009 at 11:41 AM.
LOBH is offline   Reply With Quote
Old 11-22-2009, 01:06 PM   #2
Registered User
 
Join Date: Sep 2006
Posts: 2,959
Declare a mainMenu() function:
Code:
void mainMenu(void);
and have your program call it first, from main():
Code:
mainMenu();

/* add your content to the mainMenu function */
void mainMenu(void) {
   int choice;
   

   do {
      gotoxy(2,1);  //or use SetConsoleCursorPosition() in Windows API
      printf("\t\t\t    Welcome to the Main Menu\n\n");
      printf("\t1) Series Circuits \n\t2. Parallel Circuits \n\t");
      printf"\n3) Quit\n\n\t    Enter Your Selection [1-3]: ");
      scanf("%d", &choice);
    
      switch(choice) {
         case 1: seriesMenu(); break;
         case 2: parallelMenu(); break;
         case 3: break;
      }
  }while(choice != 3);
}
When choice == 3, the program will return to main, which should have little or nothing in it but a good by message for the user.

The code in main() now, should be moved to either the seriesMenu() or the parallelMenu(), that you will create to hold that code.
Adak is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equivalent Resistance Calculations TaBkill3r C++ Programming 17 11-10-2007 05:33 AM


All times are GMT -6. The time now is 12:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22