hi there, i do hope someone can help with my delima. yes this is homeowork but i am 80% through my program and only wish to get some guidance on my program.

my program has a number of things it has to do and one the things i am having trouble with is being able to read a text file with x and y points in it and then use those points to display a XY Canvas plot. i have got the plot working with user input points but cannot get it to read a file.

this is my canvas XY plot code (without the user defining the values for x and y):

        int i, j, x, y;
        char plot[21][75] = {'.'};
        plot[x][y]='+';

        for(i=20; i>-1; i--)
       {            
              for(j=0; j<75; j++)
             {
      plot[i][j]='.';
                  plot[x][y]='+';
      cout<<plot[i][j];                         }
      cout<<"\n";

where plot[x][y]='+'; will be the points i wish to read from the file.

now i tried using this in front of the previous code:

                            ifstream File;
    File.open("c\\temp\\sample.txt");
    int i, j, x, y;
    File>>x>>y;

but once the program executed it just stopped and closed.
any help would be very helpful!!!

cheers

first you must have points stored in a file before you can get them from a file.

int plot[21][75];
ofstream fout("MyFile.txt");
for (int i = 0; i < 21; i++)
{
	for (int j = 0; j < 75; j++)
	{
		plot[i][j] = (i + j);
		fout << plot[i][j] << "\n";
	}
}

there this will populate the array and output each element to a text file seperated by a newline charecture. now all you need to do is bring them in like this.

ifstream fin("MyFile.txt");
for (i = 0; i < 21; i++)
{
	for (int j = 0; j < 75; j++)
	{
		fin >> plot[i][j];
	}
}

the reason i used a newline charecture in the output is so that when i use a loop to get the data back they are all on a seperate line and the simple fin >> plot[i][j]; will work fine.
im just using generals right now becuase im not exactly sure how you want it to work in your code but just for a test this is what i wrote for a full program so you can see how it works.

#include <iostream>
#include <stdlib.h>
#include <fstream>

using namespace std;

int main()
{
	int plot[21][75];
	ofstream fout("MyFile.txt");
	for (int i = 0; i < 21; i++)
	{
		for (int j = 0; j < 75; j++)
		{
			plot[i][j] = (i + j);
			fout << plot[i][j] << "\n";
		}
	}
	int nplot[21][75];
	ifstream fin("MyFile.txt");
	for (i = 0; i < 21; i++)
	{
		for (int j = 0; j < 75; j++)
		{
			fin >> nplot[i][j];
		}
	}
	if (plot[12][13] == nplot[12][13])
		cout << "Code Works.";
	return 0;
}

hope this helps

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.