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:

#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?
What am I doing wrong?

Thanks a lot for your help, in advance!

Edited 5 Years Ago by missscareall: n/a

For what purpose are you using array? Although you're starting out learning C++, you might want to consider using a set from the STL. (C++ Standard Template Library).

Your code appears to be assigning and comparing in array a lot more times than you have to. Or, if you want to use an array type, then search the array each time through to see if you've already seen that value.

You are reading one number at a time into the array from input file and comparing it to the next, which has not been read from the file yet (line 20). That's why your counter always stays at zero, content of array[i+1] is random.

You must read consecutive values from the file, compare them and then print accordingly.

You may consider setting your program up so that it first
1. Reads in the numbers through and array.
2. Checks the array against itself
3. then outputs any repeats it may have.

What you are currently doing is inputting a number into an array, then checking against array values that have -not- yet been initiallized.

Edited 5 Years Ago by Saith: n/a

Ok, I've understand my mistake now! Thank you so much, I'll try to read in the file and array it (:

Edited 5 Years Ago by missscareall: n/a

So, I did the array, but my input file is to big (around 100 megabytes). The program crashes. I think I have to read the file and directly write the values on the outfile..

You can do this with two variables: one for the first value in a matching sequence and one for the current value. As long as they match, increment your counter. When they don't match, print your result for the sequence, reset the counter to zero, and set and the first value to the mismatch.

A possible solution. The location of your number you are comparing for each read number could be associated with the arrays index. (i.e. 4 and array[4] ). Read in the number through a variable, then access the arrays memory location with that read in number (using the number as the index), then incrementing that location.

At which point, you are not comparing any other integers in the input file, you are just counting how many times they occur.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

After rereading the original post and seeing the output required, this solution will not work for your particular case, but may help others in similar cases that do not need to be so strict.

This article has been dead for over six months. Start a new discussion instead.