I am trying to get a file to open that the user puts in. "filename" is the variable for the name of the file the user wants to open and read data from. The code compiles and works, but does not open the file correctly, and so the user can't get what is from whatever file it is the user is trying to open. It jumps to the else at the bottom and says it cannot open the file. The file is in the same folder/directory as the program.
also, is there a way to make it stop reading in the data once there is not more data in the file? It is just reading in two sets of numbers, (x,y) coords.
Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include <cmath>
#include <fstream>
#include <ctime>
using namespace std;
void delaysec(double seconds)
{
time_t startTime, stopTime;
double delTime;
time( &startTime );
do{
time( &stopTime );
delTime = difftime( stopTime, startTime );
}while( delTime< seconds );
}
double distance(double x1, double y1, double x2, double y2)
{
double dist;
dist = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
return dist;
}
int main(void){
double x[200], y[200], x1, y1, x2, y2;
int m, n, o, p;
char filename[45];
double dist1, dist2, dist3;
m = 0;
n = 0;
o = 0;
p = 0;
ifstream file;
cout<<"Enter file name: "; <--- from here
cin>> filename[45];
file.open(filename);
if(file.is_open()){ <--- to here is where i'm having trouble
file >> x[n] >> y[n];
x1 = x[200];
y1 = y[200];
dist1 = distance(0.0,0.0,x1,y1);
if(dist1 > 46.0){
cout<<"OUT OF RANGE";
m = 1;
}
dist2 = distance(0.0,0.0,x1,y1);
if(dist2 < 6.0){
cout<<"TOO CLOSE TO CENTER";
m = 1;
}
dist3 = distance(-10.0,-10.0,x1,y1);
if(dist3 < 3.0){
cout<<"ROBOT HOME; ENDING PROGRAM";
m = 1;
}
if(m != 1){
cout<<"(" << x1 <<", " << y1 <<")"<< endl;
}
delaysec(2);
while(p < 1 && o == 0){
n++;
m = 0;
if(x == 0 && y == 0){
o = 1;
}
dist1 = distance(0.0,0.0,x1,y1);
if(dist1 > 46.0){
cout<<"OUT OF RANGE";
m = 1;
}
dist2 = distance(0.0,0.0,x1,y1);
if(dist2 < 6.0){
cout<<"TOO CLOSE TO CENTER";
m = 1;
}
dist3 = distance(-10.0,-10.0,x1,y1);
if(dist3 < 3.0){
cout<<"ROBOT HOME; ENDING PROGRAM";
m = 1;
}
if(m != 1){
cout<<"(" << x <<", " << y <<")" << endl;
}
delaysec(2);
}
}
else{
cout<<"could not open file specified";
}
while(!_kbhit());
}