在c ++中将返回值发送到数组

时间:2014-03-08 03:26:46

标签: c++ arrays function text-files

以下是交易人员:
我正在制作一个静态发生器。该程序有一个菜单(在这个程序中没有完全开发,但我有以前的代码可以工作)它要求一个名称,然后将它存储到Character1。
然后它会提示它正在生成你的分数。函数GenerateScore()接受3个随机数,每个数字的范围是1-6,将它们加在一起并返回总和。

我想让它循环6次并将总数存储到数组Stats [6]中 然后我将Character1和数组发送到名为charactersave.txt的txt文件 它保存了我每次输入的任何名称,但是当我打开txt文件时,我得到了这个名字。我明白了

安东尼-858993460-858993460-858993460-858993460-858993460-858993460-858993460-117763858413957408144036511139574241439271911568132815670376-1177638760

非常感谢任何帮助,

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

1 个答案:

答案 0 :(得分:2)

>>只能用于std::ostream来实现流媒体行为。

更改

GenerateScore()>> Stats[i];

Stats[i] = GenerateScore();