2017-10-09 4 views
0

別の基準を確認する前に何か処理が必要な場合は、入力を確認する最も良い方法は何ですか? コードスニペット:cinはすでに処理されていますか?

#include <string> 
#include <iostream> 
#include <dirent.h> 

bool has_suffix(const std::string &str, const std::string &suffix); 

void get_path_get_exit(std::string &path_input); 


int main() 
{ 
    DIR *dir; 
    struct dirent *ent; 
    std::string path_input; 
    std::getline(std::cin, path_input); 



    const char *path = path_input.c_str(); 
    dir = opendir(path); 
check: 
    while ((dir) == NULL && !path_input.empty()){ 
     /* could not open directory */ 

     std::cout << "Whoops, that didn't work. Please enter a valid directory path." << std::endl << "-->"; 
     std::getline(std::cin, path_input); 

     path = path_input.c_str(); 
     closedir(dir); 
     dir = opendir(path); 
    } 
    if ((dir) != NULL) { 

     unsigned int counter = 0; 

     while ((ent = readdir (dir)) != NULL) { 
      counter++; 
     } 
    /*check if the folder is empty*/ 
     if (counter == 0){ 
     /*how to surpass the goto statement?*/ 
      goto check; 
     } 
     std::string files[counter]; 


     closedir(dir); 
     dir = opendir(path); 
     counter = 0; 
     while ((ent = readdir (dir)) != NULL) { 

       files[counter] = ent->d_name; 
       std::cout << "[" << counter+1 << "] " << files[counter] << std::endl; 
       counter++; 

     } 
     closedir(dir); 
    } 
} 

あなたが見ることができるように、私は開かれたディレクトリが空である場合、私は、チェックしようとしている文の場合はCIN入力し、次の中をチェックしようとしています。もう一度whileループに入り、再び上の基準で新しい入力をチェックするために、トップ 'チェック'の先頭に "ホップ"するより良い方法はありますか? 後藤は確かに動作しますが、私はそれを少し恥ずかしく感じます。

+0

はwhile条件では '!path_input.empty()'であってはなりませんか? –

+0

もちろん、私はそれを間違ってコピーして申し訳ありません。私はそれを修正しました! – Dominik

答えて

0

do..whileループの場合を必要とするように私が聞こえます。これは、このような状況で優れたプログラマが行うことです。

bool IsDirectoryValidForThisOperation(DIR *dir, std::string& failDesdcription) 
{ 
    if (!dir) { 
     failDesdcription = "Invalid directory"; 
     return false; 
    } 
    if (readdir(dir)) == NULL) { 
     failDesdcription = "Directory is empty"; 
     return false; 
    } 
    // TODO: restore state of dir 

    return true; 
} 

ので、良いコードは常に、小さな関数にspitedされています。それはしやすくなり、コードFO

  • 多くの部分が、別のコンテキスト
  • で再利用することができます読みやすいです

    • テストを書く
    • デバッグが容易です
    • コードは自己文書化されます
  • +0

    ありがとうございます。私はそれを詳しく見ていきます! – Dominik

    +0

    dirent.hも認識するので、上記のwhileループをbool関数に追加しなければなりません。ファイルとして。だから私は接尾辞をチェックしなければならなかった。 – Dominik

    0

    私は完全にあなたの問題を理解していない可能性がありますが、あなたが条件をチェック機能を抽出

    const char *path = path_input.c_str(); 
        dir = opendir(path); 
        do 
        { 
         while (//some criteria that aren't met){ 
          /* could not open directory */ 
    
          std::cout << "Whoops, that didn't work. Please enter a valid directory path." << std::endl << "-->"; 
          std::getline(std::cin, path_input); 
    
          path = path_input.c_str(); 
          closedir(dir); 
          dir = opendir(path); 
         } 
         if ((dir) != NULL) { 
    
          unsigned int counter = 0; 
    
          while ((ent = readdir (dir)) != NULL) { 
           counter++; 
          } 
        /*check if the folder is empty*/ 
        } while(counter == 0) 
    
    +0

    ありがとうございますが、私はあなたが間違っていると思います。私の質問は、新しい入力が上位の要件によっても検証されるように、最初のチェックが行われる前にどのようにホップするかです。 – Dominik

    関連する問題