Nuri Nuri - 1 month ago 8
C Question

Retrieving array values in C Programming. How can I fix these errors?

this is my first post, so I am sorry if I didnt do this correctly. But, I am fairly new to C Programming and I have hit a roadblock. We were given an .exe file to build a code from scratch. I have gotten this far but I am having trouble getting past retrieving the names. It is storing them, or so it seems, but when set my value to 3, it takes from all three names for the first two, and only displays the third name correctly. It is always the last name that is correct. I provided a screenshot below showing my issues. I'm pretty close to being done, but cant get past this step. Any help would be really appreciated! Thank you
1: https://i.stack.imgur.com/JYuVf.jpg

#include <stdio.h>
#include <string.h>


int a_delete;
char b[100];
int name;
int counter=0;
int F;
int d;
int grade;
int set;
int w;
int x;
int z;
char a[100];
char names[100];
char g[100];
char h[100];
char i[100];
char c[100];
char f[100];
char phy[100];
char Counter[100];[enter image description here][1]
char final_grade[100];
double class_grades[100];
double total;
double totals;

int main()
{



printf("\n Welcome to the grade calculator");
printf("\n Enter the total number of students in the class:");
fgets(a, sizeof(a), stdin);
sscanf(a, "%d", &name);
char my_data[name*3];
char my_array[name];

if(name<1)
{
printf("\n Not a valid number");
printf("\n Please enter a valid integer number only");
}
else
{
for(x=0;x<name;++x)
{
printf("\n Please enter the students name:");
fgets(f, sizeof(f), stdin);
sscanf(f, "%s", &my_array[x]);

}
if(x<=name)
{
for(x=0 ; x<name;++x)
{

printf("\n Please enter %s's Physics score (out of 100 points) : ", &my_array[x]);
fgets(b, sizeof(b), stdin);
sscanf(b, "%d", &my_data[set]);

}

for(x=0;x<name;++x)
{

printf("\n Please enter %s's Chemestry score (out of 100 points) : ", &my_array[x]);
fgets(c, sizeof(c), stdin);
sscanf(c, "%d", &my_data[set]);

}
for(x=0; x<name;++x)
{


printf("\n Please enter %s's Math score (out of 100 points) : ", &my_array[x]);
fgets(g, sizeof(g), stdin);
sscanf(g, "%c", &my_data[set]);

}
if(x=3)
{
for(x=0; x<=name;++x)
{
puts("\n*****************");
puts("\n***Report card***");
puts("\n*****************");

printf("Name of the student: %c", my_array[name]);

printf("\nPhysics: %.2f/100", my_data[set]);

printf("\nChemistry: %.2f/100", my_data[set] );

printf("\nMath: %.2f/100", my_data[set] );


if(grade>=270)
{

printf("\nYour letter grade: A");

}
else if(my_data[set]<270>=240)
{
printf("\nYour letter grade: B");
}
else if(my_data[set]<240>=210)
{
printf("\nYour letter grade: C");
}
else if(my_data[set]<210>=180)
{
printf("\nYour letter grade: D");
}
else if(my_data[set]<180)
{
printf("\nYour letter grade: F");
}

}
}
} }

return(0);


}

Answer

You are storing the names in the same variable, overwriting the last each time you enter one. You seem to have a misunderstanding of char arrays, I'll break down the problem code.

for(x=0;x<name;++x)
{
    printf("\n Please enter the students name:");
    fgets(f, sizeof(f), stdin);
    sscanf(f, "%s", &my_array[x]);
}
  1. The first line says "starting at 0, add 1 to x until x is not less than name". x is the variable you are using to move through the array, and you are moving 1 char at a time.
  2. Skipping to fgets(), you are reading in a string from the user, and storing it in f.
  3. finally, sscanf() copies the string into &my_array[x]. This is the problem portion of the code. Here you are copying the data that the user just entered into my_array, starting at position x.

So for your example input, here is what it looks like:

  1. my_array == { 's', 'a', 'm', '\0' } when x == 0
  2. my_array == { 's', 'd', 'e', 'a', 'n', '\0' } when x == 1
  3. my_array == { 's', 'd', 'b', 'o', 'b', 'b', 'y', '\0' } when x == 2

Notice that it shifts one character at a time to the right, this is x being added to each loop. A char holds only a single character, it is not the same thing as a string of characters, or a char array. What you are probably trying to do is something like this:

for(x=0;x<name;++x)
{
    printf("\n Please enter the students name:");
    fgets(f, sizeof(f), stdin);
    sscanf(f, "%s", &my_array[x*100]);
}

Also, when you define my_array, and a few of your other variables, your sizes are pretty obscure. Note that when you define my_array and enter 3, you are only giving it 3 characters worth of space to write to. Anything after is undefined behavior, but basically it will start putting data in places that you may not want or expect. You should also initialize this array like:

char my_array[100*name];

Hopefully this answer cleared up some things for you. I recommend you do some research on primitive data types and arrays in c. Another possible solution to your problem is to implement a 2D array.

Comments