I need some help in trying to find out why my program keeps crashing i get error
Run-Time Check Failure #2 - Stack around the variable 'password' was corrupted.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
constchar array_size= 7;
char password[array_size]={"Tccd03"};
bool valid,upper;
char len;
int index;
cout << "Please enter your password: " << endl;
cin >> password;
valid =true;
len =password[array_size];
if (len<array_size)
{
valid =false;
cout << "The password must be at least 6 characters long" << endl; }
else
{
}
upper =false;
index =1;
while (!(index>len))
{
if (password[7] >='A' && password[7] <='Z')
{
upper =true;
}
else
{
}
index =index+1;
}
if (upper==true)
{
}
else
{
cout << "The password must have at least one upper case character." << endl;
valid =false;
}
if (password[array_size] =*"Tccd03")
{
if (valid==true)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
}
return 0;
}
Your array of characters will now have the following element indices:
password[0]
password[1]
password[2]
password[3]
password[4]
password[5]
password[6]
Then you have code like this:
if (password[7] >='A' && password[7] <='Z')
password[7] is out of bounds and if you attempt to write to it the stack will become corrupted.
You still have several problems. For example what are you trying to do here?
1 2
len =password[array_size];
if (len<array_size)
Basically you are copying the character located at password[7] to len. Which would be garbage data. You are then saying if len < 7 do something. The char that would have been assigned to len is backed by some numerical value because each ASCII character has a numerical equivalent. So if it was 'A' that was copied in... which I think is 65, you would be saying if 65 < 7 which is always false... but you won't even get that far because password[7] cannot be assigend a value... any of this making sense? Step through your code and try to fix your errors.
Ok so i debugged alot of the code and fixed a few thing the only part that i have now is to verify the password. It satisfies the Cap and length conditions now just need it to verify the correct password everything that i type in is saying that the password is correct heres what i got
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
constchar array_size= 7;
char password[array_size]={"Tccd03"};
bool valid,upper;
char userinput;
int index,len;
cout << "Please enter your password: " << endl;
cin >> userinput;
valid =true;
len =5;
if (*password<userinput)
{
valid =false;
cout << "The password must be at least 6 characters long" << endl; }
else
{
}
upper =false;
index =1;
while (!(index>len))
{
if (userinput >='A' && userinput <='Z')
{
upper =true;
}
else
{
}
index =index+1;
}
if (upper==true)
{
}
else
{
cout << "The password must have at least one upper case character." << endl;
valid =false;
}
if (*password=userinput)
{
if (valid==true)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
}
return 0;
}
I was wondering if i should put a for loop to test each character
What are you trying to do here? You are dereferencing password and setting it = to userinput. = is the assignment operator, == is equal to.
We'll work on fixing up your code once I have time. You have the right idea but your approach is a bit off. You have too many unnecessary variables and it would be easier to use a string instead of an array of chars.
Ohh ok thanks i forgot about that, sorry i am still not very good at this as you can tell
i need it to compare the userinput to the password and either thank them or invalid
actually what i did was use a raptor flowchart and exported to VS 2010 and tried to rebuild it from there but i would agree that using a strcmp state would be easier i just don have it all down quite yet
thank you for your help with this
What is wrong with this i cannot seem to get it to say that the password is valid when i put in the correct password can someone please help me code is posted below
line 13: strcmp() returns 0 (which is interpreted as false in the if clause) if the parameters are equal not 0 otherwise. So you're doing the opposite of what you want.
line 48: Is it really your intention to check just the first letter if it's upper case?
no i have been trying isupper to verify all userinput for uppercase but cant get it to work do you have another idea? if so that would be great i have been working on this for a week
/* This program is designed to ask the user for a password an then verifies the password, and returns
** weather or not it is correct, and why.
*/
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>
usingnamespace std;
constchar password[]={'T','c','c','d','0','3'};
bool verifypass(char *userinput,bool valid) //password verification function
{
strcmp (userinput,password);
if (valid==true)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
int length;
length = strlen(userinput); //checks for number of characters
if ((length <= 6) && (length >= 6))
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[6];
cout << "Please enter your password: " << endl;
cin >> userinput;
if ((*userinput >='A') && (*userinput <='Z'))
{
upper =false;
valid =true;
}
else
{
valid=false;
cout << "The password must have at least one upper case character." << endl;
}
verify_chars(userinput,valid);
verifypass(userinput,valid);
return 0;
}
line 16 write: valid = (0 == strcmp (userinput,password));
line 17: it's ok to write if (valid)
line 56: there're several ways to do so. Here's 1:
1 2 3 4 5 6 7
char up_str[sizeof(userinput) / sizeof(*userinput)];
int l = strlen(userinput);
for(int i = 0; i <= l; ++i) // Note that '<=': copies the trailing 0 also
{
up_str[i] = toupper(userinput[i]);
}
upper = (0 == strcmp(up_str, userinput));
I'd recomend that you use string or make the 'userinput' in line 49 much larger (like 1024). In line 51 the program will crash (or silently fail) if the user enters more than 5 char (due to the trailing 0).
thank you for your help on this i would have never thought about that sometimes its good to get a fresh pair of eyes on a project. now all there is left is to check for numbers
Oops now that I read line 64 I see that I got something wrong. My approach is that all letters should be capitals. But you want to know if there's at least 1 so:
1 2 3 4 5 6 7 8 9 10 11
bool upper = false;
constint l = strlen(userinput);
for(int i = 0; i < l; ++i)
{
upper = (isupper(userinput[i]) != 0);
if(upper)
break;
}
bool valid = upper;
if(!upper)
cout << "The password must have at least one upper case character." << endl;
nevermind i was able to get the numbers part of it thank you for all of your help on this project it is done finally after what felt like an eternity final code posted below
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>
usingnamespace std;
constchar password[]={'T','c','c','d','0','3'};
bool verifypass(char *userinput,bool valid) //password verification function
{
valid = (0 == strcmp (userinput,password));
if (valid)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
int length;
length = strlen(userinput); //checks for number of characters
if ((length <= 6) && (length >= 6))
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
// this section is to verify uppercase charaters in userinput string
{
bool upper = false;
constint l = strlen(userinput);
for(int i = 0; i < l; ++i)
{
upper = (isupper(userinput[i]) != 0);
if(upper)
break;
}
bool valid = upper;
if(!upper)
cout << "The password must have at least one upper case character." << endl;
}
// Below wants to test the userinput for any digits in the string
bool digit_yes=false;
int num = 0;
int len = strlen(userinput);
for (int count = 0; count < len; count++)
if (isdigit(userinput[count]))
digit_yes=true;
if (!digit_yes)
{
valid=false;
cout <<"Passwords must include at least one digit (1-9)"<< endl;
}
else
{
valid=true;
}
verify_chars(userinput,valid);// fuction designed to test the number of characters
verifypass(userinput,valid); // Funtion designed to strcmp the password and userinput
return 0;
}