Here is your code, indented and in one file:
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
|
#include <iostream>
#include <fstream>
#include <string>
using std::ifstream;
using std::ofstream;
using std::istream;
using std::ostream;
using std::string;
using std::cout;
using std::cin;
using std::endl;
const int LAB = 10;
void readI(string input, string output);
int
main()
{
readI("input1.txt", "output1.txt");
}
void
readI(string input, string output)
{
int Checking[LAB];
int count = 0;
ifstream in(input);
ofstream out(output);
int numbers;
out << "Index\t" << "Value" << endl;
in >> numbers;
while (!in.fail()) {
count++;
for (int i = 0; i < 10; i++) {
out << i << "\t" << Checking[i] << endl;
}
in >> numbers;
}
}
|
At lines 34 & 42, you read a number into
numbers
, but never actually do anything with it. You don't store it anywhere for later printing. At lines 38-41 you print the Checking array, but you never actually store any values into the array.
The count variable tells you how many numbers you've stored. If you want to fill the Checking array, then you can read a number directly into Checking[count]:
1 2 3 4 5 6 7 8 9 10
|
in >> Checking[count];
while (!in.fail()) {
count++;
for (int i = 0; i < 10; i++) {
out << i << "\t" << Checking[i] << endl;
}
in >> Checking[count];
}
|
That's closer, but when I run it now I get:
$ ./foo
Segmentation fault (core dumped) |
The problem now is that you've allocated room in
Checking
for 10 numbers, but input1.txt contains 85. As a result, the program happily stores numbers in whatever member comes after the
Checking
array, and it causes the program to crash. As an aside, this is one of the reasons why it's usually preferable to use C++ vectors instead of arrays. Back to the problem, never fear, we can just change LAB at line 14 to, say, 100 so it's big enough.
const int LAB = 100;
Now the output still isn't right, but it's closer. Below are the first few lines of output1.txt. The underlined entries are correct:
Index Value
0 4
1 0
2 -2147196998
3 1
4 -2145400288
5 1
6 -13400
7 0
8 -2145631872
9 1
0 4
1 -13
2 -2147196998
3 1
4 -2145400288
5 1
6 -13400
7 0
8 -2145631872
9 1
0 4
1 -13
2 -12
3 1
4 -2145400288
5 1
6 -13400
7 0
8 -2145631872
9 1
0 4
1 -13
2 -12
3 0
4 -2145400288
5 1
6 -13400
7 0
8 -2145631872
9 1 |
Why is the output file so big? Because you're printing the Checking array inside the input. So your code basically does this:
1 2 3 4 5 6
|
read one number
print all 10 values in Checking
read another number
print all 10 values in Checking
read another number
...
|
You need to move the loop that prints the array outside of the input loop:
1 2 3 4 5 6 7
|
while (!in.fail()) {
count++;
in >> Checking[count];
}
for (int i = 0; i < 10; i++) {
out << i << "\t" << Checking[i] << endl;
}
|
Now when I run it, output1.txt contains:
Index Value
0 4
1 -13
2 -12
3 0
4 -7
5 3
6 -11
7 -11
8 -12
9 7 |
Dang.... Where are the other numbers?? Looking at the code, I see that you've hard-coded 10 at the ending limit when you print:
for (int i = 0; i < 10; i++) {
So how many numbers should you print? Not the whole array: you might not fill the whole array. You want to print only the numbers that are in it, and that is handily stored in
count
:
for (int i = 0; i < count; i++) {
Putting it all together, here is the code:
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
|
#include <iostream>
#include <fstream>
#include <string>
using std::ifstream;
using std::ofstream;
using std::istream;
using std::ostream;
using std::string;
using std::cout;
using std::cin;
using std::endl;
const int LAB = 100;
void readI(string input, string output);
int
main()
{
readI("input1.txt", "output1.txt");
}
void
readI(string input, string output)
{
int Checking[LAB];
int count = 0;
ifstream in(input);
ofstream out(output);
int numbers;
out << "Index\t" << "Value" << endl;
in >> Checking[count];
while (!in.fail()) {
count++;
in >> Checking[count];
}
for (int i = 0; i < count; i++) {
out << i << "\t" << Checking[i] << endl;
}
}
|