Previous Question (not mine)

This is a discussion on Previous Question (not mine) within the C++ Programming forums, part of the General Programming Boards category; I have been reading a previous question, to try to figure out a problem. The problem regards leaving \n in ...

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

    Question Previous Question (not mine)

    I have been reading a previous question, to try to figure out a problem. The problem regards leaving \n in the input buffer, thus i am having to press enter twice. One member replied stating using cin.ignore should sort this. I have tried in vain but it just doesnt want to play. I have tried cin.ignore (254,'\n''); etc etc (and a load of other things too.) If you could just scan my code and see why I need to press enter twice on each input I would be most grateful...When using cin.ignore(etc) I actually had to press enter THREE times to continue. Thanx.


    const int PASSQUANT=10;
    const int MAX_CHARS=7;
    const int ATTEMPTS=3;

    void main()

    {

    char Password [PASSQUANT][MAX_CHARS];
    char User_Password[MAX_CHARS], Buffer[255];////255 max keyboard buffer
    int Trys=0;
    int ReturnVal;

    for (int i=0;i<PASSQUANT;i++)
    {
    cout << "Please enter supervisor password for level "<<i+1<< "access ";
    cin.getline(Buffer,255);///use the temp buffer to validate length


    while (strlen(Buffer) !=(MAX_CHARS-1))//now checking password length
    {
    cout << "INCORRECT PASSWORD LENGTH ! ";
    cout << "\nPlease enter supervisor password for level "<<i+1<< "access ";
    cin.getline(Buffer,255); //obtain password again
    }

    strcpy (Password[i],Buffer); //put the validated password into real variable

    } //end first for loop

    clrscr();


    etc etc

    Program goes on etc, on debugger, all 10 passwords are shown niceley in the array with end of markers etc....just have to press enter twice!!




    borland 5 proff
    Such is life.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    17
    got me stuffed why u need to hit enter twice, I compiled using Microsoft Visual C++ and didn't have too. Well I made some changes to your code as shown below, found 1 logic prob with your code (or it could be that u are just forcing user to have exactly 7 char passwords, well here is what I got

    Code:
    #include <iostream.h>
    #include <string.h>
    
    const int PASSQUANT=1; 
    const int MAX_CHARS=7; 
    const int ATTEMPTS=3; 
    
    int main() 
    
    { 
    
    	char Password [PASSQUANT][MAX_CHARS]; 
    	char User_Password[MAX_CHARS], Buffer[255];////255 max keyboard buffer 
    	int Trys=0; 
    	int ReturnVal; 
    
    	for (int i=0;i<PASSQUANT;i++) 
    	{ 
    		cout << "Please enter supervisor password for level "<<i+1<< " access "; 
    		cin.getline(Buffer,strlen(Buffer));///use the temp buffer to validate length 
    
    
    		while (strlen(Buffer) > (MAX_CHARS-1))//now checking password length 
    		{ 
    			cout << "INCORRECT PASSWORD LENGTH ! "; 
    			cout << "\nPlease enter supervisor password for level "<<i+1<< "access "; 
    			cin.getline(Buffer,255); //obtain password again 
    		} 
    
    		strcpy (Password[i],Buffer); //put the validated password into real variable 
    
    	} //end first for loop
    	return 0;
    }
    -----------------------------------------------
    everready

    To code, or not to code, that is the question.

    Well the answer is 'TO CODE' of cause

  3. #3
    Unregistered
    Guest
    thanks for that, but it is definate that I have to press enter twice....it takes the input and then drops o a new line-therefore i need to press enter again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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