English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

The question is, Read the numbers contained in a sequential access file, then write only the even numbers to a new sequential access file. Code the program so that it reads the numbers from thennumbers.txt file. the program should write only even numbers to a new sequential file named evennumbers.txt.

I know to open a notepad window and type in the numbers and save it. My problem is writing the code to get those numbers from that file. This is my last and only programming class and I really really need help. Thankyou for all of your help.

2006-11-26 13:44:41 · 5 answers · asked by guy 2 in Computers & Internet Programming & Design

5 answers

This is an unpolished program that should fit the bill. It assumes thenumbers.txt is in the same folder the program is run from. Also it does no error checking. It would do you well to visit some C++ sites and understand what this is doing though.

#include
#include
#include
#include

using namespace std;

int main() {
ifstream readFrom;
ofstream writeTo;
readFrom.open("thenumbers.txt");
writeTo.open("evennumbers.txt");

string line;
int number;
do {
getline(readFrom, line);
if (line.compare("") == 0) {
continue;
}
std::stringstream ss(line);
ss >> number;
if (number % 2 == 0) {
writeTo << number << endl;
}
} while(!readFrom.eof());
readFrom.close();
writeTo.close();
}

2006-11-26 14:19:30 · answer #1 · answered by saltry 1 · 0 0

Hi;

I don't have the time to help you right now but I found this to be very helpful:

http://www.cplusplus.com/

Look under reference. You'll need to include stdio.h


I think that there are some expamples here:
http://www.cplusplus.com/src/

What you want to do is such a common beginners task that I'm sure that you can find good examples by searching....

2006-11-26 22:02:39 · answer #2 · answered by modulo_function 7 · 0 0

There is an easyReader for java, I don't know abot c++ but look up input in and api, its online search for a tutorial.

2006-11-26 21:49:26 · answer #3 · answered by windfishfighter 3 · 0 0

what do i get get for writing your program?

2006-11-26 23:09:44 · answer #4 · answered by ERIASM 1 · 0 0

you sure do like that copy/paste don't ya?

2006-11-28 00:47:02 · answer #5 · answered by Anonymous · 0 0

fedest.com, questions and answers