How do I use a while(!line.empty) loop?

So I'am trying to make a program that uses a while loop to pull data form a text file.

I'm using a while loop because I do not know how many times it will have to loop, and as for as I know for loops are only useful for when you know how many times you want to loop.

I want my program to read to gather input from a txt file, and loop until there is an empty space.

I need help setting my condition to stop when there is an empty space.

For example:
The input file:
55
77
88
55

while( there is a value continue, when there is no value break loop)

the values can be floats or int,

any one might be able to help me out?
Thank you
Last edited on
I need help setting my condition to stop when there is an empty space.


Well you just answered your own question:

 
while(!empty_line_found)


Have a boolean variable and set it to true when you find an empty line. Then presto, the loop stops.
So your saying that I should have something like

bool empty_line_found;

empty_line_found = (" ");

?

how do I say "empty line" lol
bools can only have one of two values. true or false.

The idea is, you initialize it with false. Then as you read lines from the file, as soon as you come across an empty one, you mark it as true.
Im confused on how the syntax should be though, the concept I understand

I don't understand whats wrong with my syntax
1
2
3
4
5
bool line;
line = (.empty());


while(!line.empty()){



I think I need help with the syntax :-(
Your code is all nonsense. You're treating line as if it were a complex object, and it isn't. I think you are blurring two different concepts here.

First of all, your boolean value would not represent the line, so "line" is a poor name. Remember a boolean can only be either true or false. There is no "empty" function that can be used for booleans. It's impossible for a boolean to be empty.

The boolean would represent whether or not you found an empty line.

For the actual line, you would need another variable (probably a string).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool empty_line_found = false;
string line;

while(!empty_line_found)
{
  // get the line from the file here, put it in 'line'

  if( /*is 'line' an empty string?*/ )
  {
    // if yes, we found an empty line, so set our flag to 'true'
    empty_line_found = true;
  }
  else
  {
    // otherwise, we have a line with data in it.
    //  process the data here
    //  (ie:  do whatever you're supposed to be doing with these lines as you read them)
  }
}
So, Disch, so far so good, you the issue I have now is that the code repeats itself one time too many for example, what I was able to come up with was

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
bool empty_line_found = false;


ifstream train_in;



train_in.open("Test.txt");

train_in >> train_num;

cout <<setw(5)<<" "<<"Data for Train Number "<<train_num<<endl;
cout <<setw(5)<<" "<<"Car Length     Car Weight"<<endl<<endl<<endl;


while(!empty_line_found){
if ( train_in > 0){
train_in >> train_length;
train_in >> train_weight;
cout<<setw(12)<<train_length<<setw(15)<<right<<train_weight<<endl;
counter++;
empty_line_found= false;
}

}


I just need to know how to make it not repeat itself the last time

Thanks for all the help so far I really appreciate it
If you go here [http://www.cplusplus.com/doc/tutorial/files/]

You will see this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
Thanks, I got the program to work, it still loops one time too many, and I couldn't get the link to work
you may need to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.good())
  {
    while ( !myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
Last edited on
Topic archived. No new replies allowed.