【问题标题】:Whats wrong with my program? (C++)我的程序有什么问题? (C++)
【发布时间】:2017-11-27 23:08:53
【问题描述】:

我的程序在读取、从文件中查找包含短语的单词并将结果写入文件时遇到了一些问题。

这是整个程序:

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

using namespace std;

void space();
void intro();
void again(string input3);

bool isRunning = true;

class ReadWriteFile 
{


    public:
    string input;
    string input2;
    string file;
    string tmp;

    int numberOfLines = 0;
    int counter = 0;

    void fileName()
    {
        cout << "  Enter the file name: ";

        cin >> input;

        file = input + ".txt";

        space();
        fileLength();
    }

    string fileLength()
    {
        ifstream fileStream (file/*.c_str()*/);

        if (fileStream)
        {
            while (fileStream >> tmp)
                numberOfLines++;
            // cout << numberOfLines;
            // space();
            fileInput();
        }
        else
        {
            cout << "  Wrong Input";
            space();
            fileName();
        }

        fileStream.close();
    }

    void fileInput()
    {
        string arr1 [numberOfLines];

        ifstream fileStream (file/*.c_str()*/);

        for (int i = 0; i < numberOfLines; i++)
        {
            fileStream >> arr1 [i];
        }

        fileStream.close();
        orderInput(arr1);
    }

    void orderInput(string *arr1)
    {
        string arr2[numberOfLines];
        int a = 0;

        //string arr2 [numberOfLines];

        cout << "  Enter a search key phrase: ";

        cin >> input2;

        space();

        for (int i = 0; i < numberOfLines; i++)
        {
            tmp = arr1[i];
            //cout << tmp;

            if (tmp.find(input2) != string::npos)
            {
                arr2[a] = tmp;
                cout << "  " << tmp << endl;
                a++;
            }
        }

        space();
        writeFile (arr2);
    }

    void writeFile (string *arr2)
    {
        ofstream fileStream ("output.txt");

        for (int i = 0; i < numberOfLines; i++)
        {
            fileStream << arr2[i] << endl;
        }

        fileStream.close();

        cout << "  The results are located in the output text file named 'output'" << endl;
    }
};

ReadWriteFile  rwf;

int main ()
{
    string input3;

    intro();

    while (isRunning = true)
    {
        rwf.fileName();
        cout << "Do you want to run this program again? (y/n): ";
        cin >> input3;
        again(input3);
    }

    return 0;
}

void space()
{
    string enters = "\n\n\n\n\n\n\n\n\n\n";
    string rule = 
    "  --------------------\n       ----------";

    cout << endl << rule  << enters;
}

void intro()
{
    cout << endl << endl <<
    " ########## ###### ##    ## ##########"<< endl <<
    "     ##     ##      ##  ##      ##"<< endl <<
    "     ##     ##       ####       ##"<< endl <<
    "     ##     ######   ####       ##"<< endl <<
    "     ##     ##      ##  ##      ##"<< endl <<
    "     ##     ###### ##    ##     ##"<< endl <<
    " "<< endl <<
    "  ##### ######   ####    #####  ##### ##   ##"<< endl <<
    " ##     ##      ##  ##  ##     ##     ##   ##"<< endl <<
    "  ####  ##      ######  ##     ##     ##   ##"<< endl <<
    "     ## ###### ##    ## ##     ##     #######"<< endl <<
    "     ## ##     ##    ## ##     ##     ##   ##"<< endl <<
    " #####  ###### ##    ## ##      ##### ##   ##"<< endl <<
    " "<< endl <<
    " ##########  #####   #####  ##"<< endl <<
    "     ##     ##   ## ##   ## ##"<< endl <<
    "     ##     ##   ## ##   ## ##"<< endl <<
    "     ##     ##   ## ##   ## ##"<< endl <<
    "     ##     ##   ## ##   ## ##"<< endl <<
    "     ##      #####   #####  ######"<< endl << endl;
}

void again(string input3)
{
    if (input3 == "y" || input3 == "Y" || input3 == "yes" || input3 == "Yes" || input3 == "YES")
    {

    }
    else if (input3 == "n" || input3 == "N" || input3 == "no" || input3 == "No" || input3 == "NO")
    {
            isRunning = false;
    }
}

这是我认为导致我的问题的一段代码:

    void fileInput()
    {
        string arr1 [numberOfLines];

        ifstream fileStream (file/*.c_str()*/);

        for (int i = 0; i < numberOfLines; i++)
        {
            fileStream >> arr1 [i];
        }

        fileStream.close();
        orderInput(arr1);
    }

    void orderInput(string *arr1)
    {
        string arr2[numberOfLines];
        int a = 0;

        //string arr2 [numberOfLines];

        cout << "  Enter a search key phrase: ";

        cin >> input2;

        space();

        for (int i = 0; i < numberOfLines; i++)
        {
            tmp = arr1[i];
            //cout << tmp;

            if (tmp.find(input2) != string::npos)
            {
                arr2[a] = tmp;
                cout << "  " << tmp << endl;
                a++;
            }
        }

        space();
        writeFile (arr2);
    }

    void writeFile (string *arr2)
    {
        ofstream fileStream ("output.txt");

        for (int i = 0; i < numberOfLines; i++)
        {
            fileStream << arr2[i] << endl;
        }

        fileStream.close();

        cout << "  The results are located in the output text file named 'output'" << endl;
    }

编辑:程序冻结在:“结果位于名为'output'的输出文本文件中”

【问题讨论】:

  • 那么你的问题是什么?
  • 请修改您的标题,使其不那么含糊。 “我的程序有什么问题?”可以用作本网站上几乎所有问题的标题。

标签: c++ windows-7 codeblocks


【解决方案1】:

改变

ifstream fileStream (file/*.c_str()*/);

fileLength()fileInput() 函数上发送给 ifstream fileStream(file.c_str());

【讨论】:

  • 它似乎没有改变任何东西。它只会得到“结果位于名为'output'的输出文本文件中
  • -1:根据std::ifstream,构造函数可以接受std::string,不再需要与.c_str()进行转换。
  • 您使用变量“numberoflines”作为某些数组大小的值,但您没有将“numberoflines”建立为常量,例如“NUMBEROFLINES”,并且您没有将其初始化为零'0'......只是说......
  • 你的函数 'string fileLength()' 必须返回一个值,但不会...
猜你喜欢
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多