Need some help implementing an array and using it in a function

I am trying to program a basic electronics calculator for a programming class and I keep getting errors for my array and function. I have gone through the forum and tried to see what I am doing wrong. I may have just been staring at the code for to long. Any help would be greatly appreciated.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>


using namespace std;


int valid ();
int main ()
{
double total_resistance, freq, cap, ind, volts, amps, res1;
int option, num_of_resistors, res;

cout <<"what type of circuit do you have?\n";
do{
cout <<"Enter 1 for a series circuit \n";
cout <<"Enter 2 for a parallel circuit \n";
cout <<"Enter 3 to find the resonant frequency of a circuit\n";
cout <<"Enter 4 to find the voltage drop across a resistor\n";
cout <<"Enter 5 to find the current in a circuit\n";
cout <<"Enter 6 to find an unknown resistance\n";
cout <<"Enter 7 if you are an instructor\n";
cout <<"Enter 0 to exit\n";
cout <<"option";

system ("cls");
switch (option){
case 1:{

cout <<"how many resistors do you have? \n";
cin >> num_of_resistors;


for (int i = 0; i < num_of_resistors; i++)
{
cout <<"enter the resistance for R"<<(i+1)<<" in K ohms \n";
cin >> res[i];
res[i] = valid (res[i]);
}
for (int i = 0; i < num_of_resistors; i++)
{
total_resistance = total_resistance + res[i];
}
cout << "The series resistance is:"<< total_resistance << "ohms \n";
}
break;

case 2:{

cout << "Enter the number of resistors in parallel you have: \n";
cin >> num_of_resistors;
int res(num_of_resistors);

for (int i = 0; i< num_of_resistors; i++)
{
cout <<"enter the resistance for " << (i+1) << "in K ohms \n";
cin >> res[i];
res[i] = valid (res[i]);
}
for (int i = 0; i < num_of_resistors; i++){
total_resistance =1/ total_resistance +1/ res[i];
}
cout <<"The parallel resistance is:" << 1/total_resistance <<"K ohms \n";
}
break;
case 3:{

cout <<"Enter your capacitance in micro farads\n";
cin >>cap;
cap = valid(cap);

cout <<"Enter your inductance in micro-Henrys\n";
cin >> ind;
ind = valid(ind);

freq = (2 * 3.1415) * sqrt(( (cap*.000001) *( ind*.000001)));
cout <<" your resonant frequency is " << 1/freq<<"Hz";

}
break;
case 4: {

cout <<"Enter your resistance in ohms:\n";
cin >> res1;
res1 = valid(res1);

cout << "Enter your current in amps:\n";
cin >> amps;
amps = valid (amps);

volts = amps * res1;

cout <<"Your voltage drop is:\n" << volts<<" volts";
}
break;
case 5: {
cout <<"Enter your voltage:\n";
cin >> volts;
volts = valid(volts);

cout << "Enter your resistance:\n";
cin >> res1;
res1 = valid(res1);

amps = volts / res1;

cout <<"your current is:" << amps << " amps\n";

}
break;
case 6:{
cout <<" Enter your voltage drop in volts:\n";
cin >> volts;
volts = valid(volts);

cout <<"Enter your current in amps:\n";
cin >> amps;
amps = valid(amps);

res1 = volts / amps;

cout <<"Your mystery resistance is:"<< res1 <<" ohms\n";
}
break;

case 7:{

cout<<"programming this has not been fun\n \n \n \n \n";

}
break;
default:
cout << "\n\n*************goodbye*************\n\n";
}}

while (option > 0);

return 0;
}



int valid (int i)
{
int num;
do{

if (num < 0)
{
cout << "you are the weakest link\n input must be positive, \n try again\n";
cin >> num;
}}
while (num<0);
return(num);
}
closed account (j3Rz8vqX)
Next time embedded your source code in the source code bracket.

Other than that, your double res1 isn't an array, therefore you can't make it behave like an array.

In your case, because the size is not yet known, you would either use a size large enough to fit any size the user could possibly enter or a pointer and a new double array.

1) An appropriate predefined array would look similar to:
 
double res1[1000];


2) An appropriate pointer to a dynamically sized array would look like this:
1
2
3
4
5
6
7
8
double * res1;
//prompt user for size of array: num_of_resistors
res1 = new double[num_of_resistors];
//do stuff

//make sure to delete new memory afterwards!!
delete []res1; //deletes multiple objects at pointer
res1 = 0;//set pointer address to objects at null/o; ensures it doesn't cause unknown behavior if you accidently call it again later. 


In your case, #1 should be enough to solve your problem.

Use the predefined array if you haven't learn pointers, new, and delete yet.
Topic archived. No new replies allowed.