-1
每當我使用我創建的loadGame函數時,它不會顯示我以前存儲在文件中的數據。當我檢查文件時,它就在那裏。我嘗試使用全局變量和局部變量,但都沒有奏效。加載文件不工作
// Arena RPG.cpp : Defines the entry point for the console application.
// Created by Logan Daniels
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//Prototypes
void introScreen();
string loadGame();
string newGame(string, string, string);
int exit();
//constant global Variables
const string knight = "Knight";
const string mage = "Mage";
const string assasin = "Assasin";
string name, equipment, characterClass;
string gold;
int main()
{
\t introScreen();
return 0;
}
void introScreen()
{
\t int option;
\t cout << "Welcome to Arena!\n"
\t \t << "Please select an option from the list\n"
\t \t << "1.Load\n2.New Game\n3.Exit\n"
\t \t << "Number option: ";
\t cin >> option;
\t if (option == 1)
\t \t loadGame();
\t else if (option == 2)
\t \t newGame(knight, mage, assasin);
\t else if (option == 3)
\t \t exit();
}
string newGame(const string knight, const string mage, const string assasin)
{
\t system("CLS");
\t string name, equipment;
\t
\t int gold = 100;
\t string characterClass; // used in loop
\t cout << "Welcome to the character creation menu!\n"
\t \t << "Enter your name\nName: ";
\t cin.ignore();
\t getline(cin, name);
\t cout << "\nPick a class.\n1.Knight, 2.Assasin, 3.Mage\n";
\t cin >> characterClass;
\t while (characterClass != knight && characterClass != mage && characterClass != assasin)
\t {
\t \t cout << "That is an invalid character class\n";
\t \t cout << "\nPick a class.\n1.Knight, 2.Assasin, 3.Mage\n";
\t \t cin >> characterClass;
\t }
\t system("CLS");
\t cout << "Welcome to the arena!\nIts time ot begin your adventure " << name << endl << "Hit enter when redy!\n";
\t ofstream saveFile; \t \t //New game file
\t saveFile.open("Game Save Data.txt");
\t saveFile << name << endl;
\t saveFile << characterClass << endl;
\t saveFile << gold << endl;
\t saveFile << equipment << endl;
\t saveFile.close();
\t system("pause");
\t return name, characterClass, gold, equipment;
}
string loadGame()
{
\t string name, characterClass, gold, equipment;
\t ifstream saveFile;
\t saveFile.open("Game Save Data");
\t saveFile >> name;
\t cout << name << endl;
\t saveFile >> characterClass;
\t cout << characterClass << endl;
\t saveFile >> gold;
\t cout << gold << endl;
\t saveFile >> equipment;
\t cout << equipment << endl;
\t cout << name << endl << characterClass << endl << gold << endl << equipment << endl;
\t system("pause");
\t
\t return name, equipment, characterClass, gold;
}
int exit()
{
\t return 0;
}
謝謝,我意識到,當我今天早上醒來;) – Loganastan