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

why am i getting so many numbers from my array c++

本文关键字:数字 数组 为什么      更新时间:2023-10-16

我仍然在这个项目上工作,我已经修复了我的大部分问题。问题在使用选项B时显示出来,但在选项A中,并将数组发送到.txt文件。有很多额外的数字被添加到.txt文件中。当我计数时,它像它应该的那样显示数组中的数字,但在文本文件中它看起来是这样的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;
}

就像评论中提到的:

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

应该(至少)改为

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

同样,这个循环:

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

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