LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2015, 05:01 PM   #1
dsschanze
Member
 
Registered: Aug 2004
Location: Gainesville, FL
Distribution: Linux Mint 12, Win7, iOS
Posts: 208

Rep: Reputation: 33
Reading characters into an array in C


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: ");

do{
grades[i] = getchar();
i++;
}
while(grades[i] != '\n');

printf("%c", grades[1]);

return 0;


}
 
Old 10-23-2015, 05:38 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
    char grades[n];
What is the value of n?

Also, valgrind and gdb are very useful tools for debugging this kind of problems.
 
Old 10-23-2015, 06:02 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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
 
Old 10-23-2015, 06:22 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by Keith Hedger View Post
but you would be better of using a define
Or an enum:

Code:
enum { maxGrades = 10 };
 
Old 10-23-2015, 07:07 PM   #5
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Rep: Reputation: 18
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
 
Old 10-24-2015, 02:48 PM   #6
dsschanze
Member
 
Registered: Aug 2004
Location: Gainesville, FL
Distribution: Linux Mint 12, Win7, iOS
Posts: 208

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by Keith Hedger View Post
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: ");

do{
scanf("%c", &grades[i]);
i++;
printf("%c\n", grades[i]);
}
while(grades[i] != '\n');

printf("%c", grades[1]);

return 0;


}
 
Old 10-24-2015, 03:07 PM   #7
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
Quote:
Originally Posted by dsschanze View Post

Code:
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: ");
    
    do{
        scanf("%c", &grades[i]);
        i++;
        printf("%c\n", grades[i]);
    }
    while(grades[i] != '\n');
    
    printf("%c", grades[1]);
    
    return 0;
    
    
}
Code tags help a lot on these forums. Preserve white space, etc.

Why do you think the problem you are seeing is happening? List the possibilites as you understand them.
 
Old 10-24-2015, 04:59 PM   #8
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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
 
Old 10-26-2015, 02:38 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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)"
 
Old 10-26-2015, 05:44 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
Old 10-28-2015, 11:48 AM   #11
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
dsschanze - As you have not commented on this post for a few days I assume you have your solution if so can you mark this thread as 'SOLVED'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to array of characters grob115 Programming 13 11-15-2011 09:50 AM
scanf reading newline into char array while reading 1 char at a time austinium Programming 6 09-26-2010 11:27 PM
[SOLVED] Howto | an array of characters the_gripmaster Programming 6 02-19-2010 01:06 AM
splitting numbers and characters from an array of characters. trscookie Programming 6 11-14-2008 09:34 AM
mirror characters in an array flyordie Linux - Newbie 1 10-16-2008 04:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration