2017-01-13 4 views
2

ユーザがユーザ名とパスワードの両方に何も入力しなかった場合に、別の条件を追加する方法はありますか? (「ユーザー名とパスワードを空にする - ユーザー名とパスワードが必要です!再試行する(Y/N)」)ユーザーが何も入力していないかどうかを確認する方法は?

ここに私です。コード。

#include "stdafx.h" 
#include <iostream>; 
#include <string>; 
using namespace std; 
string username; 
string password; 
string choice; 

int main(int argc, const char * argv[]) 
{ 
do { 

Username: 
std::cout << "Enter Username: "; 
std::cin >> username; 

if (username != "Joven") 
{ 
std::cout << "Invalid Username. Please try again.\n\n\n"; 
goto choice; 
goto Username; 

} 
Password: 
std::cout << "Enter Password: "; 
std::cin >> password; 

if (password != "Fabricante7188") 
{ 
std::cout << "Invalid Password. Please try again.\n\n\n"; 
std::cout << "Do you want to try again? y/n \n\n"; 
cin >> choice; 
goto Password; 
} 
else 
{ 
std::cout << "Correct Username and Password - Log In Successfull.\n"; 
break; 

choice: 
std::cout << "Do you want to try again? y/n \n\n"; 
std::cin >> choice; 

} 
}while (choice != "y" && choice != "n"); 

if (choice != "y" && choice != "n") 
{ 
cout << "Invalid choice.\n"; 
goto choice; 
} 
system("pause"); 
return 0; 
}` 

ありがとう!

+0

関連のないメモでは、gotoをループとして使用することは、習慣として行うべきことではありません。 'goto'を一般的に使うのは、あなたが一切避けるべきものです。 –

+0

別の無関係な注記では、 'goto choiceで行ったように、行に2つの' goto'文を書くべきではありません。 gotoユーザー名; '。この種のコードは風変わりなコードでのみ使用され、AppleのSSLコードを楽しませることができます。失敗する; '。 –

答えて

0

入力操作>>を使用することはできません。実際に空白以外の入力があり、その後にと入力してキーが入力されるまでブロックされます(またはエラーまたは「ファイルの終わり」があります)。

(OS固有の機能に頼らずに)C++標準ソリューションは、 std::getlineの場合は、先行するスペース(および末尾のスペース)の入力を取り除き、結果の文字列が空であるかどうかを確認します。

0

私のソリューションは、このようなものになるだろう。また、自分に好意を行うとする方法として、「goto文」の使用は避けてください(スコーチ2017年1月14日10時24分AEST編集)

//#include "stdafx.h"  I made this a comment because I don't appear to have this file. It wasn't necessary for me. Don't know whether you need it or not. 
// include statements must not end with semicolons. 
#include <iostream> 
#include <string> 
#include <cstdlib> // Has system() function 
using namespace std; 

int main(int argc, const char * argv[]) 
{ 
    // Always declare variables inside the function. 
    string username; 
    string password; 
    string choice; 
    bool finished = false; // Declare this. 
    bool askingToTryAgain = false; // For when the user is asked whether they want to try again. 

    do { 

     std::cout << "Enter Username: "; 
     std::getline(std::cin, username); 

     std::cout << "Enter Password: "; 
     std::getline(std::cin, password); 

     // Validate username and password. 
     if (username != "Joven" && password != "Fabricante7188") { 
      // If both the username and password are invalid, report it. 
      std::cout << "Invalid Username and Password. Try again? (y/n): "; 
     } else if (username == "Joven" && password == "Fabricante7188") { 
      // If both fields are valid, login is successful. 
      std::cout << "Correct Username and Password - Log In Successful.\n"; 
      finished = true; // Login is now complete, the loop will end. 
     } else { 
      // If just one of the fields is invalid, report which one it is. 
      if (username != "Joven") { 
       std::cout << "Invalid Username. Try again? (y/n): "; 
      } 
      if (password != "Fabricante7188") { 
       std::cout << "Invalid Password. Try again? (y/n): "; 
      } 
     } 

     if (finished == false) { 
      // If the login was unsuccessful, await user input for whether they wish to try again or not. 
      askingToTryAgain = true; 

      do { 
       // Fetch user input (y/n) 
       std::getline(std::cin, choice); 
       // Validate it. 
       if (choice != "y" && choice != "n") { 
        std::cout << "Enter 'y' or 'n'\n"; 
       } else { 
        askingToTryAgain = false; 

        if (choice == "y") { 
         // Nothing to do here. The parent loop will continue after this one stops. 
        } else if (choice == "n") { 
         finished = true; // The user wishes to quit. 
        } 
       } 
      } while (askingToTryAgain); 

     } 

    } while (finished == false); 

    system("pause"); // During testing I used 'sleep 4' (sleep for 4 seconds) because I'm running Linux. 
    return 0; 
} 

プログラム実行の流れを制御する。ループや条件は、C++のような構造化されたプログラミング言語の 'goto'文よりはるかに良い解決策です。

参考資料としてhttp://cplusplus.com/をご覧ください。

これが役に立ちます。

+0

私はあなたのコードを試したいですが、残念ながらそれは私のVSで実行されません。それは間違いを続ける! –

+0

私はあなたのコンパイラの出力を参照する必要があります。どのようなエラーがありましたか?私は自分の環境でそれを試し、エラーを修正するコードを編集します。 – scorch

+0

私は自分のコードをテストして変更したので、今すぐ動作します。構文エラーを削除し、新しいブール変数を追加しました。私は完全なソリューションを含めるために自分の投稿を更新しました。うまくいけばそれは今あなたのために働く。 – scorch

関連する問題