对于字符串不起作用的循环
For-loop with strings not working
以下是说明:
编写一个程序,每次读取一个单词的文本文件。首次遇到单词时,将其存储到动态创建的数组中。创建一个并行整数数组,以保存每个特定单词在文本文件中出现的次数。如果单词多次出现在文本文件中,请不要将其添加到动态数组中,但请确保在并行整数数组中增加相应的单词频率计数器。在进行任何比较之前,请删除所有单词中的任何尾随标点符号。
创建并使用下面的文本文件,其中包含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:nn";
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 << "nn";
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 << "nn";
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 I
结束:6开始:6我不
结束:11开始:11我不知道
结束:15开始:15我不知道
结束:19开始:19我不知道的钥匙
结束:22开始:22我不知道的关键
结束:29开始:29我不知道成功的关键
结束:33开始:33我不知道成功的关键,但是↕>
我的问题:
最后一个for循环出了问题,在我运行后它崩溃了。我包括了结束和开始变量,也许有助于了解发生了什么。我知道有更好的方法来解决这个问题,但教练希望这样做。如果你知道我在最后一个循环中哪里出了问题,任何帮助都将不胜感激!!
在执行过程中,终止字符串不是null。您正确地复制了字符,但如果没有空终止符,您的循环可能会陷入混乱。
相关文章:
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 嵌套While循环不起作用(C++问题)
- 为什么当我对数组使用增量操作时,do for 循环不起作用
- 在矩阵逆变器上工作,由于某种原因,我的指数循环不起作用
- C++需要弄清楚为什么这个 for 循环不起作用
- 为什么我的 for 循环不起作用?
- C++循环不起作用时执行
- 为什么这个做同时循环不起作用?
- 为什么这个循环不起作用
- 为什么我的闰年计划的 for 循环不起作用?
- 为什么这个简单的循环不起作用
- 为什么循环不起作用?(BGI形状)
- 为什么这个循环不起作用
- 游戏循环不起作用
- 简单的循环不起作用
- 为什么这个循环不起作用?
- 而用于加载文件的循环不起作用
- OpenAl mp3 循环不起作用
- 对于循环不起作用
- 虽然带有指针的循环不起作用