arrays and validation (basic stuff)

This is a discussion on arrays and validation (basic stuff) within the C++ Programming forums, part of the General Programming Boards category; Hello , I have been pulling my hair out for a week now. I am just learning to program. I ...

  1. #1
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158

    Unhappy arrays and validation (basic stuff)

    Hello , I have been pulling my hair out for a week now. I am just learning to program. I have some code here, but i cant grasp 2 dimensional arrays, or moreover why this code doesnt work. Any help is appreciated.

    //prog to allow supervisor to input 10 passwords and then allow user to try to
    //login. Prog should tell user what level of access they have upon entering a
    //valid password.Password will be 6 characters long.
    #include <iostream.h>
    #include <iomanip.h>
    #include<string.h>

    const int MAXIMUM =10;
    const int CHARS=7;

    void main ()
    {
    char Password[MAXIMUM][CHARS];
    int i=0;

    for (i; i<MAXIMUM; i++)
    {
    cout << "Please enter Supervisor password for level " << i+1<< " access ";
    cin.getline(Password[i],CHARS);

    if(strlen(Password[i])!= CHARS)
    {
    cout << "Invalid length";
    cin.getline(Password[i],CHARS);
    }
    }
    getchar();
    }


    this is only the first part but the debugger shows WEIRD stuff in password[]. Using Borland 5.Any tips for an aspiring ! learner?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    have a look at the comments

    Code:
    //prog to allow supervisor to input 10 passwords and then allow user to try to
    //login. Prog should tell user what level of access they have upon entering a
    //valid password.Password will be 6 characters long.
    #include <iostream.h>
    #include <iomanip.h>
    #include<string.h>
    
    const int MAXIMUM = 10;
    const int CHARS = 7;
    
    void main(void)
    {
    	char password[MAXIMUM][CHARS];
    	
    	for(int i = 0; i<MAXIMUM; i++)
    	{
    		cout << "Please enter Supervisor password for level " << i + 1 << " access : ";
    
    		cin.getline(password[i], CHARS);
    	
    		// this is where your problem is. cin.getline(password, CHARS) retrieves CHARS-1 characters max
    		// because it null-terminates the string. so you'll only ever get 6 characters in the buffer.
    		// what you should do is check to see if the length is == to CHARS - 1 like this:
    		if(strlen(password[i]) != CHARS - 1)
    		{
    			cout << "Invalid length";
    			cin.getline(password[i], CHARS);
    		}
    	}
    }

    By the way, there are other bugs in there... but i'll let you figure them out
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    Thankyou very much for your help. I will go and try that.And look for the other bugs as well-and this is only the start of my program.! LOL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Arrays question
    By taugust7 in forum C++ Programming
    Replies: 36
    Last Post: 04-14-2009, 12:29 PM
  2. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 11:07 AM
  3. Sorry, validation and arrays again!
    By Fountain in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2001, 06:17 PM

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