Thread: Help with bug in freshmen level homework

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    Help with bug in freshmen level homework

    Good Morning, There is a bug in my code and I have been trying to figure out why its happening, I have never seen a bug like this and in the main function when I print out the queried first name and last name, it properly reads and prints out, but for some reason if you look in my code on line 314, it is not printing out last name and printing a blank, instead. This causes my find_closest_birthday function to not work properly because it depends on strcmp'ing the first and last name. Any clue on what might be causing this? It would really help if you could take a look at my code and help point me in the right direction.
    Code: [C] # include <stdio.h> # include <stdlib.h> # include <string.h> # define MAX - Pastebin.com
    Output: [Bash] Terminal output - Pastebin.com

    At the time I didn't realize there was a weird for-loop at line 92, anyhow I ended up commenting out that for loop but I get the same bug.

    The output should be:
    Code:
    j = 0
    
    i = 0
    
    classmate_First Name = JOHN
     classmate_Last Name = SMITH
    Queried_First Name = JOHN
    Queried_Last Name = SMITH
    but its only outputting, with the missing queried last name.
    Code:
    j = 0
    
    i = 0
    
    classmate_First Name = JOHN
     classmate_Last Name = SMITH
    Queried_First Name = JOHN
    Queried_Last Name =



    Any reason why its doing this?

    Edit: Sorry for link instead of using the code blocks... Hope it doesn't matter.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How many did you allocate?

    Code:
        int i, num_classes, num_students, num_queried;
    ...
        // Allocating space for the queried student structures.
        person *queried_student = malloc(sizeof(person)*num_queried);
     ...
        // Scan in the number of queried students
        fscanf(ifp, "%d", &num_queried);
    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.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Quote Originally Posted by Salem View Post
    How many did you allocate?

    Code:
        int i, num_classes, num_students, num_queried;
    ...
        // Allocating space for the queried student structures.
        person *queried_student = malloc(sizeof(person)*num_queried);
     ...
        // Scan in the number of queried students
        fscanf(ifp, "%d", &num_queried);
    2

    Here's the relevant text file:
    2
    5
    PETER GRIFFIN NOVEMBER 30 1989
    ERIC CARTMAN AUGUST 17 1988
    STEVE JOBS OCTOBER 21 1988
    JOHN SMITH APRIL 20 1980
    BILL CLINTON NOVEMBER 2 1990
    2
    JOHN SMITH
    BILL CLINTON
    4
    DAN MARINO SEPTEMBER 15 1961
    JOHN ELWAY JUNE 28 1960
    JOE MONTANA JUNE 11 1956
    DAN FOUTS JUNE 10 1951
    1
    JOE MONTANA

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    The line after bill Clinton is num queried

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Here's a clue - C does not predict the future.

    > person *queried_student = malloc(sizeof(person)*num_queried);
    What is the value of num_queried here...

    > fscanf(ifp, "%d", &num_queried);
    when you only read it here?
    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.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by C0__0D View Post
    The line after bill Clinton is num queried
    you don't know that value when queried_student is allocated.
    kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. entry level homework help: Passing Arrays into functions.
    By DHart07 in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2010, 09:11 AM
  2. Level 3, homework help!
    By Tarento in forum C Programming
    Replies: 1
    Last Post: 04-19-2006, 03:18 PM
  3. should I go up a level
    By algi in forum Windows Programming
    Replies: 1
    Last Post: 12-07-2004, 04:59 PM
  4. C++ to the next level
    By Daggie in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2003, 07:43 AM
  5. Low-Level and High-level ::: C-style And C++ Style
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2001, 05:05 PM