将返回谷发送到C 的数组

Sending the return vale to an array in c++

本文关键字:数组 返回      更新时间:2023-10-16

这是交易的人:
我正在制作一个统计生成器。该程序具有菜单(在此程序中未完全开发,但是我有以前的代码可以使用),它要求一个名称,然后将其存储到partare1。
然后提示它正在生成您的分数。该函数生成CoratesCore()将3个随机数,每个范围1-6,将它们添加在一起并返回总和。

我希望它循环6次,并将总计存储到数组统计中[6]。
然后,我将字符1和数组发送到txt文件,称为trameave.txt。
它每次都保存我输入的任何名称,但是当我打开TXT文件时,我会得到它。我得到这个

对此的任何帮助将不胜感激,

#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;
}

>>只能用于std::ostream实现流动行为。

更改

GenerateScore()>> Stats[i];

to

Stats[i] = GenerateScore();