Hokerie
Hokerie

Reputation: 2595

Print output in C is glitchy

This program I have here doesn't print out correctly. Here is the input:

How many employees? 2

Enter employee 1 info: month/day/year, age, height, name: 3/21/34, 43, 3.4, hdsfgdf

Enter employee 2 info: month/day/year, age, height, name: 4/44/44, 44, 6.2, dfgtesas

This is the output:

Employee 1 information: 0/-1081689528/134514548, 16564212, 0.0, ��

Employee 2 information: 0/1/14608664, -1217230008, 0.0, ��ܹ����

My only guess is that I'm not populating the array correctly or maybe I'm printing out the addresses rather than the data. Am I right to be assuming that? Any bit of advice would help. Thank you so much!

My code is in 3 files.

This is the main:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "personal.c"

LIST *start, *end;

int main(void)
{
int i, numEmp;  

PERSON person;

start=end=NULL;
printf("How many employees? ");
    scanf("%d", &numEmp);
PERSON employees[numEmp];

for (i = 0; i < numEmp; i++)
{
printf("Enter employee %d info: month/day/year, age, height, name:\n", i+1);
scanf("%d/%d/%d,%d,%f,%s", &person.bday.month, &person.bday.day,
          &person.bday.year, &person.age, &person.height, person.name);
    add(&start, &end, person);

}
for (i = 0; i < numEmp; i++)
{
printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", i+1, employees[i].bday.month, employees[i].bday.day, employees[i].bday.year, employees[i].age, employees[i].height, employees[i].name);

delete(&start, &end);
}

This is the list with the structures:

#ifndef LIST_H_ /* to prevent re-definitions */
#define LIST_H_ /* that cause errors */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct DATE
{
    int month;
    int day;
    int year;
} DATE;

typedef struct PERSON
{
    char name[41];
    int age;
    float height;
    DATE bday;

} PERSON;

typedef struct list 
{
PERSON data; 
struct list *next;
} LIST;

#endif

And this is where all my methods are:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"


int delete (LIST **head, LIST **tail){
    LIST *temp;

    if (*head == NULL)
        return -1;

    PERSON retVal = (*head)->data;

    if(*head==*tail){
        free(*head);
        *head=*tail=NULL;
    }
    else{
        temp=(*head)->next;
        free(*head);
        *head=temp;
    }

    //return retVal;
}

void add(LIST **head, LIST **tail, PERSON data){
    if(*tail==NULL){
        *head=*tail=(LIST *) malloc(sizeof(LIST));
        (*head)->data=data;//use arrow when struct is pointer. Use . if have direct access to struct
        (*head)->next=NULL;
    }
    else{
        (*tail)->next= (LIST *) malloc(sizeof(LIST));
        *tail=(*tail)->next;
        (*tail)->data=data;
        (*tail)->next=NULL;
    }
}

Upvotes: 0

Views: 141

Answers (3)

dreamlax
dreamlax

Reputation: 95335

You create both a variable-length array (employees) and a linked list. You add elements to your linked list but try to print the contents of your variable-length array (which you never wrote anything to).

Either:

  1. Read the data into your VLA.

    for (i = 0; i < numEmp; i++)
    {
        printf("Enter employee %d info: month/day/year, age, height, name:\n", i+1);
        scanf("%d/%d/%d,%d,%f,%s", &employees[i].bday.month, &employees[i].bday.day,
          &employees[i].bday.year, &employees[i].age, &employees[i].height, employees[i].name);
    }
    
  2. Forget about the VLA and print only the contents of your linked list.

    i = 0;
    for (LIST *item = start; item != NULL; item = item->next)
        printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", ++i, item->data.bday.month, item->data.bday.day, item->data.bday.year, item->data.age, item->data.height, item->data.name);
    

Upvotes: 1

Rohan
Rohan

Reputation: 53326

printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", i+1, 
        employees[i].bday.month, employees[i].bday.day, employees[i].bday.year,
         employees[i].age, employees[i].height, employees[i].name);

You are trying to print employees array, which is never initialized.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249143

Run your program under valgrind. It's a free tool which detects memory errors in code automatically. It will likely be able to highlight for you exactly where your code is reading or writing something invalid.

All you have to do is run your program with valgrind in front of it, assuming you're on a Linux system and have installed the valgrind package.

Upvotes: 1

Related Questions