• 11-20-2012
Nurlana
Hi guys, need a help. I have this program:
Code:

```#include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; void ReadData(ifstream& inFile, string name, string type, int n, int v, int Ri[20]); void ResistanceParallel(int v, int I, int Ri[20]); void ResistanceSeries(int v, int I, int Ri[20]); void printData(string name, string type, int n, int v, int Ri[20], float I); int main() {     ifstream inFile;     string name;     string type;     int n;                  //number of components     int v;                  //voltage of the circuit     int Ri[20];          //number of resistances     int I;                  //current in the circuit     inFile.open("circuits.dat");     cout << "This is the program to calculate the resistances" << endl;     cout << "Please select the circuit type (series or parallel): ";     cin >> type;     if (type == "series")     {       ReadData(inFile, name, type, n, v, Ri);       ResistanceSeries(v, I, Ri);       printData(name, type, n, v, Ri, I);     }     else if (type == "parallel")     {       ReadData(inFile, name, type, n, v, Ri);       ResistanceParallel(v, I, Ri);       printData(name, type, n, v, Ri, I);     }     inFile.close();     return 0; } void ReadData(ifstream& inFile, string name, string type, int n, int v, int Ri[20]) {   inFile >> name >> type;   inFile >> n >> v;   for (int r=0; r<20; r++)   {     inFile >> Ri[r];   } } void ResistanceSeries(int v, int I, int Ri[20]) {   float sum;   for(int j = 0; j < 20; j++)                 {       sum = 0;       sum += Ri[j];                 }   I = v/(float)sum; } void ResistanceParallel(int v, int I, int Ri[20]) {   float sum;   for(int j = 0; j < 20; j++)                 {       sum = 0;       sum += 1/Ri[j];                 }         I = v/(float)sum; } void printData(string name, string type, int n, int v, int Ri[20], float I) {   float sum;   cout << "Circuit name: " << name << endl;   cout << "Circuit type: " << type << endl;   cout << "Circuit voltage: " << v << endl;   cout << "Total resistance R=" << sum << endl;   cout << "Total current I=" << I << endl; }```
And this is my input file (I didn't create it, it was given by instructor)
C1
series
3
12
5 6 7
C2
parallel
3
24
2 3 5

My question is: I don't know how to write the code so that when the user types "Series", the data after the word "series" is read and processed and if the user types "parallel" the data after the word "parallel" is read and processed. Thanks
• 11-21-2012
DRK
Quote:

Originally Posted by Nurlana
Code:

```float sum; for(int j = 0; j < 20; j++) {     sum = 0;     sum += 1/Ri[j]; }```

1. sum should be initialized with 0 before the loop, not inside it.
2. Division operator used with integer values returns also an integer, so you have explicitly cast dividend or divisor to float type.
Code:

`sum += (float) 1 / Ri[j];`
• 11-21-2012
rags_to_riches
Looks to me like you want a struct, but given that we're not in your class and don't know what you've been taught nor the aim of this assignment as it relates to what you've been taught, we can't tell you for sure what is intended by this:

Quote:

My question is: I don't know how to write the code so that when the user types "Series", the data after the word "series" is read and processed and if the user types "parallel" the data after the word "parallel" is read and processed. Thanks
It could be that you're expected to read the data into an array of structs, then iterate through the array finding the type of circuit requested and processing only those. Or it could be something entirely different.

• 11-21-2012
Nurlana
Quote:

Originally Posted by rags_to_riches
Looks to me like you want a struct, but given that we're not in your class and don't know what you've been taught nor the aim of this assignment as it relates to what you've been taught, we can't tell you for sure what is intended by this:

It could be that you're expected to read the data into an array of structs, then iterate through the array finding the type of circuit requested and processing only those. Or it could be something entirely different.

Actually, I was in the class and I know what I was taught. I know how to write a code using structs and classes. The instructions were not to use structs or classes here. If you don't understand my question, you could clarify with questions and no need being nasty and showing off.
My question was how to start reading input file from certain point in the file, not from the very beginning.
• 11-21-2012
Matticus
You can use "fseek()".

Or, if the format of the data is consistent, you can:
- Read first chunk of data
- If it's what you're looking for, use it
- If it's not what you're looking for, read the next chunk of data
- Continue
• 11-21-2012
Nurlana
Thank you very much, Matticus. I will try fseek().