• Log in or sign up for an account here: https://www.sythe.org/login

severe issues

mopar-user

Guru

Banned
Joined
Jul 23, 2007
Sythe Gold
0
Messages
1,188
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.
 
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");
	}
 
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.
 
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.
 
Can this be done in DevC++? Also, can you explain how to do this? Thanks Null.
 
Thanks for the links Null. Really helpful. You should definitely be a programming mod or something lol.
 
Runescape Gamblingrunescape gambling
Back
Top