DC Circuits Program,need help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: DC Circuits Program,need help

  1. #1
    Join Date
    Apr 2014
    Posts
    2

    DC Circuits Program,need help

    My group and I are writing a program that can solve Resistance total, Voltage total and Current total in a DC series, parallel, and series-parallel circuit. We have the whole program written already but we cannot get the parallel part of the program to work. We have the program setup to have up to 5 resistors for series and 5 for parallel, the series part works perfectly and gives you the correct total resistance. When you enter the resistance values for the parallel part of the circuit it gives us the incorrect total resistance if you enter values for less than 5 resistors. Example if the circuit only has 3 resistors in parallel you enter the 3 values and the other 2 resistors you have to enter 0ís for the values and it will give you the incorrect resistance total. If you enter 5 resistor values it gives us the correct number. We think the reason we are getting the incorrect value is because you have to enter a 0ís.

    We need help in figuring what we can do to get the parallel part to work. We tried rewriting the program to first ask the user how many resistors are in parallel to avoid entering 0ís but we could not get it to work probably because we are all beginners in programming. Any assistance is appreciated.



    Code:
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    using namespace std;
    void resistance();
    void current();
    void voltage();
    void printMenu(void);
    int main()
    
    {
    char selection;
    do
    
    {
    printMenu();
    cin>>selection;
    
    switch(selection)
    
    {
    case 'r':
    case 'R':
    resistance();
    break;
    
    case 'c':
    case 'C':
    current();
     break;
    
     case 'v':
    case 'V':
    voltage();
     break;
    
     case 'q':
    case 'Q':
    cout<<"Thank you & goodbye."<<endl;
    
    return 0;
    }}
     while(selection != 'r' ||selection != 'R' ||selection != 'c' ||selection != 'C'
    
    
    ||selection != 'v' ||selection != 'V' ||selection != 'q' ||selection != 'Q');
    
     cout<<"Invalid selection."<<endl;
    return main();
    }
    
    void printMenu()
    {
    
    	cout<<"[R]esistance [C]urrent [V]oltage [Q]uit"<<endl;
    }
    
    void resistance()
    {
    
    	const int maxResRS = 5, maxResRP = 5;
    float Res[maxResRS], res[maxResRP];
    float resSTot = 0, resPTot = 0, resTot = 0;
    cout<<"Enter the value of up to 5 resistors in series:"<<endl;
    for(int i = 0; i < maxResRS; i++)
    
    {
    
    cin>>Res[i];
    resSTot += Res[i];
    }
    
    cout<<"Enter the value of up to 5 resistors in parallel:"<<endl;
    for(int j = 0;j < maxResRP; j++)
    {
    
    cin>>res[j];
    resPTot += pow(res[j],-1);
    resTot = resSTot+pow(resPTot,-1);
    }
    
    cout<<"\n The total resistance ="<<resTot<<endl;
    
    }
    void current()
    
    {
    
    int resTot, Volt_T;
    cout<<"Please enter the total resistance of the circuit:"<<endl;
    cin>>resTot;
    cout<<"Please enter the total voltage:"<<endl;
    cin>>Volt_T;
    cout<<"The total current ="<<Volt_T/resTot<<endl;
    
    }
    
    void voltage()
    int resTot, Curr_T;
    cout<<"Please enter the total resistance of the circuit:"<<endl;
    cin>>resTot;
    cout<<"Please enter the total voltage:"<<endl;
    cin>>Curr_T;
    cout<<"The total current ="<<Curr_T/resTot<<endl;
    
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: DC Circuits Program,need help

    I stopped reading here.
    while(selection != 'r' ||selection != 'R' ||selection != 'c' ||selection != 'C' ||selection != 'v' ||selection != 'V' ||selection != 'q' ||selection != 'Q');

    There may well be other problems, but if you think about it, that statement will always be true. For example, if selection is 'r', it won't be 'R', so the condition is satisfied.

    Also, please try to use meaningful indentation. It will make the code much easier to read and follow.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: DC Circuits Program,need help

    The formula for parallel resistance is 1/RT = 1/r1 + 1/r2 + 1/r3 + ... So if a value entered is 0, just don't add in its reciprical for the parallel sum.
    Code:
    	for (int j = 0; j < maxResRP; j++)
    	{
    		cin >> res[j];
    		if (res[j] > 0)
    			resPTot += 1.0/res[j];
    	}
    	resTot = resSTot + 1.0/resPTot;
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Aug 2013
    Posts
    55

    Re: DC Circuits Program,need help

    Quote Originally Posted by vela84 View Post
    Code:
    cin>>res[j];
    resPTot += pow(res[j],-1);
    resTot = resSTot+pow(resPTot,-1);
    This,

    pow(x,-1);

    is equivalent to a simple division like,

    1.0 / x;

    and if x is 0.0 the result will be infinity and strange things may happen.

    So check the input and if it's zero (or close) skip the division and don't add up any resistance.

    I also suggest you use double throughout (rather than float). And avoid mixing ints and floating points in calculations. 1 is an int while 1.0 is a double.
    Last edited by zizz; April 21st, 2014 at 09:53 AM.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: DC Circuits Program,need help

    Your menu selection code can be simplied. Try this
    Code:
    int main()
    {
    char selection;
    
    	while (true)
    	{
    		printMenu();
    		cin >> selection;
    
    		switch(tolower(selection))
    		{
    			case 'r':
    				resistance();
    				break;
    
    			case 'c':
    				current();
    				break;
    
    			case 'v':
    				voltage();
    				break;
    
    			case 'q':
    				cout << "Thank you & goodbye." << endl;
    				return 0;
    
    			default:
    				cout << "Invalid selection." << endl;
    		}
    	}
    }
    Code:
    return main();
    is not usually a good idea as it recursively calls main().
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 2014
    Posts
    2

    Re: DC Circuits Program,need help

    Thank you everyone for all the help, we have the program working now!!!! I think we might change it so the user can have the choice of how many resistors are in the circuit instead of being limited to 5 resistors. Thank you once again.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: DC Circuits Program,need help

    If you are giving the users a choice of how many resistors, you might want to consider different numbers for series and parallel. There are several of ways of doing this. One is to ask for the user to specify how many. Another is to accept values until an invalid one (eg 0) is entered. Or you can accept values until the input stream fails (eg enter a non-numeric value or force eof with CTRL-Z) or a combination of these. You don't actually need to use an array to hold the values entered as you can directly use the entered value in the calculation loops.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center