Question

Here is the deal guys:
I'm making a stat generator. The program has a menu(not fully developed in this program, but I have previous code that will work)It asks for a name and then stores it to Character1.
Then it prompts that it is generating your scores.The function GenerateScore() takes 3 random numbers with the range 1-6 each, adds them together and returns the sum.

I want this to loop 6 times and store total into the array Stats[6].
Then I am sending the Character1 and the array to the txt file called charactersave.txt.
It saves whatever name I input each time but I get this when I open the txt file. I get this

anthony-858993460-858993460-858993460-858993460-858993460-858993460-858993460-117763858413957408144036511139574241439271911568132815670376-1177638760

Any help on this would be greatly appreciated,

#include <iostream>
#include <cstring>
#include <array>
#include <string>
#include <fstream>



using namespace std;


int GenerateScore(); 



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



        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;
        do
         {

            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;
                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]
                    {   
                        GenerateScore()>> Stats[i];

                    }

                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; Stats[i]; i++)
                            {
                                savecharinfo << Stats[i]; //writing numbers of values2 in the file
                            }   
                    }
                        else cout << "File could not be opened." << endl;


                break;// this is unfinished after this point
             }
         }
while ( (Selection != 'c') || (Selection == 'C') ); // ends the program if c or C is entered.


system("PAUSE");

    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

>> can only be used for std::ostream to achieve streaming behavior.

Change

GenerateScore()>> Stats[i];

to

Stats[i] = GenerateScore();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top