Unwanted things in output
May 12, 2011 at 12:57pm UTC
Hello to everybody,
I am a c++ beginner, and I want to do a program that reads a file with a column with numbers, "finds" the repeated numbers and write the repetitions on other file. For example, I have a file like this:
Input file:
6
5
4
4
7
5
6
6
6
6
1
And I want the output to be:
6 0R
5 0R
4 1R
7 0R
5 0R
6 3R
1 0R
I've done the following program:
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
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int i=0;
int igual=0;
int array[30];
ifstream inFile;
ofstream outFile;
inFile.open("teste.txt" );
outFile.open("escrita.txt" );
while (inFile)
{
for (i=0;i<=10;i++)
{
inFile >> array [i];
if (array[i]==array[i+1])
{
igual++;
}
else
{
outFile << array [i] << " " << igual <<"R" << endl;
igual=0;
}
}
i++;
}
inFile.close();
outFile.close();
return 0;
}
But the output is:
6 0R
5 0R
4 0R
4 0R
7 0R
5 0R
6 0R
6 0R
6 0R
6 0R
1 0R
6 0R
5 0R
4 1R
7 0R
5 0R
6 3R
1 0R
Where the bolt values are what I want, but how can I get reed of the first zeros?
Thanks a lot for your help, in advance!
Last edited on May 12, 2011 at 1:05pm UTC
May 12, 2011 at 1:18pm UTC
You're reading the file one row at a time.
1 2
inFile >> array [i];
if (array[i]==array[i+1]
You don't know yet what array[i+1] is.
You can try reading the whole file once and then checking for repeats.
Topic archived. No new replies allowed.