Opening file multiple times results in error

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?

http://dl.getdropbox.com/u/285110/stack.zip is the code
What?^.-...
Except for one error, it works fine for me.

Your error is on lines 64-65 of main.cpp, which should read:
1
2
            while( 	(str!="exit") && (str!="quit") && 
					(str!="EXIT") && (str!="QUIT") && ( !file.eof() ) ) {
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):
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
67
68
69
70
71
72
73
#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")
    {
      ...
    }
    else if (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;
}

Hope this helps.
Thanks, I was planning on doing something similar later on-although I didn't think to make it a function <_<

Hmm...it's weird. I'd tried that in the past, but It didn't work. But I just did a complete rebuild using that change, and it worked! Thank you!
Topic archived. No new replies allowed.