For this assignment, I need to create and populate a stack based on either user input (which works fine) or file. If it's in a file, then I have to populate with one data file and run commands with another.
The problem is that after I compile, if I choose manual it works fine but file gives a ton of errors.
It doesn't appear to be getting the string out of file the second time it's opened, can anyone help?
Remember, you want to continue reading while all of those conditions are true.
By the way... you don't really need two separate loops for standard input and for file -- both are istream objects so you could easily code something like this (simplified significantly for illustration):
#include <cctype>
//-----------------------------------------------------------------------------
// Convert a string to lowercase
//
string lowercase( const string& s )
{
string result( s );
for (unsigned n = 0; n < s.size(); n++)
result[ n ] = tolower( s[ n ] );
return result;
}
//-----------------------------------------------------------------------------
// Manipulate the stack using commands from the input stream.
//
int run_cmds( Stack& s, istream& ins )
{
string str;
while ((str != "quit") && (str != "exit"))
{
getline( ins, str );
str = lowercase( str );
string cmd = ...
if (cmd == "push")
{
...
}
elseif (cmd == "pop")
...
}
}
//-----------------------------------------------------------------------------
int main()
{
Stack stack;
string str;
int result = 0;
cout << "Do you want to input manually or through a file? " << flush;
getline( cin, str );
if (lowercase( str ).find( "manual" ) != string::npos)
{
result = run_cmds( stack, cin );
}
else
{
ifstream file;
do {
cout << "Please enter the filename to populate the stack with:\n==>";
...
} while (!file.is_open());
...
file.close();
do {
cout << "Please enter the filename for the list of commands:\n==>";
...
} while (!file.is_open());
result = run_cmds( stack, file );
}
cout << "Press ENTER to quit..." << flush;
cin.ignore();
return result;
}