如何将随机数放入数组然后放入文本文件

时间:2015-05-16 19:39:46

标签: c++ arrays text random void

所以我要做的是将一个从十到十的随机数生成器放入一个包含50个元素的数组中,然后将其放入一个文本文件中。我的问题是我为代码和生成器编写的代码有错误,我无法理解如何将其放入文本文件中。

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include<fstream>

using namespace std;

void menu();
string createFile();
void displayNumTotalAverage(string);
void displaySortedNums();
void SearchNum();
void displayLargestNum();
void appendRandomNum(string);
void exit();
void CreateFile();
void printFunc(int[]);
void fillFunc(int[]);

int main()
{
    menu();
    string FileName;
    //createFile();
    //makeRandomNum();

    system("pause");
    return 0;
}
void menu()
{
    int choice;
    string FileName;
    do
    {

        //program output 
        cout << "** MENU **" << endl << endl;

        cout << "Curret Data File: " << endl << endl;

        cout << "(1) Select / create data file (.txt file extention will be added automaticly)" << endl;
        cout << "(2) Display all numbers, total and average" << endl;
        cout << "(3) Display all numbers sorted" << endl;
        cout << "(4) search for a number and display how many times it occurs" << endl;
        cout << "(5) display the largest number" << endl;
        cout << "(6) Append a random number(s)" << endl;
        cout << "(7) Exit the program" << endl << endl;

        //user input
        cout << "Menu Choice: ";
        cin >> choice;

        while (choice > 7 || choice < 1)
        {
            cout << "Menu Choice: ";
            cin >> choice;
        }

        switch (choice)
        {
        case 1:
            cout << "Choice 1";
            createFile();
            break;

        case 2:
            cout << "Choice 2";
            displayNumTotalAverage(FileName.c_str());
            break;

        case 3:
            cout << "Choice 3";
            break;

        case 4:
            cout << "Choice 4";
            break;

        case 5:
            cout << "Choice 5";
            break;

        case 6:
            cout << "Choice 6";
            appendRandomNum(FileName.c_str());
            break;

        }


    } while (choice != 7);


}

string createFile()<----------------------------------------------------(this)
{
    cout << "Create File - Option 1" << endl;
    string FileName;
    ifstream inFile;
    cout << "Name of data file: ";
    cin >> FileName;
    FileName = "C:\\Users\Wizard\Libraries\Documents\Final Project" + FileName;
    inFile.open(FileName + ".txt");
    if (inFile)
    {
        cout << FileName;
    }
    else
        cout << "File not found, creating file.";

    system("PAUSE");
    return FileName;
}

void displayNumTotalAverage(string FileName)
{
    ifstream inFile;
    cout << "Display Number Total Average - Option 2" << endl << endl << endl;
    inFile.open("C:\\Users\Wizard\Libraries\Documents\Final Project" + FileName + ".txt");
    int num;
    int total;
    cout << "Display Number Total Average function" << FileName;
    double average;
    bool containsNum = false;
    inFile.open(FileName + ".txt");
    if (inFile)
    {
        while (inFile >> num)
        {
            cout << num << endl;
        }
        inFile.close();
    }
    else
    {
        cout << "Error opening file" << FileName << "." << endl;
    }

    system("PAUSE");
    return;
}

void displaySortedNums()
{
    cout << "I AM THE displaySortedNums Function - Option 3" << endl;
    system("PAUSE");
    return;
}

void searchNum()
{
    cout << " I am the searchNum function - option 4" << endl;
    system("PAUSE");
    return;
}

void displayLargestNum()
{
    cout << "I am the displayLargestNum Function - option 5" << endl;
    system("PAUSE");
    return;

}

void appendRandomNum(string FileName)
{
    cout << "i am in the appendRandomNum function - option 6" << endl;
    int num = 0;
    int count = 0;
    ofstream outFile;
    outFile.open(FileName + ".txt", ios::app);
    cout << "How many random numbers: ";
    cin >> count;
    for (int i = 0; i < count; i++)
        outFile << rand() % 10 << endl;
    outFile.close();
    cout << endl << "Number(s) Added" << endl << endl;

    system("PAUSE");
    return;
}

void exit()
{
    cout << " I am the exit function - option 7" << endl;
    system("PAUSE");
    return;
}

void CreateFile()<-----------(and this)
{
    int random[50]; //Random Numbers

        srand((unsigned)time(NULL));
        fillFunc(random);
        printFunc(random);



        return;

}

void fillFunc(int arr[])
{
        for (int i = 0; i < 50; i++)
        {
                arr[i] = 1 + rand() % 10;

        }

}

void printFunc(int arr[])
{
    ofstream fout("C:\\Users\Wizard\Libraries\Documents\Final Project");
    if (fout.is_open()){
        for (int i = 0; i < 50; i++)
        {
            fout << arr[i] << std::endl;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

假设项目目录中有 tmp 文件夹,并且假装路径为:C:\Project\tmp\。此文件无法打开:ofstream fout("/tmp/nums.txt");

第一个斜杠是错误。就像你试图打开C:\Project\\tmp\一样 如果您使用的是Windows,就像在命令promt中将目录更改为C:\Project,然后使用命令cd \tmp,这将导致:

  

系统找不到指定的路径。

因此,省略第一个斜杠并让它为:ofstream fout("tmp/nums.txt");它会起作用。
(我假设你包括<fstream>并且你正在使用{{1 }}。)

答案 1 :(得分:0)

原始代码中存在以下几个问题:

1)您没有创建任何要输出的文本文件。您是否尝试使用“&gt;”输出?

2)你只生成了49个数字。要获得50,你需要从0开始而不是1.(int i = 0;)

3)您的输出中没有任何分隔符。你期望如何使用它们?

将printFunc更改为以下内容:

    ofstream fout ("/tmp/nums.txt");
    if(fout.is_open() ){
       for (int i = 0; i < 50; i++)
      {
            fout  << arr[i] << std::endl;
      }

答案 2 :(得分:0)

加入fstream #include <fstream>

在printFunc中更改:

ofstream fout("/tmp/nums.txt");

ofstream fout("folder/inner_folder/file.txt", ofstream::out);

第二个参数是文件的模式。 ofstream::out表示写访问(始终为ofstream对象设置)。 在函数结束时关闭流如下:

fout.close();

供参考:http://www.cplusplus.com/reference/fstream/ofstream/ofstream/