Member Avatar for thache

I've been trying to figure out whats wrong here, but I just can't wrap my head around it. My compiler outputs the following when I attempt to compile it.

z:\Desktop\tmp\proj4>cl proj4_linkedlist.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

proj4_linkedlist.c
proj4_linkedlist.c(40) : warning C4047: '=' : 'char *' differs in levels of indirection from 'int'
proj4_linkedlist.c(106) : warning C4047: 'initializing' : 'node *' differs in levels of indirection from 'int'
proj4_linkedlist.c(107) : error C2106: '=' : left operand must be l-value
proj4_linkedlist.c(108) : error C2106: '=' : left operand must be l-value
proj4_linkedlist.c(109) : error C2106: '=' : left operand must be l-value

The following code stores a students name, and exam scores in a struct which resides in a linked list. The problem area is where I add values to the linked list. I don't know what I need to do to fix it. Pointers, referencing and referencing variables kinda confuses me.

#include <stdio.h>
// prototypes
void add(char firstName[], char lastName[], int exam[]);// add a structure to the link list
void print(int exams);	// print all structures in the link list

void toUpper(char *str);
int getScore(int max);
double calcGrade(int points[],int len);



struct node {
     char firstName[40];
     char lastName[40];
     int exam[4];
     struct node *next;
};

// Create a global node structure pointer to our linked list
struct node *head;

int main()
{
    static int NUM_STU = 50;
    char buffer[80];
    char *newline;
    int i,j;
    char firstName[40];
    char lastName[40];
    int exam[4];
    int exam_len = (sizeof(exam)/sizeof(int))-1;
    char cont;
    head = NULL;
    for(i=0;i<NUM_STU;i++)
    {
        printf("\nPlease enter the name of student %d: ",i+1);
        fflush(stdin);
        if (fgets(buffer, sizeof(buffer), stdin) != NULL)
        {
            newline = strchr(buffer, '\n'); // find newline
            if ( newline != NULL )
                *newline = '\0'; // replace newline
        }
        toUpper(buffer);
        strcpy(firstName,strtok(buffer, " "));
        strcpy(lastName,strtok(NULL, " "));
        for(j=0; j<exam_len; j++)
        {
            printf("Please enter score for Exam %d: ",j+1);
            exam[j] = getScore(50);
        }
        printf("Please enter score for Final Exam: ");
        exam[exam_len+1] = getScore(100);
        add(firstName,lastName,exam);
        if(i < NUM_STU-1)
        {
            printf("Do you wish to enter another? (y/n): ");
            fflush(stdin);
            scanf("%c", &cont);
            toUpper(&cont);
            if(cont == 'N')
                break;
        }
    }
    printf("\n***Class Results***\n");
    print(exam_len+1);
    //displayResults(class,i);
    return 0;
}
int getScore(int max)
{
    int score;
    fflush(stdin);
    scanf("%d",&score);
    if(score < 0 || score > max)
    {
        printf("\tInvalid enter 0 - %d only...\n\tPlease re-enter score: ",max);
        score = getScore(max);
    }
    return score;
}

void toUpper(char *str)
{
    int i;
    int len = strlen(str);
    for(i = 0; i<len;i++)
    {
        if(str[i] >= 'a' && str[i] <= 'z')
            str[i] = str[i]-' ';
    }
}

double calcGrade(int points[],int len)
{
    int i;
    int total = 0;
    for(i = 0; i < len; i++)
    {
        total += points[i];
    }
    return total/250.0*100;
}

void add(char *firstName, char *lastName, int *exam) {
     struct node *newnode = malloc(sizeof(struct node));
//problems here :-/
     newnode->firstName = firstName;
     newnode->lastName = lastName;
     newnode->exam = exam;
     newnode ->next = NULL;
     if (head == NULL) {
          head = newnode;
     }
     else {
          struct node *cur = head;
          while (cur->next != NULL) {
              cur = cur->next;
          }
          cur->next = newnode;
     }
}


void print(int len) {
    struct node *cur = head;	// start structure at the start (head)
    char letter;
    double percent;
    while (cur != NULL) {
        double percent = calcGrade(cur->exam,len);
        if(percent < 60)
            letter = 'F';
        else if(percent < 70)
            letter = 'D';
        else if(percent < 80)
            letter = 'C';
        else if(percent < 90)
            letter = 'B';
        else
            letter = 'A';
    	// move through the stucture until the end (NULL)
        printf("%s, %s\tExam percentage: %%%2.1lf\tFinal Grade: %c\n",cur->lastName,cur->firstName,percent,letter);	// display the structure field 'data'
        cur = cur->next;				// move pointer to the next structure in the list
    } // end while
}

First off your missing two includes

#include <stdlib.h>
#include <string.h>

Also this function

void add(char *firstName, char *lastName, int *exam) {
     struct node *newnode = malloc(sizeof(struct node));

     newnode->firstName = firstName;//must use strcpy(newnode->firstName, firstname)
     newnode->lastName = lastName;//must use strcpy(newnode->lastName, lastName)
     newnode->exam = exam;//actually not sure what your doing here
     newnode ->next = NULL;
     if (head == NULL) {
          head = newnode;
     }
     else {
          struct node *cur = head;
          while (cur->next != NULL) {
              cur = cur->next;
          }
          cur->next = newnode;
     }
}
Member Avatar for thache
newnode->exam = exam;//actually not sure what your doing here

exam is an array of 4 ints
I'm trying to set the array in the linked list to the one filled by user input.

Edit: Instead of setting the array to the address of the other array should I be copy each element to the linked list one?

Member Avatar for thache
strcpy(newnode->firstName, firstName);
     strcpy(newnode->lastName, lastName);
     for(i=0;i<len;i++)
        newnode->exam[i] = exam[i];
     newnode ->next = NULL;

I did just that and I got it working thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.