提出/输出文件,调用案例1之后的程序崩溃
File In/Out, Program crashes after case 1 is invoked
我已经使用了堆栈溢出了一段时间来完成我的第一个C 类,这是我第一次问一个问题,所以请忍受我。此问题是关于文件输入/out-简单阅读和写作。我应该通过无褶边的文本文件进行处理,并确定字符串是否具有导致或尾随的标点符号,将其分开,计数标点符号和字母(不像字符串的中间那样在字符串的中间,如www.google中的单词中。com)我将循环降低(或者我想),但是最近我的程序每次调用主开关语句的情况1时都会崩溃。计数和字母计数。
我在这里缺少什么?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream in;
ofstream out;
char outFile[50], inFile[50];
string temp, wird;
int x, longest, shortest, words, punct, wordlen, length, count;
float letters;
cout << "Enter output file: ";
cin >> outFile;
out.open(outFile);
do{
cout << "nSelect an option:nt1 - Enter input file nament2 - Determine word statisticsnt3 - List shortest wordsnt4 - List longest wordsnt5 - Search for a wordnt6 - Exitn";
cin >> x;
switch(x){
case 1: // input file select
cout << "nEnter input file name: ";
out << "nEnter input file name: ";
cin >> inFile;
in.open(inFile);
words = 0;
letters = 0;
punct = 0;
in >> temp;
longest = temp.length();
shortest = temp.length();
do{ // to determine letters, words, longest, shortest, punct
length = temp.length();
if (ispunct(temp[length -1]))
{
punct++;
temp.erase(temp.end()); // erase trailing punct
} //end if
if( ispunct(temp[0]))
{
punct++;
temp.erase(temp.begin()); // erase leading punct
} // end if
length = temp.length();
if(length > 0)
{
words++;
letters += length;
if(longest < length)
longest = length;
if(shortest > length)
shortest = length;
} // end if nest
in >> temp;
}while(in);
in.close();
in.open(inFile);
break;
case 2: // file stats
if(!in.is_open()){
cout << "nError - no input file selected";
out << "nError - no input file selected";
} // end if
else {
cout << "nFilename: " << inFile <<
"nTotal number of words: " << words <<
"nAverage word length: " << letters / words <<
"nTotal number of word characters: " << letters <<
"nTotal number of punctuation characters: " << punct<<
"nShortest word length: " << shortest <<
"nLongest word length: " << longest;
out << "nFilename: " << inFile <<
"nTotal number of words: " << words <<
"nAverage word length: " << letters / words <<
"nTotal number of word characters: " << letters <<
"nTotal number of punctuation characters: " << punct <<
"nShortest word length: " << shortest <<
"nLongest word length: " << longest;
} // end else
break;
case 3: // shortest words list
if(!in.is_open()){
cout << "nError - no input file selected";
out << "nError - no input file selected";
} // end if
else{
cout << "Filename: " << inFile << "nShortest words in file:n";
out << "Filename: " << inFile << "nShortest words in file:n";
in >> temp;
do{
length = temp.length();
if (ispunct(temp[length -1]))
{
punct++;
temp.erase(temp.end()); // erase trailing punct
} //end if
if( ispunct(temp[0]))
{
punct++;
temp.erase(temp.begin()); // erase leading punct
} // end if
length = temp.length();
if(shortest == length){
cout << temp << endl;
out << temp << endl;
}//endif
in >> temp;
}while(in);
}//endelse
in.close();
in.open(inFile);
break;
case 4: // longest words list
if(!in.is_open()){
cout << "nError - no input file selected";
out << "nError - no input file selected";
} // end if
else {
in >> temp;
do{
length = temp.length();
if (ispunct(temp[length -1]))
{
punct++;
temp.erase(temp.end()); // erase trailing punct
} //end if
if( ispunct(temp[0]))
{
punct++;
temp.erase(temp.begin()); // erase leading punct
} // end if
length = temp.length();
if(longest == length){
cout << temp << endl;
out << temp << endl;
}//endif
in >> temp;
}while(in);
in.close();
in.open(inFile);
}// end else
break;
case 5:
if(!in.is_open()){
cout << "nError - no input file selected";
out << "nError - no input file selected";
} // end if
cout << "nEnter word to find in file: ";
out << "nEnter word to find in file: ";
cin >> wird;
cout << "nWord to find: " << wird << endl;
out << "nWord to find: " << wird << endl;
count = 0;
in >> temp;
do{
length = temp.length();
if (ispunct(temp[length -1]))
{
punct++;
temp.erase(temp.end()); // erase trailing punct
} //end if
if( ispunct(temp[0]))
{
punct++;
temp.erase(temp.begin()); // erase leading punct
} // end if
length = temp.length();
length = temp.length();
if(temp.compare(wird)){
count ++ ;
}//endif
in >> temp;
}while(in);
in.close();
in.open(inFile);
cout << "nThe word " << wird << " appears " << count << " times.";
out << "nThe word " << wird << " appears " << count << " times.";
break;
case 6:
in.close();
out << "n Program terminated by user";
out.close();
break;} // end switch
}while(x < 6);
return 0;
} // end main
一个可能的问题是这种危险组合:
for(int i = 0; i < length; i++)
与
结合temp.erase(temp.begin());
...缩短您的字符串。
这意味着,如果输入中的字符串以标点符号开头,则以下语句将窥视字符串末端的一个:
if ( i == (length - 1) && ispunct(temp[i]))
所有这些,如@molbdnilo所说,在这种情况下,您甚至不需要循环。
相关文章:
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- OpenSSL C API:如何在程序exec()之后恢复TLS连接?
- 我们如何重新制作程序以打开xml文件并在价格单词之后读取百分比值并更改此值
- 构建一个真正、完全静态的Qt 5框架和应用程序(Qt 5.8之后)
- 为什么程序在 cin 之后跳过 cin.get()?
- 如何限制文件大小,以便我的程序在变大之后创建一个新文件?并编辑新创建的文件的名称
- 在SIGABRT c ++信号之后继续运行程序
- 我的程序在SCANF语句之后不起作用
- 在我关闭刚开始的孩子对话框并进入父母对话框之后,我的QT桌面应用程序不断崩溃
- 获取提升程序选项以在函数范围之后保留
- C 程序在CIN之后突然结束
- 在机器异常之后重新执行程序
- 提出/输出文件,调用案例1之后的程序崩溃
- 如何判断 WIN32 C++ 应用程序在 CTRL-ALT-DEL 之后是否失去了焦点
- 我的程序返回整数值,但即使在显式类型转换之后也应该给出双值
- 从注入的dll调用BeginPaint时失败,即使在目标应用程序中调用了EndPaint之后也是如此
- 在Javascript服务器应用程序之后获取HTML
- 在对 MFC 应用程序进行编程之前还是之后创建附属 dll 更好
- 即使在简单的 Hello 程序中,我也无法在 C++ 中在 ./a.out 之后获得输出
- 如何创建一个C/ c++程序,生成XML并在之后运行DOS命令