in_stream strings

Hello all,

I'm trying to learn the basics of C++. I've been working on a project that is suppose to prompt the user to either enter numbers into an array and count them, or the user inputs a file. I've tried input the file as a string but everytime i try to run the program my default error message runs saying there is no file (Yes the file is stored in the same directory as my program).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void FromFile(int a[], int size, int& number_used)
{   
    string file_name;
    ifstream in_stream;
    cout <<"Please enter a file name.\n";
    cin >> file_name;
	in_stream.open(file_name.c_str());

	if (in_stream.fail()); 
    { 
		cout << "There is no input to recieve from file!\n";
		exit(1);
	}
	
	string str = "";
    char next_symbol;
	bool x = true;
	do 
    {
        in_stream.get(next_symbol);
        if  (next_symbol == ' ') {
              int tmp = 0;
              tmp = atoi(str.c_str());
              a[number_used] = tmp;
              number_used++;
              str = "";
        } 
        else if (in_stream.eof()){
              if(str != ""){
              int tmp = 0;
              tmp = atoi(str.c_str());
              a[number_used] = tmp;
              number_used++;
              str = "";
              }
             x = false;
        }
        else{
             str = str + next_symbol;        
        }
	} 
    while (x);


I'm not worried about making a dead line, I just want to learn from my mistakes (I've uploaded only the void function to read from a file).
please forgive me, if anyone was wondering what was happening with this program the ";" shouldn't be after in_stream.fail()); it should be in_stream.fail()). Such a small thing eh?
Topic archived. No new replies allowed.