[英]c++ program method errors visual studio
Ok,so im making a c++ program that has two seperate methods to generate a random password and one to take a user generated and validate it.But im getting errors with the methods and returning passwords.Im new to c++ so id appreciate if someone could give me some pointers in where im making mistakes.Im having trouble in particular with declaring methods as I'm not sure if they take parameters and if they should be int or other.I get the following errors: 好的,所以我正在制作一个c ++程序,该程序具有两种分别生成随机密码的方法和一种让用户生成并验证它的方法。但是我在使用该方法并返回密码时出错。我是c ++的新手,所以id希望有人可以给我一些提示,指出我在哪里出错。我在声明方法时遇到了麻烦,因为我不确定它们是否带参数以及它们是否应该为int或其他类型。我遇到以下错误:
error C4430: missing type specifier - int assumed.
错误C4430:缺少类型说明符-假定为int。 Note: C++ does not support default-int
注意:C ++不支持default-int
error C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
错误C4244:“参数”:从“ time_t”到“ unsigned int”的转换,可能丢失数据
error C2082: redefinition of formal parameter 'str'
错误C2082:重新定义形式参数'str'
error C2660: 'countLetters' : function does not take 6 arguments
错误C2660:“ countLetters”:函数未采用6个参数
error C2110: '+' : cannot add two pointers
错误C2110:“ +”:无法添加两个指针
error C2660: 'countLetters' : function does not take 6 arguments
错误C2660:“ countLetters”:函数未采用6个参数
error C2110: '+' : cannot add two pointers
错误C2110:“ +”:无法添加两个指针
warning C4018: '<' : signed/unsigned mismatch
警告C4018:“ <”:有符号/无符号不匹配
Thanks, 谢谢,
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <ctime>
#include <string.h>
using namespace std;
#define MAX 80
//declaring functions
int showMenu();
int generatePass();
int validatePass();
int countLetters(char *, 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("%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.
string generatePass(string str) {
char password[MAX + 1];
int iChar, iUpper, iLower, iSymbol, iNumber, iTotal;
printf("\n\n\t\tGenerate Password selected ");
printf("\n\n\t\tPassword creation in progress... ");
srand(time(0));
string str =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!£$%^&*()_+=#@;";
int pos;
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 " + password);
printf("\n\n\n");
system("pause");
} //end of generatePass method.
//method to validate a user generated password following password guidelines.
int 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(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 " + password);
printf("\n\n\n");
system("pause");
} //end validatePass method
int countLetters(char * Password, int * Upper, int * Lower, int * Symbol,
int * Number) {
int iTotal = 0, iC, tU = 0, tL = 0, tS = 0, tN = 0;
//strlen- function that returns length
for (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
//check upper case
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
Beside the errors listed by @ahenderson: 除了@ahenderson列出的错误之外:
printf("\n\n\n Your new password is verified " + password);
you can't add the char[] password
to a string literal. 您不能将char []
password
添加到字符串文字中。 Use two separate printf
s instead. 而是使用两个单独的
printf
。
printf("\n\n\n Your new password is verified ");
printf(password);
error C4430: missing type specifier - int assumed. 错误C4430:缺少类型说明符-假定为int。 Note: C++ does not support default-in
注意:C ++不支持default-in
This should be int main()
这应该是
int main()
error C2082: redefinition of formal parameter 'str' 错误C2082:重新定义形式参数'str'
Change one of the variable name to str2 将变量名称之一更改为str2
error C2660: 'countLetters' 错误C2660:“ countLetters”
Count the parameters, they do not match 计算参数,它们不匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.