Question

I'm still working on this project and I have most of my problems fixed. The problem shows itself while using selection B, but lies in option A and sending the array to the .txt file. There are a lot of extra numbers being added to the .txt file. When I cout, it displays the numbers in the array like it should, but in the text file it looks like this Anthony201910181114-8589934604445358131768008182078541176802418196927161130726102120444535893 everything before the - is correct, but all of these numbers after need to go. Why is this happening and how can I fix this? Thank you.

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;
}
Was it helpful?

Solution

Like was mentioned in the comments:

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

should (at least) be changed to

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

Also, this loop:

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

Will only terminate when Stats[i] evaluates to false, which will only happen when Stats[i] == 0. This loop is causing the extra chars in the file: the code keeps accessing out of bounds array elements until randomly the out of bounds element == 0. You probably want for(int i = 0; i < 6; i++)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top