user3267522
user3267522

Reputation: 11

Why is my program outputting memory address?

The goal of this program is to use functions to create a table of inputted student IDs and their test grades. The test grades are then converted into letter grades. My issue the program is outputting memory instead of the inputted numbers. I know I'm making a mistake somewhere but I can't seem to figure it out. Any help is greatly appreciated.

#include <stdio.h>

#define NUM 12

void obtain_id_scores (int [], int []);
void get_letter_grade (char [], int []);
void display_results (int [], int [], char []);

int main (void)
{
    int student_id [50];
    int test_score [50];
    char letter_grade [50];

    obtain_id_scores (student_id, test_score);
    get_letter_grade (letter_grade, test_score);
    display_results (student_id, test_score, letter_grade);

    return 0;
} /* End Main. */


void obtain_id_scores (int student_id [], int test_score [])
{
    int x;

    for (x = 1; x <= NUM; ++x)
    {
        printf (" Enter student ID and test grade: ");
        scanf ("%s %s", &student_id [x], &test_score [x]);
        fflush (stdin);
    }   
}


void get_letter_grade (char letter_grade [], int test_score [])
{
    int x;

    for (x = 1; x <= NUM; ++x)
    {
        if (test_score [x] > 100)
        letter_grade [x] = 'N';
        else if (test_score [x] >= 90)
        letter_grade [x] = 'A';
        else if (test_score [x] >= 80)
        letter_grade [x] = 'B';
        else if (test_score [x] >= 70)
        letter_grade [x] = 'C';
        else if (test_score [x] >= 60)
        letter_grade [x] = 'D';
        else
        letter_grade [x] = 'F';
    }
}

void display_results (int student_id [], int test_score [], char letter_grade [])
{
    int x;

    /* Display Results */

    printf ("           Student Grade Report\n");
    printf ("          ---------------------\n\n");
    printf ("   ID        TEST SCORE   LETTER GRADE\n");

    /* Display all IDs, test scores, and letter grades. */

    for (x = 1; x < NUM; ++x)
    {
        printf ("%6i       %6i     %8c\n", student_id[x], test_score[x], letter_grade[x]);      
        }
}

Upvotes: 0

Views: 55

Answers (1)

Jongware
Jongware

Reputation: 22457

scanf ("%s %s" .. stores the entered text as strings, and you are storing them into ints.

Use

scanf ("%d %d" ...

to accept the input as numbers instead.

Upvotes: 1

Related Questions