Question - Run multiple IF's

Greetings,

First off, hello all. I am new to the forum, but I have recently been using the site for advice. I am currently a novice at C++, so please forgive my ignorance.

Question: I am attempting to read two different lines from a .txt file, and I thought I could get away with running two IF statements in order to get the lines at the same time. Is is possible to do this? If not, is there a better way to go about doing this?

The result of the code below is that it reads the first string (str), but does not read the second IF all together, thus not grabbing my second string (str2).

Thank you for any advice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
    ifstream inFile;
    inFile.open("2M41.txt");

    //Check for error
    if (inFile.fail()) {
        cerr << "Error opening file" << endl;
        exit(1);
    }

    string line;
    string line2;

        for (int lineNo = 0; getline(inFile, line) && lineNo < 35000; lineNo++) {
        if (lineNo == 247) {
            string str = line.substr(31, 7);
            cout << str << endl;
        }
        }

        for (int lineNo = 0; getline(inFile, line2) && lineNo < 35000; lineNo++) {
        if (lineNo == 32351) {
            string str2 = line2.substr(32, 7);
            cout << str2 << endl;
        }
        }

    inFile.close();

    return 0;
}
The conditions in your first and second loop are the same, therefore, the second loop will never run.

If you deleted lines 28, 29, and 30, your program would work ( assuming there are at least 32351 lines )
Last edited on
Once you call getline() or any other read operation on a file, an internal pointer of the fstream class is incremented, and the next read starts at that point. What is happening in your program is that in your first loop you've already read 35000 lines of your file, and your second for loop starts reading at line 35001. You will get the result you want putting both statements in a single loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int lineNo = 0; getline(inFile, line) && lineNo < 35000; lineNo++)
{
    if (lineNo == 247)
    {
        string str = line.substr(31, 7);
        cout << str << endl;
    }

    if (lineNo == 32351)
    {
        string str2 = line.substr(32, 7);
        cout << str2 << endl;
    }
}

Last edited on
Hello MrZ,

glad you came in here. Welcome :)

It is possible to read two lines at the same time, but it's not what you want. It would not work as you intend to.
What are you aiming to achieve? If you want to compare two lines, simply load first line, then second line, and then compare them.
If you plan on doing other thing, you may want to tell us what is your point, and we will provide you with some way of achieving your goal.
The second loop starts with the inFile already eaten by getline, so it won't work. But why not do both birds in one stone loop?
1
2
3
4
5
6
7
8
9
10
for (int lineNo = 0; getline(inFile, line) && lineNo < 35000; lineNo++) {
	if (lineNo == 247) {
		string str = line.substr(31, 7);
		cout << str << endl;
	}
	else if (lineNo == 32351) {
		string str2 = line.substr(32, 7);
		cout << str2 << endl;
	}
}
Combining the two statements into the single FOR loop solved it.
I originally learned that its better to use separate loop statements because its "easier to read", but as I progress through C++, I'm learning that this isn't the best way to go about it.

Thank you all for replying so quickly!
@MatthewRock

The whole purpose of the program is to grab these strings from a .txt file, convert them to double's, and then do a subtraction between them. I shall continue my research in converting char types...
So you can do it one by one - you don't have to use both at once :)
Remember, that if your file is written properly, you can use >> operator to get formatted input from ifstream, so all in all it would just look like this
1
2
3
4
5
ifstream file;
int number;
//code...
file >> number;//Load formatted input from file to number
//That's it! Number is already an integer, no need to do anything else, no conversions or anything. 
Topic archived. No new replies allowed.