Thread: Reading from input file

  1. #1
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20

    Reading from input file

    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

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Quote Originally Posted by Nurlana View Post
    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];
    Last edited by DRK; 11-21-2012 at 03:12 AM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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:

    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.

    Maybe you should ask your professor.

  4. #4
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20
    Quote Originally Posted by rags_to_riches View Post
    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.

    Maybe you should ask your professor.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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

  6. #6
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20
    Thank you very much, Matticus. I will try fseek().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from an input file
    By Trevers in forum C Programming
    Replies: 1
    Last Post: 05-06-2010, 12:21 PM
  2. Reading Input from file
    By Horox in forum C Programming
    Replies: 2
    Last Post: 01-31-2008, 12:13 AM
  3. reading input file
    By dellebelle751 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2002, 02:05 PM
  4. reading input from file
    By lavon in forum C Programming
    Replies: 1
    Last Post: 03-19-2002, 06:49 PM
  5. Reading input from a file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-19-2002, 06:34 PM

Tags for this Thread