Free2Code
The majority of forums are now only available as archives, which means posting/editing is disabled.

The Anything and Everything forum is still open.
 
Time: 2014-11-28, 04:46pm
Need help!
Subject: Need help!  ·  Posted: 2006-04-07, 03:44am
Rank: ? (5)
Member #: 26557
Hi this is my first time tryin to use a SWITCH case in a while loop.
Printed out a menu for user to prompt. User must choose from 1-4,
1-3 are calculations and 4 is to quit the program.
when i run it first part shows when prompting the user for the follow
info voltage, Resistor 1-3 but does not show the output statement which show the calculation just keeps on repeating Goodbye. Goodbye is the output statement when the user wants to quit.
Here is the program.




Program:

/*Solving for various currents, voltages, and power in a parallel
resistor circuit.Prompt user for Vs,R1, R2, R3. Calculate and print
out Itotal, Rtotal, & Ptotal. Then print out a menu of options of
circuit [arameters that the user can choose from to be solved for/include
option to quit.Continually loop until the user chooses to quit. Seleton
Structure must be used for the options that the user chooses from. Switch
selection must be used to deal wit menus very efficiently. Must also implement
four user defined functions that calculate the currens downtthe three branches
of the circuit. Finally program must calculate and print out the voltage drop
and the power dissipated in the circuit branch in question.*/

#include <iostream.h>
#include <math.h>

void main()
{
double v, r1 = 0, r2 = 0, r3 = 0;
double pd, rt, it;
double ir1, ir2, ir3, pd1, pd2, pd3;
int n = 0;

//Header
cout << "Paralel Resistance Circuit Solver \n";

//prompt user for data
cout << "Enter the value of the voltage supply [V] \n";
cin >> v;

cout << "Enter the value of Resistor 1 [Ohms] \n";
cin >> r1;

cout << "Enter the value of Resistor 2 [Ohms] \n";
cin >> r2;

cout << "Enter the value of Resistor 3 [Ohms] \n";
cin >> r3;

//Calculating for total Resistance
rt = exp(r1) + exp( r2) + exp( r3);
cout << "The total resistance is:" << rt <<"ohms \n";

//Calculating for total Current
it = v / rt;
cout <<"The total current is:" << it <<"A \n";

//Calculating for total Power Dissipated
pd = it * v;
cout <<"The total power dissipated is:" << pd <<"W \n";

//Menu for user
cout <<"What do you want to slove for?: \n";
cout << "1. I[R1]\n";
cout << "2. I[R2]\n";
cout << "3. I[R3]\n";
cout << "4. Quit \n";
cout << "Selection =>"<< n <<"\n";

//begin while loop
while(n <= 4)
{
switch(n) //looping switch case
{

//Calculating Resistor 1, voltage, current, and power across it
case 1:
ir1 = v * r1;
cout <<"The current across R1 is" << ir1 <<"A \n";
cout <<"The voltage drop across this branch is" << v <<"V \n";
pd1 = v * ir1;
cout <<"The power dissipated across this branch is" << pd1 <<"W \n";
break;

//Calculating Resistor 2, voltage, current, and power across it
case 2:
ir2 = v * r2;
cout <<"The current across R2 is" << ir2 <<"A \n";
cout <<"The voltage drop across this branch is" << v <<"V \n";
pd2 = v * ir2;
cout <<"The power dissipated across this branch is" << pd2 <<"W \n";
break;

//Calculating Resistor 3, voltage, current, and power across it
case 3:
ir3 = v * r3;
cout <<"The current across R3 is" << ir3 <<"A \n";
cout <<"The voltage drop across this branch is" << v <<"V \n";
pd3 = v * ir3;
cout <<"The power dissipated across this branch is" << pd3 <<"W \n";
break;

default:
cout <<"Goodbye \n";

}//end while loop
}//end switch case

}//end main program



 
  Reply to this ·  Post link ·  Top
Subject: Re: Need help!  ·  Posted: 2006-04-07, 04:03am
Rank: ? (5)
Member #: 26557
opps the program i post was an old one here the recent one:

recent program:
/*Solving for various currents, voltages, and power in a parallel
resistor circuit.Prompt user for Vs,R1, R2, R3. Calculate and print
out Itotal, Rtotal, & Ptotal. Then print out a menu of options of
circuit [arameters that the user can choose from to be solved for/include
option to quit.Continually loop until the user chooses to quit. Seleton
Structure must be used for the options that the user chooses from. Switch
selection must be used to deal wit menus very efficiently. Must also implement
four user defined functions that calculate the currens downtthe three branches
of the circuit. Finally program must calculate and print out the voltage drop
and the power dissipated in the circuit branch in question.*/

#include <iostream.h>
#include <math.h>

void main()
{
double v, r1, r2, r3;
double pd, rt, it;
double ir1, ir2, ir3, pd1, pd2, pd3;
int n;

//Header
cout << "Paralel Resistance Circuit Solver \n";

//prompt user for data
cout << "Enter the value of the voltage supply [V] \n";
cin >> v;

cout << "Enter the value of Resistor 1 [Ohms] \n";
cin >> r1;

cout << "Enter the value of Resistor 2 [Ohms] \n";
cin >> r2;

cout << "Enter the value of Resistor 3 [Ohms] \n";
cin >> r3;

//Calculating for total Resistance
rt = 1/r1 + 1/r2 + 1/r3 ;
cout << "The total resistance is:" << rt <<"ohms \n";

//Calculating for total Current
it = v / rt;
cout <<"The total current is:" << it <<"A \n";

//Calculating for total Power Dissipated
pd = it * v;
cout <<"The total power dissipated is:" << pd <<"W \n";

//Menu for user
cout <<"What do you want to slove for?: \n";
cout << "1. I[R1]\n";
cout << "2. I[R2]\n";
cout << "3. I[R3]\n";
cout << "4. Quit \n";
cout << "Selection =>"<< n <<"\n";

//begin while loop
while(n <= 4)
{
switch(n) //looping switch case
{

//Calculating Resistor 1, voltage, current, and power across it
case 1:
ir1 = v * r1;
cout <<"The current across R1 is" << ir1 <<"A \n";
cout <<"The voltage drop across this branch is" << v <<"V \n";
pd1 = v * ir1;
cout <<"The power dissipated across this branch is" << pd1 <<"W \n";
break;

//Calculating Resistor 2, voltage, current, and power across it
case 2:
ir2 = v * r2;
cout <<"The current across R2 is" << ir2 <<"A \n";
cout <<"The voltage drop across this branch is" << v <<"V \n";
pd2 = v * ir2;
cout <<"The power dissipated across this branch is" << pd2 <<"W \n";
break;

//Calculating Resistor 3, voltage, current, and power across it
case 3:
ir3 = v * r3;
cout <<"The current across R3 is" << ir3 <<"A \n";
cout <<"The voltage drop across this branch is" << v <<"V \n";
pd3 = v * ir3;
cout <<"The power dissipated across this branch is" << pd3 <<"W \n";
break;

default:
cout <<"Goodbye \n";

}//end while loop
}//end switch case

}//end main program



 
  Reply to this ·  Post link ·  Top
Subject: Re: Need help!  ·  Posted: 2006-04-07, 07:24am
Rank: ? (4827)
Member #: 3416
//Calculating for total Resistance
rt = 1/r1 + 1/r2 + 1/r3 ;

what you're calculating for rt here is actually gt where gt=1/rt and would be called total admittance.

//Menu for user
...

//begin while loop

your while loop is checking the value of n, which is changed by the "menu for user" code (not inside the while loop). this is the cause for your infinite loop. you'll need to put the menu inside the while loop so that the condition will change and you can get out of the loop.

while(n <= 4)

since 4 is quit, you probably don't want to stay in the loop when they hit 4. also this allows n to be 0 or negative.

...The current across R1 is...
...The power dissipated across this branch is...

picky, but the prepositions here aren't accurate. current would be either in or through instead of across, and power would be either in or by.

}//end while loop
}//end switch case

these comments are backward. since you started the while loop and then the switch, you are ending the switch and then the while loop.

» Post edited 2006-04-07, 07:34am by misterhaan.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top
Subject: Re: Need help!  ·  Posted: 2006-04-09, 09:05pm
Rank: ? (22)
Member #: 26975
why dont you do this

Code:
  1. while (x != 4)
  2. {
  3. switch (x)
  4. {
  5. case 1: 
  6. case 2:
  7. case 3:
  8. case 4:
  9. default:
  10. }
  11. }


this should work for your menu where case 1, 2, 3 all do something, and 4 does nothing it stays like that.

 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.