字符串无效的For循环

时间:2015-02-08 03:10:59

标签: c++ string loops for-loop c-strings

以下是说明:

编写一个程序,一次读取一个单词的文本文件。首次遇到时,将单词存储到动态创建的数组中。创建并行整数数组以保存每个特定单词在文本文件中出现的次数。如果单词出现在文本文件中多次,请不要将其添加到动态数组中,但请确保在并行整数数组中增加相应的单词频率计数器。在进行任何比较之前,请从所有单词中删除任何尾随标点符号。

创建并使用包含Bill Cosby报价的以下文本文件来测试您的程序。

我不知道成功的关键,但失败的关键是试图取悦所有人。

在程序结束时,生成一个报告,打印两个数组的内容

这是我的代码:

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

using namespace std;

int main()
{
    ifstream inputFile;
    int numWords;
    string filename;
    string *readInArray = 0;
    char testArray[300] = {0};
    char *realArray = 0;
    const char *s1 = 0;
    string word;
    int j =1;
    int k = 0;

    int start =0;
    int ending = 0;
    char wordHolder[20] = {0};



    cout << "Enter the number of words the file contains: ";
    cin >> numWords;


    readInArray = new string[(2*numWords)-1];


    cout << "Enter the filename you wish to read in: ";
    cin >> filename;


    inputFile.open(filename.c_str());


    if (inputFile)
    {
        cout << "\nHere is the text from the file:\n\n";

        for (int i=0; i <= ((2*numWords) -1); i +=2)
        {
            inputFile >> readInArray[i];                    // Store word from file to string array
            cout << readInArray[i];
            strcat(testArray, readInArray[i].c_str());      // Copy c-string conversion of word 
                                                            // just read in to c-string
            readInArray[j] = " ";
            cout << readInArray[j];
            strcat(testArray, readInArray[j].c_str());      // This part is for adding spaces in arrays
            ++j;
        }
        inputFile.close();
    }
    else
    {
        cout << "Could not open file, ending program";
        return 0;
    }


    realArray = new char[strlen(testArray)];
    cout << "\n\n";


    for(int i=0; i < strlen(testArray); ++i)
    {
        if (isalpha(testArray[i]) || isspace(testArray[i]))     // Is makes another char array equal to
        {                                                       // the first one but without any
            realArray[k]=testArray[i];                          // Punctuation
            cout << realArray[k] ;
            k++;
        }
    }


    cout << "\n\n";


    for (int i=0; i < ((2*numWords) -1); i+=2)
    {
        while (isalpha(realArray[ending]))              // Finds space in char array to stop
        {
            ++ending;
       }
        cout << "ending: " << ending << " ";

        for ( ; start < ending; ++start)                // saves the array up to stopping point
        {                                               // into a holder c-string
            wordHolder[start] = realArray[start];
        }
        cout << "start: " << start << " ";

        readInArray[i] = string(wordHolder);            // Converts holder c-string to string and
        cout << readInArray[i] << endl;                 // assigns to element in original string array

        start = ending;                                 // Starts reading where left off
        ++ending;                                       // Increments ending counter
    }

    return 0;
}

输出:

输入文件包含的字数:17

输入您要阅读的文件名:D:/Documents/input.txt

以下是文件中的文字:

我不知道成功的关键,但失败的关键是试图取悦所有人。

我不知道成功的关键,但失败的关键是试图取悦所有人

结束:1开始:1我

结束:6开始:6我不

结束:11开始:11我不知道

结束:15开始:15我不知道

结束:19开始:19我不知道密钥

结束:22开始:22我不知道&gt;

的关键

结束:29开始:29我不知道成功的关键

结束:33开始:33我不知道成功的关键但是&gt;

我的问题:

最后一个for循环出了问题,运行后它崩溃了。我包括了结尾和起始变量,可能有助于了解最新情况。我知道有更好的方法来解决这个问题,但是教练希望这样做。如果你知道我在最后一个for循环中出错了,那么任何帮助都会非常感激!!

1 个答案:

答案 0 :(得分:3)

随着你的进展,你不是在终止你的字符串。您可以正确复制字符,但如果没有空终止符,您的循环可能会进入杂草。

相关问题