a new day a new problem

This is a discussion on a new day a new problem within the C++ Programming forums, part of the General Programming Boards category; hi im having a bit of a problem with a gets function Code: void store::findF(void) { int i = 0; ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Question a new day a new problem

    hi im having a bit of a problem with a gets function

    Code:
    void store::findF(void)
    {
    	int i = 0;
    	bool serch = false;
    	char person[20];
    	
    	//system("cls");
    	
    	cout << "please enter the name of the person you wish to find their details :  " << endl;
    	 gets (person);
    	cout << person;
    	
        
    	for(i = 0; i < Voters; i++)
    	{
    	  if(!strcmp(np[i].name, person) )
    		  {
    		   	  
    		    cout << "Eurika" << np[i].name <<  "\n" << np[i].phone << "\n" << np[i].address << "\n" ;
    			 cout << np[i].email << "\n" << np[i].party ;
    		 	 serch = true;
    		  }
       // cout<<i;
    	}
     
    	if(serch == false)
    	  {
    	 	 cout << "Sorry record not found.";
    	  }
    }
    ok ignore any small errors in here the problem i have concerns the gets function
    when i call this function the gets returns a null value and therefore prints eurikia and a list of names from my database/list

    any help would be most appreciated

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    This is now the third time I've warned you not to use gets. Use fgets. Better yet, use cin.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    would this work?
    cin >> person;
    gets(person)

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    cin >> person;

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Lightbulb

    sorry that works fine the cin i was struggiling because fgets kept giving me an error thanks a bunch

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,831
    Just use std::string and getline().
    Nice and safe, and lots of things to help you parse the result.

    gets() and short char arrays in C++ - Ugh!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    88
    Code:
    cin >>person;
    This works only if the string entered is smaller than the block allocated for person. This is better:
    Code:
    char person[MAX_SIZE];
    cin.getline(person, MAX_SIZE);
    This is best:
    Code:
    string person;
    getline(cin, person);

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Just in case, I'd make person a MAX_SIZE + 1 array, for the \0 character.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Actually getline handles the null character correctly if you pass it the array size, so it will read in a maximum of MAX_SIZE - 1 characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructive criticism/suggestions
    By LineOFire in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 10:32 AM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

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