Need Help Implementing Ignore() Into Program

Hello all,

I'm still a relatively new C++ coder and I'm having trouble implementing an ignore() function into my program.

I have a program that reads in 10 lines and 3 columns of values from a .txt file, and compares them to each other to see if they could make up three sides of a triangle.

That in and of itself hasn't proven too hard, but here's the rub:

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


On line 6, there is a T in the .txt file that makes the program go haywire. My instructor said in the assignment that we need to use ignore (256, '\n') to discard that entire line.

This is what I have so far:

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main() {
	ifstream tri;
	double side1(0), side2(0), side3(0);
	string filename;
	cout << "Please enter a filename:" << endl;
	cin >> filename;
	tri.open(filename.c_str());
	if (tri.fail()) {
		cerr << "I could not open " << filename;
		exit(1);
	}
	while (!tri.eof()) {
		tri >> side1 >> side2 >> side3;
		cout << setw(12) << side1 << " " << side2 << " " << side3 << " ";
		if (side1 < (side2 + side3) && side1 > (side2 - side3)
				&& side1 > (side3 - side2)) {
			cout << setw(12) << "is a triangle." << endl;
		} else {
			cout << setw(12) << "is not a triangle." << endl;
		}
	}
	tri.close();
	return (0);
}


Now, I don't need anyone to do my homework for me, but I've been trying to figure out how to implement the ignore() function for almost a week now and I haven't been able to figure out any ways to make it work. To be honest, I'm not even completely sure where to begin. I've tried putting the ignore() function into an if statement, but I can't figure out what expression to use into the if statement. I've also tried putting it outside of the loop, but that also did not garner any success.

If anyone could point me in the right direction of where to look or if anyone has any ideas on how to set this up, I'd greatly appreciate it.
Last edited on
http://cplusplus.com/reference/iostream/istream/operator%3E%3E/

you need to check if tri has the failbit flag after extracting, and another hint, clear before ignoring.
Ah!!! Thank you very much! That was EXACTLY the hint I needed! I didn't even think of checking failbit before.
Topic archived. No new replies allowed.