-1

txt file

class calcBMI {
    public:
        string line;
        string line2;
        fstream search;
        short loop = 0;
        string weight[6];
        string height[6];
        int index[6] = { 1, 2, 3, 4, 5 };
        int i;

        void getHeight();
        void getWeight();

    };

.

void calcBMI::getWeight() {

    search.open("name.txt"); //Opens the text file in which the user details are stored
    if (search.is_open())
    {
        while (getline(search, line) && (getline(search, line2))) { //While the program searches for the lines in the text file
            if (line.find("Current Weight(KG): ") != string::npos && (line2.find("Height(Metres)") != string::npos)) { //If the string "Name" isnt the last word on the line
                weight[loop] = line; //Assings the strings read from the text file to the array called weight
                height[loop] = line2;
                cout << index[loop] << ". " << weight[loop] << ", " << height[loop] << endl; //Outputs the index array which loops through the numbers in the array and outputs the weight variable which loops through the strings in the array
                loop++; //Iterates the loop 

            }

        }

    }

}

So im trying to read two pieces of data from a txt file which contains 5 users. The data stored about the users are their name, height, weight and, previous weights. The layout/format of the file is below.

  • Name: Michael
  • Current Weight(KG): 65
  • Four Previous Weight Measurements: 67, 69, 75, 72
  • Height(Metres): 1.7

I'm trying to read the heights and the weights of the users by using the following code, but when i run the code, the program doesnt output anything.

The program should print:

  1. Current Height(KG): 00, Height(Metres): 00
  2. Current Height(KG): 00, Height(Metres): 00
  3. Current Height(KG): 00, Height(Metres): 00
  4. Current Height(KG): 00, Height(Metres): 00
  5. Current Height(KG): 00, Height(Metres): 00

However, it prints nothing.

Snappy
  • 5
  • 2
  • Does this answer your question? [Read file line by line using ifstream in C++](https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c) – Frontear Dec 06 '19 at 00:58
  • Can you show me what your file looks like? – bhristov Dec 06 '19 at 01:00
  • `getline(search, line) && (getline(search, line2)` searches two consecutive lines for the data you want and only enters the body of the `if` if both searches are successful. After looking over your input format, [your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) has an observation for you: "Waaaugh! These two lines are not beside each other!" You should sit down with duckie and talk through the implications of that. – user4581301 Dec 06 '19 at 01:02
  • I've uploaded the txt file – Snappy Dec 06 '19 at 01:21
  • I made an edit to the code. I believe that it will work on your file. – bhristov Dec 07 '19 at 00:02

1 Answers1

0

This is a rewritten version:

        bool w = false, h = false;
        while (getline(search, line))
        {
            if (line.find("Current Weight(KG):") != string::npos)
            {
                weight[loop] = line; //Assings the strings read from the text file to the array called weight
                w = true;
            }
            else if (line.find("Height(Metres)") != string::npos)
            {
                height[loop] = line;
                h = true;
            }

            if (w && h)
            {
                cout << index[loop] << ". " << weight[loop] << ", " << height[loop] << endl; //Outputs the index array which loops through the numbers in the array and outputs the weight variable which loops through the strings in the array
                loop++; //Iterates the loop 
                w = false;
                h = false;
            }
        }
bhristov
  • 3,137
  • 2
  • 10
  • 26
  • The `if` is useless in this case. There is no good way to recover from a mis-aligned file. Just read the four lines and assume they're right or write a much smarter scanner to ensure alignment. – user4581301 Dec 06 '19 at 01:13
  • It does work but it only outputs the first user and the last skipping over the users in between – Snappy Dec 06 '19 at 01:20
  • I rewrote it. Try it now. – bhristov Dec 06 '19 at 04:10