为什么我从我的数组c ++中得到这么多数字

时间:2014-03-09 03:59:10

标签: c++ arrays text-files

我还在研究这个项目,我的大部分问题都得到解决。 使用选择B时问题显示,但位于选项A并将数组发送到.txt文件。有很多额外的数字被添加到.txt文件中。当我cout时,它会显示数组中的数字,但它在文本文件中看起来像这样 Anthony201910181114-8589934604445358131768008182078541176802418196927161130726102120444535893 之前的一切 - 是正确的,但所有这些数字之后都需要去。 为什么会发生这种情况?我该如何解决这个问题? 谢谢。

int main()
{
    int Stats[6];
    char Selection;
    string Character1;
    int i;
    char keep;




    do
     {
        cout << "Hello, welcome to my character generator. Please select an option" << endl;// Menu Options
        cout << "A: Create Character Name and generate stats" << endl;
        cout << "B: Display Name and Stats" << endl;
        cout << "C: Quit" << endl;

        cin >> Selection; // Menu Selection
        cout << endl;

            if ( (Selection == 'a') || (Selection == 'A') )// if user selects a, this happens
             {
                cout << "Welcome, Before you can start your adventures you must name your character." << endl;
               do
                {
                 cout << "Please enter your a name." << endl;// prompts user to enter a name for their Caracter
                 cin >> Character1;             

                 cout << "Thank you now lets generate your stats." << endl;

                    for (i=0; i<6;i++)// I Want this to run the function GenerateScore() 6 times and input each result into the next element of Stats[6]
                        {   
                            Stats[i]=GenerateScore(); 
                        }
                    for(i=0;i<6;i++)// displays your scores to the player.
                        {
                            cout << Stats[i]<< endl;
                        }
                    cout << "would you like to keep this name and these Stats?"<< endl;
                    cout << "Y/N" << endl;
                    cin >> keep;
                    break;
                 }
                    while ( (keep=='n') || (keep=='N') );


                ofstream savecharinfo("charactersave.txt");// saves the Name and the filled array Stats[6] to the charactersave.txt file
                    if(savecharinfo.is_open())
                    {
                        savecharinfo << Character1;
                        for(int i = 0; i<6; i++)
                            {
                                savecharinfo << Stats[i]; 
                            }   
                    }
                        else cout << "File could not be opened." << endl;
             }
            else if ( (Selection =='b') || (Selection == 'B') )
              {

                cout << " Welcome back, here are is your character." << endl;

                ifstream displaycharacter("charactersave.txt");
                    if(displaycharacter.is_open())
                        {
                            while ( getline (displaycharacter,Character1) )
                            {
                                cout << Character1 << endl;
                            }
                                displaycharacter.close();               
                        }
                    else cout << "File could not be opened";
              }
            else
                break;
    }



while ( (Selection != 'c') || (Selection == 'C') ); // ends the program is c or C is entered.
return 0;
}       

int GenerateScore()
{
    int roll1 = rand()%6+2;
    int roll2 = rand()%6+2;
    int roll3 = rand()%6+2;
    int sum;

       sum=roll1+roll2+roll3;


      return sum;
}

1 个答案:

答案 0 :(得分:0)

评论中提到过:

while ( (keep='n') || ('N') );

应该(至少)改为

while ( (keep == 'n') || (keep == 'N') );

此外,这个循环:

for(int i = 0; Stats[i]; i++)

仅在Stats[i]评估为false时终止,这只会在Stats[i] == 0时发生。这个循环导致文件中出现额外的字符:代码不断访问超出边界的数组元素,直到随机超出边界元素== 0.你可能想要for(int i = 0; i < 6; i++)