2012-11-21 2 views
0

이 다 나는 다음과 같은 포함 런타임 오류를 생성하는 작업 된 ++ 프로그램 :C 프로그램 검증 오류 및 건너 뛰기 사용자 입력

1.generatePass 방법은 검증 프로세스를 생략합니다.

2.validatePass 메서드는 사용자 입력을 건너 뛰고 유효성 검사를 건너 뜁니다.

저는 C++을 처음 접했습니다. Java와 사용자 입력에 추가 할 수있는 일종의 nextLine 메서드가 있습니까? 유효성 검사가 건너 뛴 이유는 무엇입니까? 다른 시각은 도움이 될 것입니다. 다른 것을 찾지 못했습니다. 지금까지.

고마워, 브라이언.

#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

감사합니다. 매우 통찰력 있고 유익한 답변입니다. 나중에이 코드를 나중에 구현하여 잘 작동합니다. –