0

I am still new to C programming and need to figure out why when I enter the "c" choice the program isn't printing out the grades entered in the program. I am not seeing what I am missing, can someone let me know if they see what I am missing please?

#include <stdio.h>
#include <stdlib.h>


int main()
{
    //Add all of the variables and the array for the grades I want to enter.
    char choice;
    int gradeScore = 0;//percentage
    //int gradeArray[100];//percentArrray //Comment Out
    int gCount = 0,i;//count

    //Allocate dynamic memory point using gradeArray.
    int *gradeArray = (int*)malloc(sizeof(int));

    /*The for loop is set to enable the user to enter no more than 100 grades.  This is because the gradeArray variable
    limit is set to 100.  This will then loop through until the user has entered up to 100 grades to ensure there
    is no buffering issue.*/

    for (gCount = 0; gCount < 100;)

        {
        /*This prompts the user for a choice that enables them to either enter another grade or exit the program and
        print the grades.  It also reads the choice entered by the user.*/
        printf("******************Enter Choice Selection in Parenthesis******************");
        printf("\n\n To add grades, enter choice (a)");
        printf("\n When finished entering grades, enter choice (c) \n \nEnter Choice: ");
        scanf(" %c", &choice);  //space is entered to ensure the compiler does not read whitespaces

        /* Then I use an if with the condition set to a valid choice of 'a'.  Then I prompt the user
        to enter a grade, read it and move on to the next if statement.*/
        if(choice == 'a')
        {
            printf("\nEnter grade: ");
            scanf(" %d", &gradeScore);

            /*If the grade entered does meet the if condition statement below it is added to the gCount
            of grades entered. This will allow all of the grades entered to be printed with the exit condition.*/
            if(gradeScore <= 100 && gradeScore >= 0)
            {
                gradeArray = realloc(gradeArray, sizeof(int) * gCount);
            }

        }


        //The last if statement prints out each grade on a new line when the user choice is c.
        if(choice == 'c')
        {
            break;
        }
    }
    printf("Grades are:\n");
    for(i = 0; i < gCount ; i++)
    {
        printf("  %d\%%\n", gradeArray[i]);

    }
    free(gradeArray);
    return 0;
}

Thank you, Annette

  • 1
    You're making an array with *one* `int` in it? Why not start with some reasonable default and resize by ~1.5x after you hit that limit? – tadman Jul 06 '20 at 21:25
  • 1
    When gcount == 0, `realloc(gradeArray, sizeof(int) * gCount)` is a concern. This looks like an off by one error. – William Pursell Jul 06 '20 at 21:34
  • Can you please provide your exact input and result? – kaylum Jul 06 '20 at 21:35
  • Note that `printf(" %d\%%\n", gradeArray[i]);` should be `printf(" %d%%\n", gradeArray[i]);` You escape the `%` format spec by putting two `"%%"`. – Weather Vane Jul 06 '20 at 21:36
  • ******************Enter Choice Selection in Parenthesis****************** To add grades, enter choice (a) When finished entering grades, enter choice (c) Enter Choice: a Enter grade: 34 ******************Enter Choice Selection in Parenthesis****************** To add grades, enter choice (a) When finished entering grades, enter choice (c) Enter Choice: c Grades are: Process returned 0 (0x0) execution time : 4.766 s Press any key to continue. – Annette Kitz Jul 06 '20 at 21:44
  • There is no code anywhere in here that ever modified `gCount`. It stays zero under all conditions. – David Schwartz Jul 06 '20 at 21:46
  • My last comment shows the results and it allows me to enter choice a and then enter a grade as it should. Then when I enter choice c to exit, it should print the grade entered, but it is blank. – Annette Kitz Jul 06 '20 at 21:46
  • The next comment has the code I updated, but the output is still wrong. It is now printing the first grade entered, but then it isn't prompting me to enter another choice or grade until I am done. Then the output is the first grade entered and a 0. – Annette Kitz Jul 06 '20 at 22:29
  • if(choice == 'a') { printf("\nEnter grade: "); scanf(" %d", &gradeScore); //Allocate dynamic memory point using gradeArray. int *g = (int*)malloc(gCount*sizeof(int)); for(i = 0; i < gCount ; i++) *(g+i)=*(gradeArray+i); g[gCount-1]=gradeScore; gradeArray=g; gCount++; } //The last if statement prints out each grade on a new line when the user choice is c. if(choice == 'c') { break; } – Annette Kitz Jul 06 '20 at 22:29
  • I got it, I had missed gCount - 1 in the for statements. Once I added that I am now getting the expected output with the modified code above. Thank you for you assistance. – Annette Kitz Jul 06 '20 at 22:37

2 Answers2

1

You are using wrong variable inside for loop according to your program. You are re-initialising gCount to 0 in for loop and not incrementing it. Later you are using same gCount to print grades. But since its value is 0 so no grades are printed.

atul_pant
  • 99
  • 8
  • So, I wondered if the for loop was the issue and I wanted to comment out that line of code, but I wasn't sure how to go about it. Can you help me with that if that is the right direction please? – Annette Kitz Jul 06 '20 at 21:34
  • What do you want to achieve? Can you please elaborate? – atul_pant Jul 06 '20 at 21:39
  • I updated the code, but I am trying to allow the user to enter a choice "a" which will then prompt the user to enter a grade. Once the user enters the choice of "c" it should break and print out the grades entered with a percent sign. I updated my code, but now it isn't continuing to promt me to enter grades and instead it just prints the first grade i entered and another grade 0. – Annette Kitz Jul 06 '20 at 22:26
  • if(choice == 'a') { printf("\nEnter grade: "); scanf(" %d", &gradeScore); //Allocate dynamic memory point using gradeArray. int *g = (int*)malloc(gCount*sizeof(int)); for(i = 0; i < gCount ; i++) *(g+i)=*(gradeArray+i); g[gCount-1]=gradeScore; gradeArray=g; gCount++; } //The last if statement prints out each grade on a new line when the user choice is c. if(choice == 'c') { break; } } – Annette Kitz Jul 06 '20 at 22:27
  • I got it, I had missed gCount - 1 in the for statements. Once I added that I am now getting the expected output with the modified code above. Thank you for you assistance. – Annette Kitz Jul 06 '20 at 22:37
0

Problem is that you never increased the value of a gCount variable, and for() loop is wrong too, I would recommend using while() here, also you never added gradeScore to gradeArray. You could write something like this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    //Add all of the variables and the array for the grades I want to enter.
    char choice;
    int gradeScore = 0;//percentage
    //int gradeArray[100];//percentArrray //Comment Out
    int gCount = 0,i;//count
 
    //Allocate dynamic memory point using gradeArray.
    int arr_size = 20;
    int *gradeArray = malloc(sizeof(int)*arr_size);

    /*The for loop is set to enable the user to enter no more than 100 grades.  This is because the gradeArray variable
    limit is set to 100.  This will then loop through until the user has entered up to 100 grades to ensure there
    is no buffering issue.*/
    
    while(gCount < 100)
    {
        /*This prompts the user for a choice that enables them to either enter another grade or exit the program and
        print the grades.  It also reads the choice entered by the user.*/
        printf("******************Enter Choice Selection in Parenthesis******************");
        printf("\n\n To add grades, enter choice (a)");
        printf("\n When finished entering grades, enter choice (c) \n \nEnter Choice: ");
        scanf(" %c", &choice);  //space is entered to ensure the compiler does not read whitespaces

        /* Then I use an if with the condition set to a valid choice of 'a'.  Then I prompt the user
        to enter a grade, read it and move on to the next if statement.*/
        if(choice == 'a')
        {
            printf("\nEnter grade: ");
            scanf(" %d", &gradeArray[gCount]);
            
            /*If the grade entered does meet the if condition statement below it is added to the gCount
            of grades entered. This will allow all of the grades entered to be printed with the exit condition.*/
            if((gCount+1) == arr_size)
            {
                arr_size += 20;
                gradeArray = realloc(gradeArray, sizeof(int) * arr_size);
            }
            
            gCount++;
        }


        //The last if statement prints out each grade on a new line when the user choice is c.
        if(choice == 'c')
        {
            break;
        }
    }
    
    printf("Grades are:\n");
    for(i = 0; i < gCount ; i++)
    {
        printf("  %d\%%\n", gradeArray[i]);

    }
    
    free(gradeArray);
    return 0;
}

Also, I want to point out that you shouldn't allocate array with just one element and constantly reallocate it. It is a bad practice, time-consuming, and can lead to some bigger problems later.

I recommend you to check this Do I cast the result of malloc? for malloc casting.

Majkl
  • 153
  • 1
  • 7