ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm working on some practice/suggested homework for my C programming class and am attempting to write a program which will read in an array of grades and then pass the array to a function which will calculate the GPA based on the input.
I'm having difficulties with reading the characters into the array however and I'd like some input. My code is compiling without error however I'm getting a "Segmentation Fault: 11" at runtime.
My code is below, thanks in advance:
#include <stdio.h>
//float compute_GPA(char grades[], int n);
int main(void)
{
int n, i=0;
char grades[n];
printf("Enter a series of grades A,B,C,D,F, separated by spaces: ");
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152
Rep:
n will be unset on entering main, ie some ranfdom number, only global variables are guarenteed to be 'zeroed', so you shoud set n before defining grades, but you would be better of using a define eg
Code:
#define MAXGRADES 10
char grades[MAXGRADES];
...
you should also check i against MAXGRADES to see if you have reached the maximum allowable entrys
You also could try to use an IDE to help you to debug your source code, like Netbeans.
With an IDE you will be able to run your code step by step and check the value of your variables
n will be unset on entering main, ie some ranfdom number, only global variables are guarenteed to be 'zeroed', so you shoud set n before defining grades, but you would be better of using a define eg
Code:
#define MAXGRADES 10
char grades[MAXGRADES];
...
you should also check i against MAXGRADES to see if you have reached the maximum allowable entrys
Thanks this helped getting my program to compile successfully, however I'm still having now I'm having difficulties with actually reading the user input to the array. The way I have programmed the "do while" loop and "scanf" function makes sense to me however if I insert a "printf" into the loop, my code is not actually reading the variables to the array (the print F is printing my initialized values").
Also I'm coding with X-code on OSX so I am using an IDE.
Thanks for the the help!
int main(void)
{
int n=0, i=0;
char grades[MAXGRADE] = {'C','C','C','C','C','C','C','C','C','C'};
printf("Enter a series of grades A,B,C,D,F, separated by spaces: ");
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152
Rep:
you are scaning into the array then incrementing the counter and the printing the array indexed by the counter which will not be set as you have just incremented it put the i++ after the first printf
Be cautious about exceeding the size of that list. If the entering person never hits return and just keeps entering grades beyond the size of the array limitations, you'll likely get another error or some undefined behavior because you'll be pointing beyond the size of your table. The while test can include "&& (index < MAXGRADE)"
As Keith Hedger mentioned, you're reading into i, incrementing i, and then printing grades[i] and checking if grades[i] != '\n'. Because you're incrementing i between the read and the write/test, your write/test will never return anything other than fill values.
Step through it in your head:
i=0
grades[i] is grades[0] which is the fill value
scanf("c", &grades[i]) loads the first character into grades[i], so now grades[i], which is grades[0], is 'A' or whatever was entered
i++, so now i=1
printf("c", grades[i]), well i is now 1, so you're printing grades[1], which is still a fill value
while(grades[i] != '\n'), again i is 1, grades[1] is still the fill value
And so on through your loop. You need to increment the index AFTER you're done with it. After the read, after the print, after the test, AND you should verify i < MAXGRADE before you start referencing grades[i], or you're bound to hit a seg fault.
I HIGHLY suggest turning on all warnings and bounds checking in your compiler, you'll catch a lot of problems like this.
Last edited by suicidaleggroll; 10-26-2015 at 05:46 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.