Thread: Encountering an infinite loop, unsure as to why

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    4

    Encountering an infinite loop, unsure as to why

    Hello all, recently I've been working on a HashTable data structure and have attempted to make it work for a project. Currently, I am encountering an infinite loop when I make it about two lines deep into the input file I need to parse through. From what I can surmise, my functions for key creation and inserting into the hash table are not the issue, but it may be how I'm parsing through the lines of my input file. I cannot see the fault in my logic that's causing the issue but I hope you may be able to help me. I'm currently just trying to get past reading and inserting the names from the input file so some of the comments in main are irrelevant, I will also attach the files to this post in zip file. Here is my code for main.

    Code:
    #include "HashTable.h"
    #include <string.h>
    #define MAX_LINE_LENGTH 100
    
    int main()
    {
        //create hash table size 100
        //open file nbaTeamNames.csv
        //read in each team name by line and use put to write an element for
        //the team with intial wins and loss of 0
        //open file nbaData2019
        //read by line and determine a winner and loser
        //use get function to obtain element associated with team and increment
        //wins and losses accordingly
        //after all lines read, reopen nbaTeamNames.csv
        //print out team name and win loss count per line
        //use get function to get win loss count
        //Print result should look like
        //"San Antonio Spurs: XX Wins - XX Losses"
        HashTable Hash = newHashTable(100);
        Element tempElm;
        Element *e;
        FILE *inputFile;
        inputFile = fopen("nbaTeamNames.csv", "r");
        if(inputFile == NULL){
    
      perror("Error opening file");
      return -1;
        }
        
        char nextLine[MAX_LINE_LENGTH];
        char currLine[MAX_LINE_LENGTH];
        
        while(fgets(nextLine, MAX_LINE_LENGTH, inputFile)!= NULL){
            int deli = strlen(nextLine) - 1;
            if(nextLine[deli] == '\n'){
                nextLine[deli-1] = '\0';
            }
            sscanf(nextLine, "%[^\n]\n", currLine);
            strcpy(tempElm.szTeamName, currLine);
            tempElm.losses = 0;
            tempElm.wins = 0;
            put(Hash, tempElm);
            printf("%s\n", tempElm.szTeamName); 
        }
        fclose(inputFile);
        
        return 0;
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,008
    Code:
            if(nextLine[deli] == '\n'){
                nextLine[deli-1] = '\0';
            }
    Why are you setting nextLine[deli-1] to '\0' ?
    Instead of nextLine[deli] ?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    4
    Ah, it was because I was attempting to get the carriage return character at the end of the lines of my input file. I've since updated the declaration to be as such:

    Code:
    i= 0;
    while(nextLine[i] != '\r' && nextLine[i] != '\n' && nextLine[i] != '\0')
         i++;
    
    
    if(nextLine[i] == '\r' || nextLine[i] == '\n')
         nextLine[i] = '\0';
    However, I still encounter my glaring issue.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,008
    Better in my viewpoint is.

    Edit: But, yours looks as good; I just try to avoid loops when I think they are not needed.

    Code:
    if(nextLine[deli] == '\n' || nextLine[deli] == '\r'){
        nextLine[deli] = '\0';
        if(nextLine[deli-1] == '\n' || nextLine[deli-1] == '\r'){
            nextLine[deli-1] = '\0';
        }
    }
    Last edited by stahta01; 05-06-2020 at 03:49 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,008
    Did you try commenting out the "put(Hash, tempElm);" call?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,008
    I would replace "strcpy" with "strncpy" because it is safer and might be the cause of your issue.

    Tim S
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2020
    Posts
    4
    I attempted your first suggestion and unfortunately it yielded much of the same results. omitting put(Hash, tempElm) will get all of the team names but will not insert those names into the hashTable. What would you suggest I set the buffer size of strncpy to be?
    Last edited by JMerritt; 05-06-2020 at 03:56 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,008
    Code:
        char nextLine[MAX_LINE_LENGTH];
        char currLine[MAX_LINE_LENGTH];
    To be safe use
    Code:
        char nextLine[MAX_LINE_LENGTH+1];
        char currLine[MAX_LINE_LENGTH+1];
    
        nextLine[MAX_LINE_LENGTH] = '\0';
        currLine[MAX_LINE_LENGTH] = '\0';
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    May 2020
    Posts
    4
    Sorry to do this but I had reached out to a colleague and he'd pointed out that my newHashTable function had a flaw that was making my hash table matrices be of array size int instead of array size HashTableEntry. My issue has since been resolved. Thank you for your input's!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop in insertNode function is an infinite loop
    By blongnec in forum C Programming
    Replies: 8
    Last Post: 03-19-2016, 09:57 PM
  2. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  3. infinite loop
    By hlam in forum C Programming
    Replies: 4
    Last Post: 10-29-2008, 01:16 AM
  4. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  5. infinite loop
    By puckparches in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2005, 10:12 PM

Tags for this Thread