Disclaimer - my solutions below are done to help you get the immediate issues with your code to a point to where it will work and you can continue your learning path. Please keep in mind that this code will NOT suffice in the real world, but since you are still in 'learning land' it will work for your needs, for now. Please read through comments made by users and take them into consideration as well. In addition, you are using library functions that have LOTS of caveats and pitfalls. Once you understand the immediate mistakes of your current code, do some SO searches for these functions (fgets(), strncmp(), atoi(), and malloc()) to find more information on correctly using them.
Your code has multiple syntax errors and logical/design issues.
First lesson to learning 'C': The compiler is your friend.
The code you originally posted won't compile. Let's see what our friend, the compiler, has to say...
error: missing terminating " character
18 | printf("You have a maximum of 14 grades that can be entered. If less, then enter 'D' for
error: missing terminating " character
19 | "Done".");
Problems:
- Do you see how it broke this into two error messages? That means you have something wrong with your string.
- You need to escape the double quotes around Done.
Solutions:
- Remove the carriage return after 'for' or break it up into multiple strings like:
printf("string one "
"string two");
- Escape the double quotes -> \"Done\"
Okay, let's see what the compiler says now...
warning: implicit declaration of function ‘strncmp’ [-Wimplicit-function-declaration]
22 | if(strncmp(input,'D',1)== 0)
| ^~~~~~~
Problem: This is telling us that we are using a function (strncmp) that we haven't declared yet.
Solution: Include the string header file at the top -> #Include <string.h>
Let's see what happens now...
warning: passing argument 2 of ‘strncmp’ makes pointer from integer without a cast [-Wint-conversion]
23 | if(strncmp(input,'D',1)== 0)
| ^~~
| |
| int
Problem: Here is the strncmp function signature:
int strncmp(const char *str1, const char *str2, size_t n)
The compiler is telling you that your second argument is an (int), but the function requires a pointer to a char.
Solution: Declare a char so you can pass a pointer to it
char done = 'D';
Now call strncmp like:
if(strncmp(input,&done,1)== 0)
Now, let's see what happens...
error: invalid operands to binary * (have ‘float *’ and ‘long unsigned int’)
36 | gradeNums = (int*) malloc (gradeBook * sizeof(int));
| ~~~~~~~~~ ^
| |
| float *
Problem: gradebook is an array of type float. You are multiplying a pointer to float times an int (size_t) here. You can't multiply pointers in C. Even if gradeBook was a float, multiplying a float and an int would yield a float. malloc expects an int (size_t).
Solution: change gradeBook to totalEntered.
Now, we should be able to compile and run the program.
It is wrong and won't output what you are expecting
Here is what we get...
You have a maximum of 14 grades that can be entered. If less, then enter 'D' for "Done".
Please enter grade ->
11
You have a maximum of 14 grades that can be entered. If less, then enter 'D' for "Done".
Please enter grade ->
22
You have a maximum of 14 grades that can be entered. If less, then enter 'D' for "Done".
Please enter grade ->
33
You have a maximum of 14 grades that can be entered. If less, then enter 'D' for "Done".
Please enter grade ->
D
You have entered 4 grades
The grades you have entered are:
0.00
The final for loop should be changed from:
printf("You have entered %d grades \n", gradeNum + 1);
printf("The grades you have entered are: \n");
for (j=0; j <= i; j++)
printf("%.2f ", gradeBook[j]);
printf("\n \n");
to something like the following:
printf("You have entered %d grades \n", totalEntered);
printf("The grades you have entered are: \n");
for (j=0; j <= totalEntered; j++) {
printf("%.2f ", gradeBook[j]);
printf("\n \n");
}
This still has a problem though. gradeBook is never updated anywhere so it will always ouput '0.00'. I am not sure how you want to store or present the data so I took the liberty of making a few modifications to your initial code below. I kept things close in spirit to your original code to provide you with a stepping stone to get through this set of problems.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int i = 0;
int intArray [15];
int gradeNum;
int gradeEntered;
int totalEntered = 0;
char ans;
char input [15];
float * gradeBook;
char done = 'D';
for(gradeNum = 0; gradeNum < 15; gradeNum++) {
printf("You have a maximum of 14 grades that can be entered. If less, then enter 'D' for \"Done\".");
printf("\nPlease enter grade -> \n");
fgets(input, 15, stdin);
if(strncmp(input,&done,1)== 0) {
break;
}
gradeEntered = atoi(input);
intArray[gradeNum]= gradeEntered;
totalEntered++;
}
gradeBook = malloc (totalEntered * sizeof(float));
for (i = 0; i < totalEntered; i++) {
gradeBook[i] = (float) intArray[i] / 10.0;
}
printf("You have entered %d grades \n", totalEntered);
printf("The grades you have entered are: \n");
for (i =0; i < totalEntered; i++) {
printf("%.2f ", gradeBook[i]);
printf("\n \n");
}
free(gradeBook);
return 0;
}
Here is some output when it is run...
You have a maximum of 14 grades that can be entered. If less, then enter 'D' for "Done".
Please enter grade ->
901
You have a maximum of 14 grades that can be entered. If less, then enter 'D' for "Done".
Please enter grade ->
874
You have a maximum of 14 grades that can be entered. If less, then enter 'D' for "Done".
Please enter grade ->
773
You have a maximum of 14 grades that can be entered. If less, then enter 'D' for "Done".
Please enter grade ->
D
You have entered 3 grades
The grades you have entered are:
90.10
87.40
77.30
If there is something you don't understand or want different, please let us know. Good Luck!