Need a help please. I need a program which reads from input file and then calculates total resistance and current. Circuit can be series or parallel. this is my input file:
C1
series
3
12
5 8 12
C2
parallel
3
24
6 19 5
Here is the code I have so far:
Code:#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; int main() { string name; string type; int n; int v; float res[3]; int I; int sum = 0; ifstream fin; fin.open("circuits.dat"); if (!fin) { cout << "Cannot open the file. Please make sure the file is in right folder." << endl; return 1; } cout << "Please enter the circuit type: "; cin >> type; cout << endl; if (type == "series") { fin >> name; fin >> type; fin >> n; fin >> v; for (int i=0; i<3; i++) { fin >> res[i]; sum = sum + res[i]; I = v*sum; } cout << name << endl; cout << type << endl; cout << n << endl; cout << v << endl; for (int i=0; i<3; i++) cout << res[i] << " "; cout << endl; cout << "The total resistance R = " << sum << endl; cout << "The total current I = " << I << endl; } else if (type == "parallel") { fin >> name; fin >> type; fin >> n; fin >> v; for (int i=0; i<3; i++) { fin >> res[i]; sum = sum + res[i]; I = v*sum; } cout << name << endl; cout << type << endl; cout << n << endl; cout << v << endl; for (int i=0; i<3; i++) cout << res[i] << " "; cout << endl; cout << "The total resistance R = " << sum << endl; cout << "The total current I = " << I << endl; } else { cout << "Terminating program." << endl; return 1; } fin.close(); return 0; }
My problem is: when the user selects "parallel" as a circuit file, the program must start reading from line "C2" and ignore all previous lines. I can't get it working. What function do I need to use for that?
PS: this must be beginning level and I can't use structs, classes or vectors here.
Thanks



LinkBack URL
About LinkBacks


