Console Prog

This is a discussion on Console Prog within the C++ Programming forums, part of the General Programming Boards category; Hi, im pretty new to C++, Ive just latly been getting into it. I want to make a simple text ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    3

    Question 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();
    
      }
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    3
    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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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')

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  2. Help on simple text console prog...
    By Azh321 in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 06:03 AM
  3. When prog is compiled the Console becomes FULLSCREEN
    By IrishSlasher in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2002, 08:25 AM
  4. Color Glitches in Console Prog.
    By Darkflame in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2002, 10:52 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21