Openning user input file name

Hello all,

Anyone have any idea why this is looping in an error message. I am inputting the entire file name when I run the program and it just keeps falling into my "if(!inputfile)" statement. Let me know if there is anything I'm just not seeing.

Thanks.



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
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "File.h"

#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <stdio.h>
#include <limits>


using namespace std;


int main (){
	bool b;

	do{
		char a;

		cout << "Do you have equations that need to be checked for syntax errors? (y/n) :";
		cin >> a;
		cin.ignore();

		if(a == 'y' || a == 'Y'){
			string inputfilename;
			ifstream inputfile;
			     ofstream fout;
			     fout.open("exp_output.dat", ios::out);
			while(!inputfile.is_open()) {
				cout << "Input filename: ";
				getline (cin, inputfilename);
				inputfile.open(inputfilename.c_str(),ios::in);
				if(!inputfile){
					cout<< "Error: please try again. \n";
				}
			}
		    equations obj;
			obj.readFile(inputfile, fout);
			b = true;
			inputfile.close();
			fout.close();
		}
		else if (a =='n' || a == 'N'){
			cout << "\n\nNo further testing needed. Program has ended. ";
			b = false;
		}
		else{
			b = true;
			cout << "Your entry was not recognized. Please try again. \n";
		}

	}while(b == true);

	return 0;
}
Last edited on
closed account (N36fSL3A)
does the readfile function have error checking?
Anyone have any idea why this is looping in an error message. I am inputting the entire file name when I run the program and it just keeps falling into my "if(!inputfile)" statement.

The obvious reason would be that it isn't able to open the file. You don't clear the error state when you've determined there is an error state, so subsequent iterations of that loop are not going to change anything.

Is the file you're attempting to open in the same directory that "exp_output.dat" is?
Yes it is in the same directory @cire.

I moved it into the implementation file where I create the class obj. Everything is reading in fine now.

Thanks for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void equations::readFile(ifstream &inputfile, ofstream &fout)
{
        string eq;
        while(!inputfile.eof())
        {
                getline(inputfile, eq);
                fout << eq;
                for(int i = 0; i < eq.size(); i++)
                {
                        check(eq[i]);
                }
                while (!charStack.empty())
                        {
                                evaluate(charStack.top());
                                charStack.pop();
                        }
                        printMissing(fout); //print out missing brackets to console
                        mop = mcp = mob = mcb = 0;
        }
}
Last edited on
Topic archived. No new replies allowed.