-
Console Prog
Hi, im pretty new to C++, Ive just latly been getting into it. I want to make a simple text game. All I need to know is how to save/load variables perhaps in a txt file? I tryed some source code from a tutorial that worked to an extent but was realy sloppy. I included most of the code for my prog, just took out a few unneed lines. Thanks!
Code:
#include <iostream.h>
#include <conio.h>
int choice;
void main() {
cout << "Welcome to Monster .Net!!!\n";
cout << "1. Fight 2. Eat 3. Sleep 4. Stats \n";
cout << "5. Move North 6. Move South 7. Move East 8. Move West\n";
cout << "11. Save 12. Quit\n";
cin >> choice;
while ( choice == 1 || 2 || 3 )
{
switch ( choice )
{
case 1:
cout << "Hahaha\n";
cout << "\n";
cout << "\n";
break;
case 2:
cout << "Hahaha\n";
break;
case 3:
cout << "Hahaha\n";
break;
default:
cout << "\n";
main();
}
main();
}
}
-
Sorry for not describing my problem that much. I want it to check for a .txt file and if it found then go to main(), if not found then go to some other function that I made that will create a txt file
-
Read about file input/output in your turorial/text. Basically, in C++, you can declare an fstream object to both read and write to file or an ifstream object to read from file only or an ofstream object to write to file only. The file streams have a number of flags you can use to tell the system whether you want to open the file in text mode or binary mode, and how you want to write to file (append, random, whatever). You can use the is_open() method to determine whether the stream object was associated with the file you wanted or not. All of this should be covered in the tutorial/text, however.
Using the above tools you shouldn't have to call main() outright. That's not a good idea, if it's even legal.
Also, the while conditional will need to be:
while(choice == 'a' || choice == 'b' || choice == 'c')
-
Yes, calling main() recursively is illegal, and saying void main isn't so clever either.
main returns an int
> while ( choice == 1 || 2 || 3 )
Try
while ( choice == 1 || choice == 2 || choice == 3 )
Additionally, there is no reason at all for choice being global.