XY Plotter Read File

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
hi

i would be able to help you if you post more of the code...because from what you have posted...i cant really help you...

and it will help if you post a sample of the file your trying to read...
i need help with selecteing a file from a list which i have only allowed the circle option to be selected but to keep things easy for now my text file only has a few random points not a whole circle. the text file just looks like this:

13 5
3 4
18 60

where 13 is the row and 5 is the column. Heres the full code minus the the functions that are not needed:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <locale>
using namespace std;
int inputFile();


const int row=21;
const int col=75;

int main()
{

int choice;

cout<<endl<<"XY Plotter"; //Heading to program
cout<<endl<<"--------------------------------------------------";
cout<<endl<<"1. Display XY Plot\n";
cout<<"2. Read Input From File\n";
cout<<"3. Read Input From Keyboard\n";
cout<<"4. Clear XY Canvas\n";
cout<<"5. Scale / Shift Figure\n";
cout<<"6. Quit Program\n"<<endl;
cout<<"Make A Selection From The Menu: ";
cin>>choice; //Input selection made by user

if (choice==2)
{
inputFile();
}

return 0;
}

int inputFile()
{
int choice2, file;

cout<<endl<<"\t**You Have Selected To Read From A File**\n"<<endl;
cout<<"Please Choose From The Following:";
cout<<"\n--------------------------------"<<endl;
cout<<"1. Enter a file name: "<<endl;
cout<<"2. Select file from a list"<<endl;
cout<<"3. Return To Main Menu"<<endl;
cout<<endl<<"Make A Selection From The Menu: ";
cin>>choice2;

if (choice2==2)
{
cout<<endl<<"*Please Select One Of The Following:"<<endl;
cout<<"1. A Circle"<<endl;
cout<<"2. A Square"<<endl;
cout<<"3. A Triangle"<<endl;
cout<<endl<<"Make A Selection From The Menu: ";
cin>>file;
cout<<"\n";

if(file==1)
{

ifstream File;
File.open("c\\temp\\sample.txt");
int i, j, x, y;
File>>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";
}

}









Last edited on
ah just realised tha by cutting it short it doesnt work, here is my full code where if(file==1) is where i am having my problem:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <locale>
using namespace std;
int canvas();
int inputFile();
int inputKeyboard();
void clear();
void scaleShift();
int quit();

const int row=21;
const int col=75;

int main()
{

int choice;

cout<<endl<<"C++ Assignment 2 Tim Gradden 16519448 - XY Plotter"; //Heading to program
cout<<endl<<"--------------------------------------------------";
cout<<endl<<"1. Display XY Plot\n";
cout<<"2. Read Input From File\n";
cout<<"3. Read Input From Keyboard\n";
cout<<"4. Clear XY Canvas\n";
cout<<"5. Scale / Shift Figure\n";
cout<<"6. Quit Program\n"<<endl;
cout<<"Make A Selection From The Menu: ";
cin>>choice; //Input selection made by user

if (choice==1)
{
canvas();
}
if (choice==2)
{
inputFile();
}
if (choice==3)
{
inputKeyboard();
}
if (choice==4)
{
clear();
}
if (choice==5)
{
scaleShift();
}
if (choice==6)
{
quit();
}
return 0;
}

int canvas ()
{
int i, j;
char plot[21][75] = {'.'};

for(i=20; i>-1; i--)
{
for(j=0; j<75; j++)
{
plot[i][j]='.';


cout<<plot[i][j];
}
cout<<"\n";

}


return main();
}

int inputFile()
{
int choice2, file;

cout<<endl<<"\t**You Have Selected To Read From A File**\n"<<endl;
cout<<"Please Choose From The Following:";
cout<<"\n--------------------------------"<<endl;
cout<<"1. Enter a file name: "<<endl;
cout<<"2. Select file from a list"<<endl;
cout<<"3. Return To Main Menu"<<endl;
cout<<endl<<"Make A Selection From The Menu: ";
cin>>choice2;


if(choice2==1)
{
char fileName;
cout<<"Please Enter The File Name: ";
cin>>fileName;
}
if (choice2==2)
{
cout<<endl<<"*Please Select One Of The Following:"<<endl;
cout<<"1. A Circle"<<endl;
cout<<"2. A Square"<<endl;
cout<<"3. A Triangle"<<endl;
cout<<endl<<"Make A Selection From The Menu: ";
cin>>file;
cout<<"\n";

if(file==1)
{
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";
}

}
if(file==2)
{
cout<<"\t\t\t\tA Square"<<endl;
int i, j;
char plot[row][col] = {'.'};
for(i = 0; i<row; i++)
{
for(j = 0; j<col; j++)
{
plot[i][j] = '.';
cout << plot[i][j];


}
cout<<"\n";
}

}
if(file==3)
{
cout<<"\t\t\t\tA Triangle"<<endl;
int i, j;
char plot[row][col] = {'.'};
for(i = 0; i<row; i++)
{
for(j = 0; j<col; j++)
{
plot[i][j] = '.';
cout << plot[i][j];

}
cout<<"\n";
}

}
}
if(choice2==3)
{
return main();
}
return inputFile();
}








int inputKeyboard()
{


int x, y;
cout<<endl<<"Please Enter A Point: ";
cout<<"X= ";
cin>>x;
cout<<"\t\t Y= ";
cin>>y;

int i, j;
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";
}
return main();
}

void clear()
{
}


void scaleShift()
{

}
int quit()
{
return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.