-
Spot the error
Anyone care to help me in getting my program to compile, there are currently 3 errors in my code. Any help appreciated.
#include <iostream.h>
#include <string.h>
main()
{
const int max_size = 80;
char *password[] = {"aa0001", "aa0002", "aa0003", "aa0004", "aa0005", "aa0006",
"aa0007", "aa0008", "aa0009", "aa00010"};
char new_password[],answer;
int index, x,i, count, loop;
for (loop=0; loop <10 loop++)
{
count =0;
do
{
while ((new_password != password[i]) && (count <3))
{
cout << "enter a word up to 80 chars ";
cin >> new_password[i];
index = strlen(new_password);
if (new_password == password[0]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 1 ACCESS GRANTED!" << endl;
}
if (new_password == password[1]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 2 ACCESS GRANTED!" << endl;
}
if (new_password == password[2]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 3 ACCESS GRANTED!" << endl;
}
if (new_password == password[3]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 4 ACCESS GRANTED!" << endl;
}
if (new_password == password[4]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 5 ACCESS GRANTED!" << endl;
}
if (new_password == password[5]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 6 ACCESS GRANTED!" << endl;
}
if (new_password == password[6]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 7 ACCESS GRANTED!" << endl;
}
if (new_password == password[7]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 8 ACCESS GRANTED!" << endl;
}
if (new_password == password[8]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 9 ACCESS GRANTED!" << endl;
}
if (new_password == password[9]) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 10 ACCESS GRANTED!" << endl;
}
else
{
cout << "INVALID PASS" <<endl;
++count;
}
if (count >2)
cout <<"3 wrong attempts" << endl;
}
cout << "would you like another go ";
cin >> answer;
}
while (answer =='y');
}
return 0;
}
-
for a start it should be
int main (void) not main()
in the for loop
for (loop=0; loop <10 loop++)
there is a semicolon missing after loop <10
-
lots of errors....
heres one...
char new_password[],answer;
no size given for array.
heres another....
while ((new_password != password[i]) && (count <3))
{
cout << "enter a word up to 80 chars ";
cin >> new_password[i];
there is no array for you to use cos it was not constructed properly.
and another...
if (new_password == password[0]) && (index <7))
assuming you believe new_password to be an array then you appear to be trying to compare strings here so the function to do this is needed. Its called strcmp() and you will find it in <string.h> or <cstring>
-
3 errors huh?
I would say there are more than that....
Iain pointed out 2, but as a guide, here are some more;
Code:
char new_password[]
Try delaring a size for the array
Code:
if (new_password == password[3]) && (index <7))
Loads of these are missing brackets
Code:
while ((new_password != password[i]) && (count <3))
You have not set 'i' to anything.....it will compile, but the results will be undefined.....therefore you must fix this....
This was just from reading the errors my compiler spewed up...... try reading what the compiler is saying......it will point out loads of stuff and help you learn
-
thanx for the input guys as u can probably tell im new to c++ ive corrected those mistakes which u have pointed out and it now compiles but doent work as i had hoped.
#include <iostream.h>
#include <string.h>
main()
{
const int max_size = 80;
char *password[] = {"aa0001", "aa0002", "aa0003", "aa0004", "aa0005", "aa0006",
"aa0007", "aa0008", "aa0009", "aa00010"};
char new_password[10],answer;
int index,count,loop,a,b,c,d,e,f,g,h,i,j;
for (loop=0; loop <10; loop++)
{
count =0;
do
{
while (count <3)
{
cout << "enter a password ";
cin >> new_password;
index = strlen(new_password);
a = strcmp(password[0],new_password);
b = strcmp(password[1],new_password);
c = strcmp(password[2],new_password);
d = strcmp(password[3],new_password);
e = strcmp(password[4],new_password);
f = strcmp(password[5],new_password);
g = strcmp(password[6],new_password);
h = strcmp(password[7],new_password);
i = strcmp(password[8],new_password);
j = strcmp(password[9],new_password);
if ((a ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 1 ACCESS GRANTED!" << endl;
}
if ((b ==0 ) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 2 ACCESS GRANTED!" << endl;
}
if ((c ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 3 ACCESS GRANTED!" << endl;
}
if ((d ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 4 ACCESS GRANTED!" << endl;
}
if ((e ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 5 ACCESS GRANTED!" << endl;
}
if ((f ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 6 ACCESS GRANTED!" << endl;
}
if ((g ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 7 ACCESS GRANTED!" << endl;
}
if ((h ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 8 ACCESS GRANTED!" << endl;
}
if ((i ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 9 ACCESS GRANTED!" << endl;
}
if ((j ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 10 ACCESS GRANTED!" << endl;
}
else
{
cout << "INVALID PASS" <<endl;
++count;
}
if (count >2)
cout <<"3 wrong attempts" << endl;
}
cout << "would you like another go ";
cin >> answer;
}
while (answer =='n');
}
return 0;
}
whats happen is that when you input a password which is correct the right message appears but so does the "INVALID PASSWORD" any one have any idea why?
Also the for loop appears not to be working corractly, as i thought it would loop 10 times allowing u to enter 10 passwords in before it asked u if u wanted another go.
Any more help greatly appreciated.
-
what you need to do is to break out of the while loop instead of using else which if not used properly can have funny effects.
you use the keyword break to escape a loop. use it like this....
if ((a ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 1 ACCESS GRANTED!" << endl;
break;
}
if ((b ==0 ) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 2 ACCESS GRANTED!" << endl;
break;
}
etc.
and remove the else
if ((j ==0) && (index <7))
{
cout << "VALID PASS" <<endl;
cout <<"Level 10 ACCESS GRANTED!" << endl;
break; //add this
}
//else //remove this
{
cout << "INVALID PASS" <<endl;
++count;
}
if (count >2)
cout <<"3 wrong attempts" << endl;
}
-
In his program, he uses a lot of if statements. I have had to do this before as well. I was wondering if there was a more effecient way to do this.
-
when you have lots of if blocks they can become confusing especially with nested ifs if you go more than three or 4 layers.
An alternatic is the case switch
-
how would i do that if i wanted to use strcmp??