Hello everyone, I have a question for my c++ code. I have an assignment where I get have a file with different expressions to which I have to convert to post, pre, and in order. However, the prof. wants a menu option that every time the user choses to convert the expression, It only shows 1 at a time. So 1st conversion converts the first expression in the file, 2nd time the conversion option is called the second expression in the file is converted, etc. My question is, is there a way to keep the position of the getline within the file after returning the expression? I want to have a function that only gets the one expression in the file to then convert it to pre,post, and in order in a different function, but when I run the code again, the getline only gets the first expression. I would want it to continue to the next line in the file in order to get the next expression for converssion when the user decideds to print the next conversion. I hope this makes sense.
ExpressionsFile.txt:
x+y*a/b
y/x*b*a/x+x+a*b
x-b*a+c/y-a
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 56 57 58 59 60 61 62 63 64 65 66
|
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
//Function Prototypes
void mainMenu();
void clearIn();
string getData();
int main() {
mainMenu();
return 0;
}
void mainMenu(){
enum option {PRINT = 1, EXIT = 2};
int option = 0;
while(option != EXIT){
cout << "\nWelcome to The Expression Tree Menu\nPress:\n\n"
"'1' - Print Expression in Post, Pre, and In order\n"
"'2' - Exit Program\n"<<endl;
cin >> option;
switch(option){
case PRINT:cout << '\n' << getData() << endl;break;
case EXIT:break;
default: clearIn(); break;
}
}
}
void clearIn() {//clears invalid inputs
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nInvalid input. Please try again.\n" << endl;
}
string getData(){
string expression;
ifstream inputFile;
// Checks for files Records to open successfully
inputFile.open("ExpressionsFile.txt");
if (!inputFile) {
cout << "Input file not found. Quitting program." << endl;
system("pause");
exit(EXIT_FAILURE);
}
// Checks if the input file is empty
if (inputFile.peek() == std::ifstream::traits_type::eof()) {
cout << "\nWARNING: The input file is empty. Please add inputs for "
"Validation\nEnding program..."
<< endl;
exit(EXIT_FAILURE);
}
if(!inputFile.eof()){//getline of the expression in file unless its at end of file
getline(inputFile, expression);
}
return expression; //here I want it to return the first expression, then once getdata is called again, it goes to the next line in the file to get the next expression
}
|
-----------------
current output:
Welcome to The Expression Tree Menu
Press:
'1' - Print Expression in Post, Pre, and In order
'2' - Exit Program
1
x+y*a/b
Welcome to The Expression Tree Menu
Press:
'1' - Print Expression in Post, Pre, and In order
'2' - Exit Program
1
x+y*a/b
Welcome to The Expression Tree Menu
Press:
'1' - Print Expression in Post, Pre, and In order
'2' - Exit Program
1
x+y*a/b
Welcome to The Expression Tree Menu
Press:
'1' - Print Expression in Post, Pre, and In order
'2' - Exit Program
2
-------------------------
Desired output:
Welcome to The Expression Tree Menu
Press:
'1' - Print Expression in Post, Pre, and In order
'2' - Exit Program
1
x+y*a/b
Welcome to The Expression Tree Menu
Press:
'1' - Print Expression in Post, Pre, and In order
'2' - Exit Program
1
y/x*b*a/x+x+a*b
Welcome to The Expression Tree Menu
Press:
'1' - Print Expression in Post, Pre, and In order
'2' - Exit Program
1
x-b*a+c/y-a
Welcome to The Expression Tree Menu
Press:
'1' - Print Expression in Post, Pre, and In order
'2' - Exit Program
2
-------------------
I want to know if there is a way to do this without having to use a string array to which I have to store each expression individually to.
Thank you for your help.