Thread: Loops won't iterate

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Loops won't iterate

    Hey folks,

    Working on a project here and I can't figure out why my loop won't iterate within my function. It's the for loop at the end. Could you take a look?

    Code:
    //Load File
    void load(entry *phonebook, int numOfEntries) {
            FILE *pRead;
            double totalSize;
            int num, m = 0;
            
            //Begin Reading
            pRead = fopen("phoneBook.txt", "r");
            
            if(pRead == NULL) {
                printf("\nFile Can't be Opened\n");
            }// End if
            
            //Else Read Sucessfull
            else {
                //Find File Total Size        
                fscanf(pRead, "%d", &totalSize);
                printf("\nGot total size. Total size is %d\n", totalSize);
                
                //reallocate memory
                entry *temp = realloc(phonebook, (totalSize+1) * sizeof(entry));
                
                //Debugging memory allocation error
                if (temp == NULL){
                    printf("\nMemory Allocation Error in LOAD FILE\n");
                }//End Debugging If
                
                //Memory allocation successfull
                else {
                    phonebook = temp;
                    printf("\nMemory Allocation Sucessful\n");
                    
                }//End Else
                
                for(m=0; m < totalSize; m++){
                        printf("1st time: i is %i, totalSize is %d\n", m, totalSize);phonebook[i].fName);
                        printf("2nd time: i is now %i, totalSize is %d\n", m, totalSize);
                }
                
                        printf("out of loop: i is now %i, totalSize is %d\n", m, totalSize);
                    
            } //End Read Sucessful Else
                
            fclose(pRead);
    }// End Load File

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    You've made totalSize a double (floating point) but you are using it as if it's an int. It should probably be an int.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2018
    Posts
    2
    thanks.

    I'm also trying to copy the lines from my file into a structure in an array. Each line in the file is formatted word,word,word
    I originally tried to just pick up everything between either spaces or commas (originally had spaces between words instead of commas) and copy each word into its section of the structure, but that's not working. I also tried to pick up the entire line and copy it into a variable, and then print that, but that just results in a single character being printed.
    Ideas on how to take each word from the line and put them into my structure?

    Code:
    					
    			char v1;
    			while (i < numOfEntries) {
    				printf("m is %i, totalSize is %d\n", i, numOfEntries);
    				fscanf(pRead,"%c", &v1);
    			
    				printf("Found contact number %i\n", i);
    				printf("contact is %c\n", &v1);
    				
    //				fscanf(pRead,"%s %s %s", phonebook[i].fName, phonebook[i].lName, phonebook[i].phoneNum);
    				
    				i++;
    			}

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    How about this:
    Code:
        while (i < numOfEntries) {
            fscanf(pRead, "%s ,%s ,%s",
                &phone[i].fName,
                &phone[i].lName,
                &phone[i].number);
            i++;
        }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Another problem you have is if realloc moves the memory to somewhere else, the caller has no idea where it moved to.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need to iterate through an std::map...
    By Programmer_P in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2011, 11:46 AM
  2. Loops that iterate/alter code in a binary fashion?
    By 1:1 in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2011, 06:32 PM
  3. iterate through keys of map
    By lord in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2008, 10:34 AM
  4. ::iterate
    By Coding in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2008, 05:46 AM
  5. iterate a 3d vector
    By Coding in forum C++ Programming
    Replies: 12
    Last Post: 07-31-2008, 05:35 PM

Tags for this Thread