0

我一直在处理的这个 c++ 程序正在生成涉及以下内容的运行时错误:

1.generatePass 方法跳过验证过程。

2.validatePass 方法跳过用户输入,也跳过验证。

我是 C++ 的新手,是否有某种 nextLine 方法可以像在 java 中一样添加到用户输入中,以及关于为什么要跳过验证的任何想法。不同的观点会有所帮助,因为到目前为止我还没有找到任何其他东西。

谢谢,布赖恩。

#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
4

1 回答 1

1

我认为主要的问题是,虽然你有构建块,但你没有充分跟上规划你的设计。别担心,它是可以修复的。

1.generatePass 方法跳过验证过程。

generatePass() 在自身内部执行验证,但您可能希望将这些行...

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

...进入一个 else 块,这样用户就不会认为密码没问题。您可能还想利用 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”函数中获得一些很好的实用程序,所以这只是一个想法。)

于 2012-11-21T22:11:49.483 回答