提出/输出文件,调用案例1之后的程序崩溃

File In/Out, Program crashes after case 1 is invoked

本文关键字:之后 程序 崩溃 案例 调用 输出 文件 提出      更新时间:2023-10-16

我已经使用了堆栈溢出了一段时间来完成我的第一个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所说,在这种情况下,您甚至不需要循环。