extract

closed account (ohf3T05o)
I am a beginner and I ALWAYS have the toughest time doing I/O files. It's extremely frustrating. It "seems" it should be so simple.

The program should find a code from a list of numbers. These numbers are from 0 - 9, and after each number is a space in the file. Your job is to extract a special code containing only 10 of those numbers. For the number to be part of the code, it should be divisible by 2. After extracting 10 numbers divisible by 2 for the code, write those 10 numbers to the file to form the expected code.

Input file is ("question.txt")
Output should be ("code.txt")


Should this contain a "for loop" or If/else ?
Here's what I did . .
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
/
   // int numbers, total, counter;

 
     
    ifstream inFile;
            inFile.open ("question.txt"); 
    outFile.open ("code.txt");
            if (!inFile)
    cout << "The file can't be opened";
            else
            {
                while (!inFile.eof()) 

                { int numbers;
                    inFile >> numbers;
                  i = numbers;
                }

            }  
   for (int i % 2 = 0; i <= 10; i++)

 inFile.close();
 outFile.close();
First of all, without any sense of scope, it's hard to make a mental picture of how this code is used - I'm guessing in the main function.

I would split things up into functions.
line 13: while(!inFile.eof())
This is a guaranteed way to get buggy code. I would do this differently, like with std::getline() or even by placing the reading of the file in a while loop condition, to assure that the loop only gets executed if the ifstream was able to read without setting an error flag.

Line 21: for(int i % 2 = 0; i<=10; i++)...
This line makes very little sense. First of all, what is int i % 2 = 0 supposed to mean? I can only assume that you somehow thought this would limit your extracted numbers to be divisible by two, since that's what your assignment asks of you. Secondly, you'll get an out-of-bounds with the i <=10 condition. Thirdly, I see no braces.

Here's some code I slapped together. If I understood you correctly, the code is made up of the first ten numbers which are divisible by two. If there are less than ten numbers that are divisible by two (or numbers in general), then the rest of the code is filled with zeros.

Also, you didn't explicitly state that this is a homework assignment, which I'm assuming it is. For your own benefit, don't copy and paste. If you do, you're guaranteed to get caught. I wrote this so that you can get a taste for how I would do it.

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

const unsigned short NUM_NUMBERS = 10;

void extractCode(const char* filename, unsigned short code[]/*assuming the numbers in the file are between 0 - 65535*/) {
	std::ifstream stream(filename, std::ios::binary);/*Create an input file stream*/
	if(!stream) {return;}/*If any error flags are set, return.*/

	unsigned short temp = 0, index = 0;
	while((index<NUM_NUMBERS) && (stream >> temp)) {/*Do the following while we haven't gotten ten numbers, and while the stream can successfully read into temp*/
		if(temp % 2 == 0) {/*if temp is divisble by two*/
			code[index++] = temp;/*then it's part of the code*/
		}
	}
}

void writeCode(const char* filename, unsigned short code[]/*assuming the numbers in the file are between 0 - 65535*/) {
	std::ofstream stream(filename, std::ios::binary);/*Create an output file stream*/
	if(!stream) {return;}/*If any error flags are set, return.*/

	for(unsigned short i=0; i<NUM_NUMBERS; ++i) {/*for loop to write the code to a file*/
		stream << code[i];
		if(i!=NUM_NUMBERS-1) {
			stream << " ";
		}
	}
}

int main(int argc, char* argv[]) {

	unsigned short code[NUM_NUMBERS];/*Create an array of ten unsigned shorts*/
	memset(code, 0, sizeof(code));/*memset to zero, windows hurr durr*/

	extractCode("numbers.txt", code);/*extract the code from the file, using the method from above*/
	writeCode("code.txt", code);/*write the code file, using the method from above*/

	std::cin.get();
	return 0;
}
Last edited on
closed account (ohf3T05o)
Thx for giving me some insight but I'm still not that advanced yet.. I'll keep looking over it and studying.
Topic archived. No new replies allowed.