2012-11-21 53 views
0

該C++程序我已經工作時產生涉及以下運行時錯誤:C程序驗證錯誤和跳過用戶輸入

1.generatePass方法跳過驗證過程。

2.validatePass方法跳過用戶輸入並跳過驗證。

我是新來的c + +,是否有某種nextLine方法添加到用戶輸入像在java和任何想法,爲什麼驗證被跳過。不同的角度會有所幫助,因爲我還沒有發現任何東西至今。

謝謝,Brian。

#include <stdlib.h> 
#include <stdio.h> 
#include <iostream> 
#include <string> 
#include <windows.h> 
#include <ctime> 
#include <string.h> 
#include <time.h> 


using namespace std; 
#define MAX 80 


//declaring functions 
int showMenu();  
void generatePass();  
void validatePass(); 
int countLetters(char *,int *,int *,int *,int *,int *); 





int main() 
{ 

    int iChoice; 

    // have menu appear, user makes decision, do work, reshow menu 
    // do this until user enters 5 

    do 
    { 

     iChoice = showMenu(); 


    }while(iChoice != 3); 

    printf("\n\n\n"); 
    system("pause"); 


}//end of main 

//Methods placed here: 

//showMenu method calls program menu,either 1.generate password,2.enter password and validate. or 3.exit(close program) 
int showMenu() 
{ 
    int iChoice; 

    system("cls"); 
    printf("\n\n\t\tWelcome to Password Generator and Validator\n\n"); 
    printf("\n\t\t1. Generate"); 
    printf("\n\t\t2. Validate"); 
    printf("\n\t\t3. Exit"); 

    printf("\n\n\t\tEnter your menu choice: "); 
    fflush(stdin); 
    scanf_s("%d", &iChoice); 

    // user enters one of 3 values 
    // generate,validate or exit program 


    switch(iChoice) 
    { 
     case 1:  // generate 
     { 
      generatePass(); 

      break; 
     } 
     case 2:  // validate 
     { 
      validatePass(); 


      break; 
     } 
     case 3:  // exit 
     { 
      printf("\n\nProgram exiting!..."); 

      break; 
     } 
     default: 
     { 
      break; 
     } 
    }//end of switch 


    return(iChoice); 
} //end of showMenu 


//method to generate a random password for user following password guidelines. 
void generatePass() 

{ 

    int iChar,iUpper,iLower,iSymbol,iNumber,iTotal; 

    printf("\n\n\t\tGenerate Password selected "); 
    printf("\n\n\t\tPassword creation in progress... "); 

    int i; 
    char password[10 + 1]; 
    char strLower[59+1] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTUVWXYZ!£$%^&*"; 


    srand(time (0)); 

    for(i = 0; i < 10;i++) 
    { 
     password[i] = strLower[(rand() % 52)]; 

    } 
    password[i] = '\0'; 


    iChar = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal); 

    if(iUpper < 2) 
    { 
     printf("Not enough uppercase letters!!!\n"); 


    } 
    else if(iLower < 2) 
    { 
     printf("Not enough lowercase letters!!!\n"); 


    } 
    else if(iSymbol < 1) 
    { 
     printf("Not enough symbols!!!\n"); 


    } 
    else if(iNumber < 2) 
    { 
     printf("Not enough numbers!!!\n"); 


    } 
    else if(iTotal < 9 && iTotal > 15) 
    { 
     printf("Not enough characters!!!\n"); 


    } 
    printf("\n\n\n Your new password is verified "); 
    printf(password); 


    printf("\n\n\n"); 
    system("pause"); 



}//end of generatePass method. 








//method to validate a user generated password following password guidelines. 
void validatePass() 
{ 

    char password[MAX+1]; 
    int iChar,iUpper,iLower,iSymbol,iNumber,iTotal; 

    //shows user password guidelines 
    printf("\n\n\t\tPassword rules: "); 
    printf("\n\n\t\t 1. Passwords must be at least 9 characters long and less than 15 characters. "); 
    printf("\n\n\t\t 2. Passwords must have at least 2 numbers in them."); 
    printf("\n\n\t\t 3. Passwords must have at least 2 uppercase letters and 2 lowercase letters in them."); 
    printf("\n\n\t\t 4. Passwords must have at least 1 symbol in them (eg ?, $, £, %)."); 
    printf("\n\n\t\t 5. Passwords may not have small, common words in them eg hat, pow or ate."); 

    //gets user password input 
    printf("\n\n\t\tEnter your password following password rules: "); 
    gets_s(password); 


    iChar = countLetters(password,&iUpper,&iLower,&iSymbol,&iNumber,&iTotal); 

    if(iUpper < 2) 
    { 
     printf("Not enough uppercase letters!!!\n"); 


    } 
    else if(iLower < 2) 
    { 
     printf("Not enough lowercase letters!!!\n"); 


    } 
    else if(iSymbol < 1) 
    { 
     printf("Not enough symbols!!!\n"); 


    } 
    else if(iNumber < 2) 
    { 
     printf("Not enough numbers!!!\n"); 


    } 
    else if(iTotal < 9 && iTotal > 15) 
    { 
     printf("Not enough characters!!!\n"); 


    } 
    printf("\n\n\n Your new password is verified "); 
    printf(password); 


    printf("\n\n\n"); 
    system("pause"); 


}//end validatePass method 

int countLetters(char * Password,int * Upper,int * Lower,int * Symbol,int * Number,int * Total) 
{ 
    int iTotal = 0,iC = 0,tU = 0,tL = 0,tS = 0,tN = 0; 


    //strlen- function that returns length 
    for (int iC = 0;iC < strlen(Password);iC++) 
    { 

     printf("%d",Password[iC]); 
     //uppercase letters are in the range 65 - 90 
     //lowercase letters are in the range 97 - 122 
     //symbols are in the range 32-48 
     //numbers are in the range 47 - 58 


     if((Password[iC] < 64) && (Password[iC] < 91)) 
     { 
      tU++; 
      iTotal++; 

     } 
     else if((Password[iC] > 96) && (Password[iC] < 123)) 
     { 
      tL++; 
      iTotal++; 

     } 
     else if((Password[iC] > 32) && (Password[iC] < 48)) 
     { 
      tS++; 
      iTotal++; 

     } 
     else if((Password[iC] > 47) && (Password[iC] < 58)) 
     { 
      tN++; 
      iTotal++; 

     } 

     *Upper = tU;/*set value at memory address = tU,passing by reference saves memory used.*/ 
     *Lower = tL; 
     *Symbol = tS; 
     *Number = tN; 


    }//end for statement 


    return (iTotal); 
}//end of countLetters 

回答

1

我認爲主要的問題是,雖然你有積木,但你沒有充分跟上規劃你的設計。別擔心,它是可以修復的。

1.generatePass方法跳過驗證過程。

generatePass()執行驗證內本身,而是你可能想要把這些行...

printf("\n\n\n Your new password is verified "); 
printf(password); 

...到別的塊,使得用戶並不認爲密碼沒事的時候可以。您也可能想要利用generatePass()中的validatePass()函數,因爲您當前正在重複代碼。

2.validatePass方法跳過用戶輸入並跳過驗證。

也許這個鏈接與你使用gets_s()的問題有關:StackOverflow.com。無論如何,您可以暫時使用scanf,因爲它會讓事情變得簡單一些,並且您可以稍後再回來,並在需要時使您的程序更健壯。 Scanf將修復被繞過的輸入。驗證被忽略的方式與#1相同,所以如果你想知道你想要做什麼,那麼在這裏很容易。

我不確定我是否理解Java nextLine()問題,但是使用帶有%s的scanf讀取與Java Scanner.nextLine()大致相似的字符串函數。免責聲明:我將程序作爲C程序運行,因爲將它從C++轉換爲C是微不足道的(我想如果你打算用C風格的代碼編寫代碼,你可能會考慮只是把它變成一個C程序,同樣的,如果你希望程序更加便攜,你可以選擇避免只使用Windows的東西(例如'_s'函數),但是在你之前你可能會從這些'_s'函數中獲得一些很好的效用因爲它只是一個想法。)

+0

非常有見地的,內容翔實的答案。將在今晚晚些時候在代碼中實現它,希望它有效。 –