2016-06-28 4 views
-2

私はほとんどプログラムで終わっていますが、私はそれを設定する必要がありますので、ループし続け、無効な名前が与えられるまでファイル名を尋ねてきます。すぐに終了し、プログラムを終了しますが、私は何か間違ったことをやっているし、それは一度だけループを通過する原因それと間違っているかを把握することはできません...無効なファイル名が与えられるまでファイルを開くよう依頼してください

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

using namespace std; 

int main() 
{ 
    int line_number = 0; 
    char words; 
    int number_of_characters = 0; 
    int number_of_spaces = 0; 
    int number_of_words = 0; 
    string line; 
    ifstream myFile; 
    string file; 

    cout <<"Enter file name: "; 
    getline(cin,file); 

    myFile.open(file.c_str()); 
    char output[100]; 

    while(myFile.good()) 
    { 
     if(myFile.is_open()) 
     { 
      cout << " " << endl; 

      while(getline (myFile, line)) 
      { 
       line_number += 1; 
       cout << "Line: " << line_number << " "; 
       cout << line << endl; 

       number_of_characters += line.length(); 

       for (int i = 0; i < line.length(); i++) 
       { 
        if(line[i] == ' ' || line[i] == '/t') 
        { 
         number_of_spaces++; 
        } 
       } 
      } 

      myFile.clear(); 
      myFile.seekg(0,myFile.beg); 

      while(myFile >> line) 
      { 
       number_of_words++; 
      } 

      cout << " " << endl; 
      cout << number_of_characters << " characters, " << endl; 
      cout << number_of_characters - number_of_spaces << " non whitespace characters, " << endl; 
      cout << number_of_words << " words, " << endl; 
      cout << line_number << " lines." << endl; 
      cout << " " << endl; 
     } 
    } 

    cout << endl << "Invalid file name." << endl << "Terminating..." << endl << endl; 
    myFile.close(); 
    system("Pause"); 
    return 0; 
} 
+2

あなたは 'while'ループでファイル名を読み取るコードを入力しませんでした。 –

答えて

0

私はあなたのコードで見る主な問題は、ということですwhileループのファイル名を読み取るコードを記述していませんでした。

また、別のエラー、おそらくタイプミスがあります。あなたは'/t'の代わりに、もっと重要なのは、あなたが以下の個別の操作にmainにコードを分割することができますラインで'\t'

if(line[i] == ' ' || line[i] == '/t') 

を使用しています。

  1. ファイル名を取得します。
  2. 良いファイルであることを確認してください。
  3. ファイルが良好な場合は、その内容のあるものを実行してください。
  4. ファイルが不良な場合は、メッセージで終了してください。

これらの操作を反映するようにmainを定義できます。

int main() 
{ 
    std::string file; 
    while (is_good_file(file = get_filename())) 
    { 
     process_file(file); 
    } 

    cout << endl << "Invalid file name." << endl << "Terminating..." << endl << endl; 
    return 0; 
} 

と定義し、次に3つの機能を定義します。

std::string get_filename() 
{ 
    std::string file; 
    cout <<"Enter file name: "; 
    getline(cin,file); 
    return file; 
} 

bool is_good_file(std::string const& file) 
{ 
    std::ifstream fstr(file); 
    return (fstr); 
} 

void process_file(std::string const& file) 
{ 
    int line_number = 0; 
    int number_of_characters = 0; 
    int number_of_spaces = 0; 
    int number_of_words = 0; 
    string line; 

    std::ifstream fstr(file); 
    while(getline (fstr, line)) 
    { 
     line_number += 1; 
     cout << "Line: " << line_number << " "; 
     cout << line << endl; 

     number_of_characters += line.length(); 

     for (size_t i = 0; i < line.length(); i++) 
     { 
     if(line[i] == ' ' || line[i] == '\t') 
     { 
      number_of_spaces++; 
     } 
     } 
    } 

    fstr.clear(); 
    fstr.seekg(0,fstr.beg); 

    while(fstr >> line) 
    { 
     number_of_words++; 
    } 

    cout << " " << endl; 
    cout << number_of_characters << " characters, " << endl; 
    cout << number_of_characters - number_of_spaces << " non whitespace characters, " << endl; 
    cout << number_of_words << " words, " << endl; 
    cout << line_number << " lines." << endl; 
    cout << " " << endl; 
} 

高度な操作を考えれば、コードを整理できます。また、バグを分離して修正するのに役立ちます。

関連する問題