while loop and read file program

i have to write a program that 2. Use file I/O to read the integers from the file. If the data file is saved in your project folder, only
the file name is needed when you declare an ifstream variable, otherwise, a path to the file is
required.
3. Suppose the ifstream variable is input, then use while(!input.eof()) for testing whether the end
of file is reached.


This is all I have so far... Please help!

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inFile;

inFile.open (" hw4dataf11.txt")

y=x%2
if (y=0) = even;
if (y=1) = odd;
}
http://www.cplusplus.com/doc/tutorial/files/

Taken from the above link...
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;
}


EDIT:
Woo my 100th post :D
Last edited on
There are so many things wrong about this it hurts a little.
@xander337 be nice, he is just starting :P

Here is a complete program that will take each line (if it is a number) and output "Odd" or "Even"

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sstream>
using namespace std;

// Determines if the string is a number
bool isNumeric(string pszInput);

int main () {
	
	// Hold the line in the file
	string line;
	
	// Open the file
	ifstream myfile ("example.txt");

	if (myfile.is_open()) {

		// While the file is good
		while (myfile.good() ) {

			// Get the current line in the file
			getline (myfile, line);

			// Verify that the line is an integer
			if (isNumeric(line)) {

				// Convert 'line' to an integer and calculate
				if (atoi(line.c_str())%2 == 0) {

					cout << "Even\n";
		
				} else {

					cout << "Odd\n";
				
				}
				
			}

		}

		myfile.close();

	} else {

		cout << "Unable to open file\n"; 

	}

	// Exit
	return 0;

}

// Determines if the string is a number
bool isNumeric(string pszInput) {

	istringstream iss(pszInput);

	double dTestSink;
	iss >> dTestSink;

	// was any input successfully consumed/converted?
	if (!iss) {
	
		return false;
	
	}

	// was all the input successfully consumed/converted?
	return (iss.rdbuf()->in_avail() == 0);

}


it might be a little shotty.. whipped it up in a couple minutes
Last edited on
No I mean. Well for one he posted his thread in the wrong forum. He didn't so much ask a question as plead for help, oh and he didn't even thinly vale the fact that this is a homework assignment that you just gave him a complete answer to. Also no code tags (but that's nitpicking.)

I didn't mean there was so much wrong with his code, I meant there was so much wrong with this thread.
Quote "xander337"
oh and he didn't even thinly vale the fact that this is a homework assignment that you just gave him a complete answer to


I didn't think of that... I learn c++ on my own so my first thoughts are that it is a personal project... >_>
Last edited on
Topic archived. No new replies allowed.