I need help creating a program. I have a text file that is 0's and 1's which create a picture. There are 33 rows and 50 columns. Every row contains 0's and 1's. what i need to do is do a program that will read the zeroes and ones and display it as follows: 2 4 5 2 9, this on my text file would show 0011110000011000000000. This is what i have so far but it is not working

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  string filenameIn="house.txt";
  string filenameOut="casa.txt";
  const int elements = 1650;
  int i, house[elements], count = 0;
  double avg;
  ifstream inFile;
  ofstream outFile;
  
  inFile.open(filenameIn.c_str());
  outFile.open(filenameOut.c_str());
  
  if (inFile.fail())
  {
  	cout<<"\nThe file was not successfully opened."
		<<"\nPlease check that the file currently exist."<<endl;
	exit(1);
   }
   
   if (outFile.fail())
   {
   	cout<<"The out file was not successfully opened."<<endl;
	exit(1);
   }
  inFile>>house[elements];
 	  
  if(inFile.good())
  { 
  for (i = 0; i < elements; i++)
  
  	if(house[i]!=house[i+1])
		outFile<<house[i];
		count++;
  
   }
  return 0;
}

Recommended Answers

All 5 Replies

I don't understand what you're trying to do in the following line:

inFile>>house[elements];

But if you're trying to read all the data from the file into the array using that one line then it's wrong. You need to use a loop to enter the data into each element of house (from house[0] to house[end]) one number at a time.

so can i just take:

inFile>>house[elements];
out of the program? As for the for loop can you help me with that? Am i going to have another for loop since i have one already or is that incorrect?thanks for your post

Well, you want to have each element of the array store a single number, right? So you'll have to have at least one loop to go through the file and place the data into the array. You can use the for loop if your data is always the same length or a while loop if you have variable length data.

So, if you don't know how large the data section is, but that it will be less than some number... say 5000, then you can do something like this:

int main()
{
    ifstream inFile;
    inFile.open("data.txt");
    int data[5000];
    int current = 0;

    while (inFile >> data[current] && current < 5000)
    {
        cout << data[current] << endl;
        current++;
    }

    return 0;
}

Now, if you want you can add all your extra code within the while loop (or for loop... whatever you choose) and do your comparisons, write to the output file or whatever you want. So really, you only need one loop but you can do it in 2 loops if you really want.

My main point was that if you do something like:

inputFile >> data[100]

Then you're placing one single digit into one single location, location 100 to be precise. You're not putting the entire file into the array. If you're not convinced then try to print your array from your original code and you'll see that you get a bunch of garbage.

Well i know that there will always be 1650 elements in the array. What would i need to write to let the program read from the beginning to the end of the file. Yes i do see what you are talking about with the inFile part. When i run the program it does give me a bunch of garbage. How would i need to place the inFile part so that it will read all of the text file?

Well, you know that we're assigning one value from your file to your array via: inFile >> data. So, as long as you put it in any loop then you're done. You can do it in a for loop if you want.


for (int i = 0; i < END; i++)
{
      inFile >> data[i];
     //Do some comparisons, output etc here
}

That's all there is to it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.