Thread: ignore spaces & eof

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    ignore spaces & eof

    My program accepts a file that contains names. It looks like:
    Code:
    dave
    john
    daniel
    ...
    With the following code it works fine (as long as there are no extra lines or spaces after a name or at the beginning or end of a file)
    Code:
    char name[20];
    
    while(!inFile.eof())
    }
    			
    	inFile.getline(name, 20);
    	...
    }
    So, the getline reads the names into "name". However, if there are any spaces, it will count the spaces as part of the name or a new name if the spaces are on a separate line. I want it to ignore spaces. So, after searching through the forum, i saw the suggestion to make the getline:
    Code:
    inFile.getline(name, 20, ' ');
    So, i tired this. When i ran my program again, it got caught in an endless loop. So, i figured it was:
    Code:
    while(!inFile.eof())
    So, i wasn't sure what to put here. I read somewhere that i could do:
    Code:
    while(inFile.getline(name, 20))
    When i do this, my program crashes.
    So, i want to stay in the loop until there are no more names to read, and i do not want to read spaces into name.

    Any suggestions?
    Thanks
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you want to read upto space why not
    Code:
    std::string name;
    while(inFile >> name)
    {
    ...
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    I don't think i can declare name as a string because of my hash function. It adds characters passed to it.

    But when i changed:
    Code:
    inFile.getline(name, 20);
    to
    Code:
    inFile >> name;
    I get the same problem. It works fine with !inFile.eof() as long as there are no extra spaces or lines. If i change the
    Code:
    while(!inFile.eof())
    to
    Code:
     while(inFile >> name)
    it only counts half the votes.
    IDE - Visual Studio 2005
    Windows XP Pro

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    while(inFile >> name)
    Will work with c-strings, so there should be no problem. Can you explain what you mean by "it only counts half the votes"?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I don't think i can declare name as a string because of my hash function. It adds characters passed to it.
    And what is the problem with the string class here?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    "Can you explain what you mean by "it only counts half the votes"?"
    Halfway through the file of names, it hits a name that it doesn't record correctly. Now, this name appears hundreds of times above and has no problems. But, once it hits this certain instance of this name, it messes up. Now, if i copy and paste a duplicate of that name right after, it becomes fixed. It' weird, because it works perfect with !inFile.eof(). I haven't the slightest clue what is going on here, so i might just stick with !inFile.eof() since it at least works.

    "And what is the problem with the string class here?"
    Maybe this can be done using a string? If it can, i wouldn't know how to convert it.
    Here is my hash function:
    Code:
    char name[20];
    hashSpot = Hash(name, tableSize, count);
    
    int Hash(char *str, int table_size, int &count)
    {
    	int sum = 0;
    
    	/* Make sure a valid string passed in */
    	if (count++ && str == NULL)
    	{
    		return -1;                                                        count++;
    	}
    
    	/* Sum up all the characters in the string */
    	for( ; *str; count++ && str++)
    	{		
    		//Convert all uppercase letters to lowercase, so differently capitalized names will hash to the same spot.
    		if(count++ && (90-*str) >= 0 && (90-*str) <= 25)
    		{
    			*str = (*str+32);                                             count++;
    		}	
    		
    		sum += *str;	                                                  count++;	
    	}
    
    	/* Return the sum mod the table size */
    	return (sum % table_size);
    }
    IDE - Visual Studio 2005
    Windows XP Pro

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There is nothing in that function that you couldn't do with a string - it does nothing but accesses each character in the string.

    You could also change the function to not modify str (instead of adding 32 to a character you might subtract 32 from sum). Then the arguments type could be const char* and you could get that pointer from a string with c_str().

    Now, if you say you changed !inFile.eof() to inFile >> name, did you also keep the line within the loop that reads the file as well? Using eof is not very convenient for file input: you'd need to check if the file input was successful anyway, after getting a bit of input.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. Ignore the spaces when loading
    By hdragon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 02:30 PM
  4. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM
  5. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 05:18 AM