-1

I am having trouble with this programming assignment. I need to calculate the value of total resistance for a parallel and series circuit. I have the series circuit functioning, but my problem is that when I try to calculate the total resistance for parallel circuit, my return value is 1.#INF. Any suggestions to how I can fix this

    // project.cpp : Defines the entry point for the console application.
    //

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

    using namespace std;

    void menu()
    {
       cout <<"\t\t\tLab 2 Menu Driven Algorithms" << endl;
       cout <<"\t\t  Calculating Parallel and Series Resistance" << endl;
       cout <<"1)\tSeries" << endl;
       cout <<"2)\tParallel" << endl;
       cout <<"3)\tQuit" << endl;   
    }

    int series(int& num, int& sum)
    {
int answer;
num = 0;
sum = 0;

do
{
    cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
    cin >> answer;
    cout << endl;

    sum = sum + answer;

    num++;
}
while(answer != 0);     

return sum;
    }

    double parallel (int& num, double& sum)
    {
double answer;
num = 0;
sum = 0.0;
int counter = 0;

do
{
    cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
    cin >> answer;
    cout << endl;
    counter++;

    sum = (1/answer) + sum;
    cout << sum << endl;

    num++;
}
while(answer != 0);

return sum;
    }




    int main()
    {
char choice;
int num = 0;
int sum = 0;
double sum2 = 0.0;

menu();

cout <<"\n\nPlease enter a value from the menu: ";
cin >> choice;
cout << endl;

while (choice != '3' && choice != 'q' && choice != 'Q')
{


switch(choice)
{
    case '1': cout << "Calculating Series Resistance" << endl;
              cout << "The series resistance for the " << num-1 << " resistors is: " << series(num, sum) << " Ohms\n";
              system("pause");
              break;
    case '2': cout << "Calculating Parallel Resistance" << endl;
              cout << "The parallel resistance for the " << num-1 << " resistors is: " << parallel(num, sum2) << " Ohms\n";
              system("pause");
              break;
    default: break;

}

system("cls");

menu();

cout <<"\n\nPlease enter a value from the menu: ";
cin >> choice;
cout << endl;


} 
system("pause");

return 0;
    }
fkianos15
  • 39
  • 1
  • 5

1 Answers1

1

You probably want to rewrite the cycle inside the parallel() function this way, so that you will never process a value of 0 (which causes a division by zero in this case):

cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
cin >> answer;
cout << endl;

while (answer != 0);     
{
    counter++; // NOTICE: This is never used for computation...

    sum = (1/answer) + sum;
    cout << sum << endl;

    num++;

    cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
    cin >> answer;
    cout << endl;
}

The counter variable is never used for computation, so I guess you could get rid of it.

Also notice, that even the loop inside series() has a similar problem, although the fact that you never cause a division by zero doesn't make it visible.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451