2017-07-31 58 views
-3

我正在使用Microsoft Visual Studio 2017社區,並對某事感到好奇。如何正確結束帶有開關功能的程序?我知道程序員有一定的禁忌,像exit()。我曾嘗試使用返回0;在不同的地方,如功能再見和開關,但沒有任何運氣,它只是結束功能或開關,但問題是它不會退出或關閉程序。也可以隨時糾正任何可能出錯或可以做得更好的事情。我真的很陌生,這是我的第一個程序。注意:儘量避免將用戶名作爲全局變量,但它用於多個函數,所以我繼續前進並使其成爲全局變量。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],這樣做甚至可能會碰到你自己的問題。爲什麼不用一個簡單的'main()'函數與一個硬編碼變量和'switch',看看是否退出?從那裏,建立起來從用戶那裏獲得輸入。 – 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無效函數來解決我的問題。然後,我將菜單變量設置爲全局的,這樣我就可以在主函數中爲while語句設置它。我在while語句上面調用了options_menu()函數。我做了while語句,而菜單不等於3(退出的情況下),然後返回0;主(結束程序)。 – pickybeginner

+0

它具有全局變量的不良做法。只是一個fyi –

0

我解決我的問題剛纔:)只是請讓我知道,如果這是一個沒有沒有喜歡的exit()或系統( )。我不想像我到處讀到的那樣陷入壞習慣。我儘量小心。這是我對我的問題的回答。我希望沒關係。它確實工作。請注意,如果從菜單中選擇退出,測試功能(或任何在其之後調用的函數或調用函數)不會被調用。 所以,現在退出選項的工作,現在的問題是,這是好的代碼?請隨時給我建議或糾正我的任何地方,或讓我養成編程的良好習慣。感謝大家花時間閱讀長碼並向我借鑑他們的見解。

#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;只有在main中調用纔會結束程序。如果在函數或過程中被調用,它只會結束函數或過程。返回0;結束程序必須調用主MAIN,據我所知... – pickybeginner

+0

編輯:將主if if語句改爲while語句以縮短代碼並簡化它。 – pickybeginner

+0

'return 0;'必須在'main'函數中調用來終止整個程序,這在C++中通常是通用的。 –

相關問題