file open error

Apr 12, 2015 at 3:55pm
I am getting an error with the in.open section, it is telling me I cannot convert from a float to a stream, any insight?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
#define Length 150
ifstream in;

float Anumber[Length];

int main()
{
	float file;
	
	cout << "please enter the location of the file" << endl;
	cout << "an example would be C:/my file.txt" << endl;
	cin >> file;
	
	in.open(file);
	if (!in)
	{
		cerr << "Error file not found" << endl;
		exit(1);
	}
Apr 12, 2015 at 4:00pm
Edit: Misread the code. The file name is obviously a string, not a number right?

string file;
Last edited on Apr 12, 2015 at 4:01pm
Apr 12, 2015 at 4:03pm
So then how would I allow the user to enter the file location?
Apr 12, 2015 at 4:06pm
The file name in this case is numbers.txt
Apr 12, 2015 at 4:07pm
closed account (2LzbRXSz)
"C:/my file.txt" is an example of a string.
Apr 12, 2015 at 4:10pm
I now see the error that I'm trying to make a file type of float allow a char to be entered. But that still doesn't fix my problem. But thank you for point that out.
Apr 12, 2015 at 4:12pm
Change it from float to string. And use getline(cin, file); instead of cin >>
Apr 12, 2015 at 4:13pm
closed account (2LzbRXSz)
What error is it giving you now?
Apr 12, 2015 at 4:15pm
Thanks that did it, your awesome!
Apr 12, 2015 at 4:16pm
You're welcome^^
Topic archived. No new replies allowed.