Noob in dire need with 'ignore' function -

Hello, new coder here.

I'm having trouble implementing the 'ignore(256,'\n)' function into my code.

My program reads these numbers from a file:

1
2
3
4
5
6
7
8
9
10
11
31.50 30.27 94.45
 8.10 41.81 77.74
87.24 59.21 88.91
73.89 84.42 10.28
55.85 18.12 80.05
55.85 18.12 T80.05
23.41 21.19 77.13
85.08  0.79 54.69
28.77 34.22 38.47
37.93  2.49  6.31
49.63 23.57 22.56


And using my program it will print these out as well as printing out whether or not they are a triangle.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>	


using namespace std;

int main ()
{
	
	float  a, 
	       b,
	       c;
	string filename;
	ifstream lol;
	
	cout << "Enter the name of the input file: ";
	cin >> filename;

	lol.open(filename.c_str());

	if(lol.fail())
	{
	    cerr << "Error opening input file";
	    exit(1);
 	}

	lol >> a;
	lol >> b;
	lol >> c;
	
	while(!lol.eof())
	{
            cout << setw(12) << a << " " << b << " " << c << " ";
	   
		if (a<b+c && b<a+c  && c<b+a && a>b-c && b>a-c && c>b-a)
		{
			cout << "Triangle" << endl;
		}
		
		else
		{
			cout << setw(12) << "Not Triangle" << endl;
		}
	

	    lol >> a;
	    lol >> b;
	    lol >> c;     

	}

lol.close();

return 0;

} 


Now, I've tried just reading the first 5 lines from the file, and my output from my program is fine. However, on line six there is a 'T' that once my program reads it - everything goes haywire.

I've tried adding another else if statement including the ignore function as well as placing it inside/ouside the while loop but I can't seem to figure out exactly how to implement it properly... Or even use it for that matter.

Any tips/help?
Last edited on
You probably need to review ifstream and the ">>" operator. Once a stream gets hosed, you need to check to see if it is still valid and handle the situation.
Hmmm.
Topic archived. No new replies allowed.