2017-07-31 13 views
-3

私はMicrosoft Visual Studio 2017 Communityを使用しています。どのようにスイッチ機能を使ってプログラムを正しく終了させるのですか?私は、プログラマーはexit()のようなno-noを持っていることを知っています。私はリターン0を使用しようとしました。機能の別れやスイッチのような別の場所では運がなかったのですが、機能やスイッチが終了するだけですが、プログラムを終了したり閉じたりしないという問題があります。また、間違っているかもしれない、あるいはより良くできるかもしれないものについて私を修正することを自由に感じてください。私はC++には本当に新しく、これが私の最初のプログラムです。注:ユーザー名をグローバル変数にしないようにしようとしましたが、いくつかの関数で使用されていましたので、先に進んでグローバル化しました。C++スイッチ機能を使用してプログラムを正しく終了する方法

これは私の例です:

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

///////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////// 
////    Switch does not end program on QUIT :(     //// 
///////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////////////////////////////////////////////// 

void welcome(); 

int options_menu(); 

void register_username(); 

std::string register_password(); 

void save_user(const std::string &password); 

void user_login(); 
void display_file(); 

void clock_in_hour(); 
void clock_in_minute(); 
void clock_out_hour(); 
void clock_out_minute(); 

void test(); 

void goodbye(); 

std::string username; 

template <typename T> 
T get_input(const std::string &strQuery) 
{ 
    std::cout << strQuery << "\n> "; 
    T out = T(); 

    while (!(std::cin >> out)) 
    { 
     std::cin.clear(); 
     std::cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n'); 
     std::cout << "Error!" "\n"; 
     std::cout << strQuery << "\n> "; 
    } 
    return out; 
} 

int main() 
{ 
    welcome(); 
    options_menu(); 
    test(); 
    return 0; 
} 

void welcome() 
{ 
    std::cout << "Welcome to the Time Card Calculator Pro V1.0 \n\n"; 
} 

int options_menu() 
{ 
    int menu = 0; 

     std::cout << "Please type an option number and press enter to continue: \n\n"; 

     std::cout << "[1] Register \n"; 
     std::cout << "[2] Login \n"; 
     std::cout << "[3] Quit \n\n"; 

     menu = get_input <int>("Please type an option number and press enter to continue \n"); 

     switch (menu) 
     { 
     case 1: 
      std::cout << "\n"; 
      std::cout << "You chose to register \n\n"; 
      register_username(); 
      break; 
     case 2: 
      std::cout << "\n"; 
      std::cout << "You chose to login \n\n"; 
      user_login(); 
      break; 
     case 3: 
      std::cout << "\n"; 
      std::cout << "You chose to quit \n\n"; 
      goodbye(); 
      break; 
     default: 
      std::cout << "\n"; 
      std::cout << "Error! Invalid option \n\n"; 
      options_menu(); 
      break; 
     } 
} 

void register_username() 
{ 
    std::string username; 

    std::cout << "Please enter your full name: "; //ask user to create username... 
    std::cin.ignore(); 
    std::getline(std::cin, username); 

    while (get_input <int>("Confirm? [0|1]") != 1) 
    { 
     std::cout << "Please enter your full name: "; 
     std::cin.ignore(); 
     std::getline(std::cin, username); 
    } 

    std::ifstream file(username + ".txt"); //check if user file exists... 
    if (file.is_open()) 
    { 
     std::cout << "Error! Username already taken \n\n"; 
     options_menu(); 
    } 
    else  //ask user to create a password... 
    { 
     register_password(); 
    } 
} 

std::string register_password() 
{ 
    std::cout << "Now please create a password \n"; 
    std::string ask_password = get_input<std::string>("Password may not have any spaces "); 
    std::string password = get_input<std::string>("Please re-enter the password "); 

    if (ask_password == password) 
    { 
     save_user(password); 
    } 
    else 
    { 
     std::cout << "Error: Passwords did not match \n"; 
     register_password(); 
    } 
    return password; 
} 

void save_user(const std::string &password) 
{ 
    std::cout << "Saving user info... \n"; 

    std::ofstream file(username + ".txt"); 
    file << password << "\n"; 

    std::cout << "Username: " << username << "\n"; 
    std::cout << "Password: " << password << "\n"; 
} 

void user_login() 
{ 
    std::cout << "Please enter your username "; 
    std::cin.ignore(); 
    std::getline(std::cin, username); 

    std::cout << "Searching for file... \n"; 

    std::ifstream file(username + ".txt"); //look for user file... 

    if (file.is_open())  //if user file is found... 
    { 
     std::cout << "File found \n"; 
     display_file(); 
    } 
    else 
    { 
     std::cout << "Error: Username not registered \n"; 
     std::cout << "Please register username \n"; 
     options_menu(); 
    } 
} 

void display_file() 
{ 
    std::string line; 
    int numberoflines = 0; 

    std::cout << "Searching for user file " << username << "\n"; 

    std::ifstream file(username + ".txt"); 
    while (std::getline(file, line)) 
    { 
     std::cout << line << "\n"; 
     numberoflines++; 
    } 
} 


void test() 
{ 
    std::cout << "This is a test \n"; 
} 

void goodbye() 
{ 
    std::cout << "Thank you for using the Time Card Calculator Pro V1.0 \n"; 
    std::cout << "Good bye \n"; 
} 
+6

あなたは大きすぎます。質問を[mcve]に絞ってください。そうすることで、あなた自身の問題を偶然見つけ出すことさえできます。なぜ、ハードコードされた変数と 'switch'を持つ単純な' main() '関数を持っていないのでしょうか?そこから、ユーザーからの入力を取得するように構築します。 – Tas

+0

[C++コンソールプログラムを終了するにはどうすればいいですか?](https://stackoverflow.com/questions/4038302/how-do-i-make-ac-console-program-exit) – philipxy

答えて

1

You need to return something from your switch statement to your main loop. The return 0あなたのメインループはプログラムを終了させるものです。

int main() 
{ 
    welcome(); 
    int exitNow = options_menu(); //lets say a -1 return is the symbol for exit 
    while(exitNow != -1) 
     exitNow = options_menu(); 
    test(); 
    return 0; 
} 

だからあなたのスイッチはこのようなものになるだろう:

int options_menu() 
{ 
    int menu = 0; 

     std::cout << "Please type an option number and press enter to continue: \n\n"; 

     std::cout << "[1] Register \n"; 
     std::cout << "[2] Login \n"; 
     std::cout << "[3] Quit \n\n"; 

     menu = get_input <int>("Please type an option number and press enter to continue \n"); 

     switch (menu) 
     { 
     case 1: 
      std::cout << "\n"; 
      std::cout << "You chose to register \n\n"; 
      register_username(); 
      break; 
     case 2: 
      std::cout << "\n"; 
      std::cout << "You chose to login \n\n"; 
      user_login(); 
      break; 
     case 3: 
      std::cout << "\n"; 
      std::cout << "You chose to quit \n\n"; 
      goodbye(); 
      return -1; 
      break; 
     default: 
      std::cout << "\n"; 
      std::cout << "Error! Invalid option \n\n"; 
      options_menu(); 
      break; 
     } 
    return 0; 
} 
+0

..マイナーバグ:P。固定 –

+0

私はこれを解決しました。私が解決する必要があった小さな部分だけでなく、コード全体を含めて本当に残念です。私はoptions_menuをvoid関数にすることで問題を解決しました。私はメニュー変数をグローバルにしたので、main関数のwhile文を設定することができました。 while文の上にoptions_menu()関数を呼び出しました。私はwhile文を作ったが、メニューは3(終了した場合)に等しくなく、0を返す。メインで(プログラムを終了する)。 – pickybeginner

+0

グローバル変数を持つのは悪いことです。ちょうどfyi –

0

私はちょうどこれは何も、全く同様の出口()またはシステム(ない場合は私に知らせてください:)ちょうど今、私の問題を解決しました)。私はどこでも読んでいるような悪い習慣に陥るのは嫌です。私は慎重にしようとする。ここに私の質問に対する私の答えがあります。私はそれが大丈夫だと思います。しかし、それは動作します。メニューからquitを選択すると、テスト関数(またはその関数の後に呼び出される関数)が呼び出されないことに注目してください。 これで、quitオプションが機能しました。問題は、この良いコードですか?私にアドバイスを与えたり、どこにいても私を修正したり、プログラミングの良い習慣をつかまえてください。長いコードを読み、自分の洞察を貸す時間を割いた皆さん、ありがとうございました。

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

void welcome(); 

void options_menu(); 

void register_username(); 

std::string register_password(); 

void save_user(const std::string &password); 

void user_login(); 
void display_file(); 

void test(); 

void goodbye(); 

std::string username; 
int menu = 0; 

template <typename T> 
T get_input(const std::string &strQuery) 
{ 
    std::cout << strQuery << "\n> "; 
    T out = T(); 

    while (!(std::cin >> out)) 
    { 
     std::cin.clear(); 
     std::cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n'); 
     std::cout << "Error!" "\n"; 
     std::cout << strQuery << "\n> "; 
    } 
    return out; 
} 

int main() 
{ 
    welcome(); 
    options_menu(); 
    while (menu != 3) 
    { 
     test(); 
     return 0; 
    } 
    return 0; 
} 

void welcome() 
{ 
    std::cout << "Welcome to the Time Card Calculator Pro V1.0 \n\n"; 
} 

void options_menu() 
{ 
    std::cout << "Please type an option number and press enter to continue: \n\n"; 
    std::cout << "[1] Register \n"; 
    std::cout << "[2] Login \n"; 
    std::cout << "[3] Quit \n\n"; 

    menu = get_input <int>("Please type an option number and press enter to continue \n"); 

     switch (menu) 
     { 
     case 1: 
      std::cout << "\n"; 
      std::cout << "You chose to register \n\n"; 
      register_username(); 
      break; 
     case 2: 
      std::cout << "\n"; 
      std::cout << "You chose to login \n\n"; 
      user_login(); 
      break; 
     case 3: 
      std::cout << "\n"; 
      std::cout << "You chose to quit \n\n"; 
      goodbye(); 
      break; 
     default: 
      std::cout << "\n"; 
      std::cout << "Error! Invalid option \n\n"; 
      options_menu(); 
      break; 
     } 
} 

void register_username() 
{ 
    std::string username; 

    std::cout << "Please enter your full name: "; //ask user to create username... 
    std::cin.ignore(); 
    std::getline(std::cin, username); 

    while (get_input <int>("Confirm? [0|1]") != 1) 
    { 
     std::cout << "Please enter your full name: "; 
     std::cin.ignore(); 
     std::getline(std::cin, username); 
    } 

    std::ifstream file(username + ".txt"); //check if user file exists... 
    if (file.is_open()) 
    { 
     std::cout << "Error! Username already taken \n\n"; 
     options_menu(); 
    } 
    else  //ask user to create a password... 
    { 
     register_password(); 
    } 
} 

std::string register_password() 
{ 
    std::cout << "Now please create a password \n"; 
    std::string ask_password = get_input<std::string>("Password may not have any spaces "); 
    std::string password = get_input<std::string>("Please re-enter the password "); 

    if (ask_password == password) 
    { 
     save_user(password); 
    } 
    else 
    { 
     std::cout << "Error: Passwords did not match \n"; 
     register_password(); 
    } 
    return password; 
} 

void save_user(const std::string &password) 
{ 
    std::cout << "Saving user info... \n"; 

    std::ofstream file(username + ".txt"); 
    file << password << "\n"; 

    std::cout << "File saved \n"; 
    std::cout << "Username: " << username << "\n"; 
    std::cout << "Password: " << password << "\n"; 
} 

void user_login() 
{ 
    std::cout << "Please enter your username "; 
    std::cin.ignore(); 
    std::getline(std::cin, username); 

    std::cout << "Searching for file... \n"; 

    std::ifstream file(username + ".txt"); //look for user file... 

    if (file.is_open())  //if user file is found... 
    { 
     std::cout << "File found \n"; 
     display_file(); 
    } 
    else 
    { 
     std::cout << "Error: Username not registered \n"; 
     std::cout << "Please register username \n"; 
     options_menu(); 
    } 
} 

void display_file() 
{ 
    std::string line; 
    int numberoflines = 0; 

    std::cout << "Searching for user file " << username << "\n"; 

    std::ifstream file(username + ".txt"); 
    while (std::getline(file, line)) 
    { 
     std::cout << line << "\n"; 
     numberoflines++; 
    } 
} 

void test() 
{ 
    std::cout << "This is a test \n"; 
} 

void goodbye() 
{ 
    std::cout << "Thank you for using the Time Card Calculator Pro V1.0 \n"; 
    std::cout << "Goodbye \n"; 
} 

+0

追加の注釈:return 0;メインで呼び出された場合にのみプログラムを終了します。関数またはプロセスで呼び出された場合にのみ、関数またはプロセスが終了します。 0を返します。プログラムを終了するには、私が知っている限りMAINから呼び出さなければなりません... – pickybeginner

+0

edit:メインのif elseステートメントをwhileステートメントに変更してコードを短縮し、簡素化しました。 – pickybeginner

+0

'return 0;'はあなたの 'main'関数で呼び出されて、全体的なプログラムを終了させる必要があります。これは普通はC++で普遍的です。 –

関連する問題