Spot the error

This is a discussion on Spot the error within the C++ Programming forums, part of the General Programming Boards category; Anyone care to help me in getting my program to compile, there are currently 3 errors in my code. Any ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    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;
    }

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    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
    Monday - what a way to spend a seventh of your life

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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>
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    4
    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.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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;

    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    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.

  8. #8
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    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
    Monday - what a way to spend a seventh of your life

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    how would i do that if i wanted to use strcmp??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 10:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21