severe issues

Discussion in 'Programming General' started by mopar-user, Apr 2, 2009.

severe issues
  1. Unread #1 - Apr 2, 2009 at 10:56 AM
  2. mopar-user
    Joined:
    Jul 23, 2007
    Posts:
    1,188
    Referrals:
    1
    Sythe Gold:
    0

    mopar-user Guru
    Banned

    severe issues

    Code:
        string name;
        int menuChoice, attackMenu, shopMenu, saveLoad;
        int level = 1, counter;
        int exp = 0, expGain = 25;
        int gold = 0;
        int health = 100;
        int enemyHealth = 100;
        int damage, enemyDamage;
        char again;
        ofstream Name, Level, Exp, Gold, COUNTER;
        ifstream NAME, LEVEL, EXP, GOLD, Counter;
        
        Counter.open("counter.txt",ios::in);
        {
                    if(Counter)
                    {
                               Counter >> counter;
                    }
        }
        Counter.close();
        
        cout << "Press 1 to load previous save\nPress 2 to start new game\n";
        cin >> saveLoad;
        
        switch(saveLoad)
        {
                        case 1:
                             if(counter == 1)
                             {
                             NAME.open("name.txt",ios::in);
                             {
                                      if(NAME)
                                      {
                                              system("cls");
                                              getline(NAME,name);
                                      }
                                      else
                                      {
                                          cout << "ERROR";
                                          system("PAUSE");
                                          system("cls");
                                          return EXIT_SUCCESS;
                                      }
                             }
    Please explain why the name from the file will not be assigned to name.

    EDIT:: This is just partial code. All the rest works perfect. I just can not get the name to be assigned to the variable.

    EDIT:: BUMP! Please help me out here guys.
     
  3. Unread #2 - Apr 3, 2009 at 12:03 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    severe issues

    It seems to work for me after I added all the includes and closed open braces and such. In the future if you're going to post partial code and want help with it please post code that compiles. It's nice enough of people to be willing to help without having them spend extra time adding the missing parts of code you didn't post so that it will compile.

    Here's the code I have and it puts the name in the file into "name" properly. I set the "counter" variable manually to 1 because I don't understand how/when it is one in your program since you've only included a small part of a larger program.

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    	int main() {
        string name;
        int menuChoice, attackMenu, shopMenu, saveLoad;
        int level = 1, counter;
        int exp = 0, expGain = 25;
        int gold = 0;
        int health = 100;
        int enemyHealth = 100;
        int damage, enemyDamage;
        char again;
        ofstream Name, Level, Exp, Gold, COUNTER;
        ifstream NAME, LEVEL, EXP, GOLD, Counter;
    
        Counter.open("counter.txt",ios::in);
        {
                    if(Counter)
                    {
                               Counter >> counter;
                    }
        }
        Counter.close();
        counter = 1;
        cout << "Press 1 to load previous save\nPress 2 to start new game\n";
        cin >> saveLoad;
        
        switch(saveLoad)
        {
                        case 1:
                             if(counter == 1)
                             {
                             NAME.open("name.txt",ios::in);
                                      if(NAME)
                                      {
                                              system("cls");
                                              getline(NAME,name);
                                      }
                                      else
                                      {
                                          cout << "ERROR";
                                          system("PAUSE");
                                          system("cls");
                                          return EXIT_SUCCESS;
                                      }
                             }
    		}
    		cout << name;
        system("PAUSE");
    	}
    
     
  5. Unread #3 - Apr 3, 2009 at 4:05 PM
  6. mopar-user
    Joined:
    Jul 23, 2007
    Posts:
    1,188
    Referrals:
    1
    Sythe Gold:
    0

    mopar-user Guru
    Banned

    severe issues

    ok well I figured it out. Sorry about partial code. Was in a hurry and had to leave class.

    BTW, the counter is read in from a file. It starts out 0 until you save, which then changes it to 1 and saves it back to the file.
     
  7. Unread #4 - Apr 3, 2009 at 8:25 PM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    severe issues

    Ah I see. Well you should consider looking into binary file writing if you don't want people to just be able to modify their saved data.
     
  9. Unread #5 - Apr 6, 2009 at 8:00 AM
  10. mopar-user
    Joined:
    Jul 23, 2007
    Posts:
    1,188
    Referrals:
    1
    Sythe Gold:
    0

    mopar-user Guru
    Banned

    severe issues

    Can this be done in DevC++? Also, can you explain how to do this? Thanks Null.
     
  11. Unread #6 - Apr 6, 2009 at 8:56 AM
  12. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    severe issues

    Dev-C++ is just the editor you use to write your code (and it also happens to have a buttons for compiling with the included MingW compiler.

    So yes, you should be able to do binary file writing/reading since it is a part of C/C++ standards. Check out the documentation and examples below.

    http://www.cplusplus.com/doc/tutorial/files.html
    http://www.codersource.net/cpp_file_io_binary.html
     
  13. Unread #7 - Apr 6, 2009 at 2:23 PM
  14. mopar-user
    Joined:
    Jul 23, 2007
    Posts:
    1,188
    Referrals:
    1
    Sythe Gold:
    0

    mopar-user Guru
    Banned

    severe issues

    Thanks for the links Null. Really helpful. You should definitely be a programming mod or something lol.
     
< I think I'm starting to get it, but have some questions - public classes | Paying you to program $$$ >

Users viewing this thread
1 guest


 
 
Adblock breaks this site