Thread: Another question with my darn program

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

    Another question with my darn program

    Right what i want to happen is that the program starts i want u to be able to enter 10 passwords in one after another then once this has been done then ask the user if they want another go.
    Whats happening at the moment is that every time u have entered one pass word it asks you if want another go. Ive tryed moveing the for loop and the do loop around and havent had any luck. Anyone have any ideas how to solve this. Thanks

    #include <iostream.h>
    #include <string.h>
    #include <conio.h>
    main()
    {

    const int MAX_SIZE = 10;
    int index,count,loop,compare0, compare1,compare2,compare3,compare4,compare5,
    compare6,compare7,compare8,compare9;
    char *password[] = {"aa0001", "aa0002", "aa0003", "aa0004", "aa0005", "aa0006",
    "aa0007", "aa0008", "aa0009", "aa0010"};
    char new_password[MAX_SIZE],answer;


    count =0;

    do
    {

    for (loop=0; loop <10; loop++)
    {

    cout << "enter a password ";
    cin >> new_password;

    clrscr();

    index = strlen(new_password);
    compare0 = strcmp(password[0],new_password);
    compare1 = strcmp(password[1],new_password);
    compare2 = strcmp(password[2],new_password);
    compare3 = strcmp(password[3],new_password);
    compare4 = strcmp(password[4],new_password);
    compare5 = strcmp(password[5],new_password);
    compare6 = strcmp(password[6],new_password);
    compare7 = strcmp(password[7],new_password);
    compare8 = strcmp(password[8],new_password);
    compare9 = strcmp(password[9],new_password);

    if ((compare0 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 1 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare1 ==0 ) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 2 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare2 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 3 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare3 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 4 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare4 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 5 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare5 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 6 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare6 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 7 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare7 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 8 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare8 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 9 ACCESS GRANTED!" << endl;
    break;
    }

    if ((compare9 ==0) && (index <7))
    {
    cout << "VALID PASS" <<endl;
    cout <<"Level 10 ACCESS GRANTED!" << endl;
    break;
    }



    {
    cout << "INVALID PASS" <<endl;
    ++count;
    }

    if (count >2)
    {
    cout <<"3 wrong attempts" << endl;
    count =0;
    }


    }//for loopend

    cout << "would you like another go ";
    cin >> answer;

    } //do end
    while (answer =='y');







    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    The problem is here:

    cout << "enter a password ";
    cin >> new_password;

    new_password is a char array of 10 elements. You should use
    a 2d array or an array of pointers. Then set up a loop for
    reading the keyboard input before making the comparisons.

    Ted

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    why do u have the invalid password block in brackets??

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main(void)
    {
    	const int MAX_SIZE = 10;
    	const int MAX_PASSWORD = 7;
    	const char* passwords[] = {
    					"aa0001", "aa0002", "aa0003", "aa0004", "aa0005",
    					"aa0006", "aa0007", "aa0008", "aa0009", "aa0010" };
    	char userInput[MAX_SIZE];
    
    	do
    	{
    		cout << "Enter your password > ";
    		cin.getline(userInput, sizeof(userInput));
    
    		for(int i = 0; i < 10; i++)
    		{
    			if(strcmp(userInput, passwords[i]) == 0)
    			{
    				break;
    			}
    		}
    
    		if(i < 10)
    		{
    			cout << "Valid Password. Level " << i + 1 << " access granted\n";
    		}
    		else
    		{
    			cout << "Invalid Password.\n";
    		}
    
    		cout << "Try again? > ";
    		cin.getline(userInput, sizeof(userInput));
    	} while(userInput[0] == 'y' || userInput[0] == 'Y');
    
    	return(0);
    }
    This works under MSVC++ 6.0

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM