char array issues
-
#include
#include
#include
using namespace std;
char password1[25];
int x=1;
int i=0;
int q;
int main()
{
do{cout << "Enter a test password: " ; for(i=0;i<25;i++){ password1\[i\]=getch(); if(((password1\[i\]>=65)&&(password1\[i\]<=90)) || ((password1\[i\]>=97)&&(password1\[i\]<=122)) || ((password1\[i\]>=48)&&(password1\[i\]<=57)) || ((password1\[i\]>=35)&&(password1\[i\]<=38)) || ((password1\[i\]>=40)&&(password1\[i\]<=42)) || ((password1\[i\]==33))||((password1\[i\]==64)) || ((password1\[i\]==94)) || ((password1\[i\]==32))){cout << "\*"; i+1;} if(password1\[i\]==13){i-1; goto end;} else{i-1; cout << "";} } end: for (i; i < strlen(password1); i++){ password1\[i\] = '\\0';} cout << "\\nYour password is "; for(q=0;q<i;q++){cout><< password1\[q\];} Sleep(2000);
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
system("cls");
}while(x=1);
end1:
cout << "\nIT WORKS!";
getch();
return 0;
}I cannot seem to get the program to check the password properly, it either doesn't work. Or anything works
-
#include
#include
#include
using namespace std;
char password1[25];
int x=1;
int i=0;
int q;
int main()
{
do{cout << "Enter a test password: " ; for(i=0;i<25;i++){ password1\[i\]=getch(); if(((password1\[i\]>=65)&&(password1\[i\]<=90)) || ((password1\[i\]>=97)&&(password1\[i\]<=122)) || ((password1\[i\]>=48)&&(password1\[i\]<=57)) || ((password1\[i\]>=35)&&(password1\[i\]<=38)) || ((password1\[i\]>=40)&&(password1\[i\]<=42)) || ((password1\[i\]==33))||((password1\[i\]==64)) || ((password1\[i\]==94)) || ((password1\[i\]==32))){cout << "\*"; i+1;} if(password1\[i\]==13){i-1; goto end;} else{i-1; cout << "";} } end: for (i; i < strlen(password1); i++){ password1\[i\] = '\\0';} cout << "\\nYour password is "; for(q=0;q<i;q++){cout><< password1\[q\];} Sleep(2000);
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
system("cls");
}while(x=1);
end1:
cout << "\nIT WORKS!";
getch();
return 0;
}I cannot seem to get the program to check the password properly, it either doesn't work. Or anything works
gamefreak2291 wrote:
if(((password1[i]>=65)&&(password1[i]<=90)) || ((password1[i]>=97)&&(password1[i]<=122)) || ((password1[i]>=48)&&(password1[i]<=57)) || ((password1[i]>=35)&&(password1[i]<=38)) || ((password1[i]>=40)&&(password1[i]<=42)) || ((password1[i]==33))||((password1[i]==64)) || ((password1[i]==94)) || ((password1[i]==32))){cout << "*"; i+1;}
Have you considered
isdigit()
,isalnum()
,isalpha()
,isxdigit()
, etc? It won't necessarily solve your problem (I've not looked to see what the actual problem is), but making it easier to read won't hurt.gamefreak2291 wrote:
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
Is this supposed to do something?
gamefreak2291 wrote:
}while(x=1);
Look at this line real close. Also, your
i+1
andi-1
statements are not doing what you expect.gamefreak2291 wrote:
for(q=0;q<i;q++){cout><< password1[q];}
Does this even compile? What warning level are you using?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
gamefreak2291 wrote:
if(((password1[i]>=65)&&(password1[i]<=90)) || ((password1[i]>=97)&&(password1[i]<=122)) || ((password1[i]>=48)&&(password1[i]<=57)) || ((password1[i]>=35)&&(password1[i]<=38)) || ((password1[i]>=40)&&(password1[i]<=42)) || ((password1[i]==33))||((password1[i]==64)) || ((password1[i]==94)) || ((password1[i]==32))){cout << "*"; i+1;}
Have you considered
isdigit()
,isalnum()
,isalpha()
,isxdigit()
, etc? It won't necessarily solve your problem (I've not looked to see what the actual problem is), but making it easier to read won't hurt.gamefreak2291 wrote:
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
Is this supposed to do something?
gamefreak2291 wrote:
}while(x=1);
Look at this line real close. Also, your
i+1
andi-1
statements are not doing what you expect.gamefreak2291 wrote:
for(q=0;q<i;q++){cout><< password1[q];}
Does this even compile? What warning level are you using?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
no, i have not considered the others, because it took long enough to get the ascii values going.
DavidCrow wrote:
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
yes, that takes you to the end which prints that the password works.
DavidCrow wrote:
}while(x=1);
okay, im looking at it. It does exactly what it is supposed to, that bit of code is only in the program at this point for debugging purposes, it allows the user to keep trying to get the correct password. As for i+1, and i-1.. i++ and i-- was not working properly, I changed them to +1 and -i, then had them print to make sure they were working. And of course it compiles. the reason why "for(q=0;q<i;q++){cout><< password1[q];}" is in the code is because password1[i] allows you to enter 25 characters max, and if you only enter 5 characters, the rest of them are filled in with null terminators. However, I don't want my password to be full of null terminators, so if i input 5 characters, i is currently 5, so i have q=i; then i fills out with the rest of the array with null terminators. Then the program prints password1[q] from 0-5, which is really like printing password1[0], password1[1], password1[2], password1[3], password1[4], and password1[5]. I previously had the program print the entire array however it was over lapping on multiple tries, so now it only prints the needed numbers. Thanks again for the help
-
no, i have not considered the others, because it took long enough to get the ascii values going.
DavidCrow wrote:
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
yes, that takes you to the end which prints that the password works.
DavidCrow wrote:
}while(x=1);
okay, im looking at it. It does exactly what it is supposed to, that bit of code is only in the program at this point for debugging purposes, it allows the user to keep trying to get the correct password. As for i+1, and i-1.. i++ and i-- was not working properly, I changed them to +1 and -i, then had them print to make sure they were working. And of course it compiles. the reason why "for(q=0;q<i;q++){cout><< password1[q];}" is in the code is because password1[i] allows you to enter 25 characters max, and if you only enter 5 characters, the rest of them are filled in with null terminators. However, I don't want my password to be full of null terminators, so if i input 5 characters, i is currently 5, so i have q=i; then i fills out with the rest of the array with null terminators. Then the program prints password1[q] from 0-5, which is really like printing password1[0], password1[1], password1[2], password1[3], password1[4], and password1[5]. I previously had the program print the entire array however it was over lapping on multiple tries, so now it only prints the needed numbers. Thanks again for the help
gamefreak2291 wrote:
okay, im looking at it. It does exactly what it is supposed to, that bit of code is only in the program at this point for debugging purposes, it allows the user to keep trying to get the correct password.
better get a good C programming book.
This signature was proudly tested on animals.
-
gamefreak2291 wrote:
if(((password1[i]>=65)&&(password1[i]<=90)) || ((password1[i]>=97)&&(password1[i]<=122)) || ((password1[i]>=48)&&(password1[i]<=57)) || ((password1[i]>=35)&&(password1[i]<=38)) || ((password1[i]>=40)&&(password1[i]<=42)) || ((password1[i]==33))||((password1[i]==64)) || ((password1[i]==94)) || ((password1[i]==32))){cout << "*"; i+1;}
Have you considered
isdigit()
,isalnum()
,isalpha()
,isxdigit()
, etc? It won't necessarily solve your problem (I've not looked to see what the actual problem is), but making it easier to read won't hurt.gamefreak2291 wrote:
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
Is this supposed to do something?
gamefreak2291 wrote:
}while(x=1);
Look at this line real close. Also, your
i+1
andi-1
statements are not doing what you expect.gamefreak2291 wrote:
for(q=0;q<i;q++){cout><< password1[q];}
Does this even compile? What warning level are you using?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
Welcome again in the CP's memorable quotes page. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
#include
#include
#include
using namespace std;
char password1[25];
int x=1;
int i=0;
int q;
int main()
{
do{cout << "Enter a test password: " ; for(i=0;i<25;i++){ password1\[i\]=getch(); if(((password1\[i\]>=65)&&(password1\[i\]<=90)) || ((password1\[i\]>=97)&&(password1\[i\]<=122)) || ((password1\[i\]>=48)&&(password1\[i\]<=57)) || ((password1\[i\]>=35)&&(password1\[i\]<=38)) || ((password1\[i\]>=40)&&(password1\[i\]<=42)) || ((password1\[i\]==33))||((password1\[i\]==64)) || ((password1\[i\]==94)) || ((password1\[i\]==32))){cout << "\*"; i+1;} if(password1\[i\]==13){i-1; goto end;} else{i-1; cout << "";} } end: for (i; i < strlen(password1); i++){ password1\[i\] = '\\0';} cout << "\\nYour password is "; for(q=0;q<i;q++){cout><< password1\[q\];} Sleep(2000);
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
system("cls");
}while(x=1);
end1:
cout << "\nIT WORKS!";
getch();
return 0;
}I cannot seem to get the program to check the password properly, it either doesn't work. Or anything works
gamefreak2291 wrote:
if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
I suspect that line should read
if (0==strcmp(password1, "password")) { goto end1;}
That's how you compare two strings, anyway. What you've done is assign 'p' to the q'th character in password (probably overwriting the null terminator, I suspect). The rest of the expression is repeated application of the comma operator[^].
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p