Whats wrong with this code?

I'm trying to add the functionality of allowing the user to repeat my program without having to start it up again. So what I've done is put all of my main program code into a new function, int program(). Added all the function headers and declarations and variables to a new cpp file and put this into main():

int main()
{
while (cont)
{
ok = false;
choice = -1;
c[0] = 'z';
if (prompt() == -1) //was 3
{
cout << endl << message(cout) << endl;
return 0;
}
cout << choose() << endl;
indicator *= -1;
}
return 0;
}

This was the template version i used to make sure the loop was working correctly. It works fine the way it is here.

But when I changed prompt() to program() everything started going haywire.
(all I did mind you was change prompt() to program() and removed the line with choose().)

It will still work 1 time, but when it starts to loop weird things will happen like skipping over user input etc and not output anything to the files it is supposed to. I have a feeling the problems might be caused by the fact that i'm opening several files, and using the same variables to open different files on each consecutive repeat. So I've made sure that I'm manually closing each file, but it still doesn't work. In fact, when I tried debugging I added lines such as:
infile.open("test.txt");
infile.seekg(0, ios::beg);
if (infile.eof()) cout <<"infile.end of file!!!!!" << endl;
(and the results show that infile will be open, but it will be eof
so I know this is the error. but what can I do differently so I won't have this error?

in case your wondering here are the functions from the good code:

int prompt()
{
cout << "Enter 1 for option 1. " << endl;
cout << "Enter 2 for option 2. " << endl;
cout << "Type 'Exit' to exit. " << endl;
do
{
if (indicator > 0)
cout << ">";
else
indicator *= -1;
cin.getline(c, 1000);
input = c;
if (upper(input) == ex)
{
return -1;
}
else
{
choice = c[0] - 48;
if ( (choice == 1) || (choice == 2) )
{
ok = true;
}
}
} while (!ok);
return choice;
}

int choose()
{
cout << "Enter a filename. " << endl << ">";
cin >> filename;
outfile.open(filename.c_str());
if (choice == 1)
{
outfile << "wtf" << endl;
}
if (choice == 2)
{
outfile << "hell world " << endl;
}
int n;
outfile.close();
return n;
}

string upper(const string & old)
{
string temp;
int length = old.length();
char test[length]; char *p;
old.copy(test, length);
p = test;
int i = 0;
for (p; *p != '\0'; ++p)
{
*p = toupper(*p);
if (i < length)
temp += *p;
++i;
}
return temp;
}

I'd rather not share the code for program() as its a private project I'm working on. But any help will be greatly appreciated.

What it boils down to is this:
my code looks something like this:

void function()
{
infile.open(instring.c_str(), ios::binary);
outfile.open(outstring.c_str(), ios::binary);
if (infile.is_open() cout << "infile is open. " << endl;
infile.seekg(0, ios::beg);
if (infile.eof()) cout << "infile.eof. " << endl;
infile.close();
outfile.close();
}

starting with the second time the files will be open, but they will be eof,
what gives? causing me lots of headaches... thanks guys!

Just guessing, but I think your problem might be that your are using cin >>, which leaves a \n at the end of the line. Use getline(cin, <some std::string here>) instead.
ok well i've figured out what it probably is...

I had to call a member function, infile.clear() after each time I opened a new file. (which resets the flags of eof etc).

But there's still a little bit of a problem as it still says eof on some cases.

I'm writing an encryption program and I want the user to be able to encrypted any # of files (provided he has enough keys) and also if he'd like to decrypt any # of files.... all without having to restart the program.

I have it now so, after adding the .clear() to all my ifstreams and ofstreams the user can encrypt OR decrypt any # of files.... but he can't go back and forth between encrypt and decrypt or the same error message will occor: eof.... (even though i've done .clear already. )
Topic archived. No new replies allowed.