Reading from specific word in text file

This is a discussion on Reading from specific word in text file within the C++ Programming forums, part of the General Programming Boards category; Need a help please. I need a program which reads from input file and then calculates total resistance and current. ...

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

    Reading from specific word in text file

    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    All the information you need is in the file. You shouldn't have any cin >> var in your code at all.

    Code:
    while ( fin >> name ) {
        fin >> type;
        if ( type == "series" ) {
            fin >> n;  // the number of resistors
            fin >> v;  // the voltage
            for (int i=0; i<n; i++)  // n resistors
            {
                fin >> res[i];  
                sum = sum + res[i];
                I = v*sum;
            }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20
    The requirement is you ask the user what type of the circuit he wants and depending on choice (cin >> type (where type is either "series" or "parallel")) the program reads and calculates the required data. It works great for "series", but when the choice is "parallel" the program again takes data for C1 (beginning of file). I can't figure out how to make the program to start reading from line C2 if the user types "parallel" for type of circuit.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    It looks like you should be searching for "series" or "parallel" depending on what the user wants. Once you actually find the keyword then the next line starts the values for the calculations. If the keyword isn't present you read another line until you find the correct keyword. You are lucky at this time that series is on the first line, what happens if "parallel" was first?


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting specific word from file
    By donaldgx in forum C Programming
    Replies: 5
    Last Post: 04-13-2012, 09:00 PM
  2. Reading a specific line from a text file
    By acidrain in forum C Programming
    Replies: 3
    Last Post: 12-01-2009, 01:23 PM
  3. Searching a specific string/word/etc in a text file?
    By zacharyrs in forum C Programming
    Replies: 5
    Last Post: 11-29-2009, 06:54 PM
  4. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  5. Help reading text file word by word
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2002, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21