|
-
Feb 19th, 2003, 03:20 PM
#1
Thread Starter
Lively Member
char array
Hello everyone. I am writing a simple function that returns a char array. Here is the code that I have so far. Can anyone help me fix the errors I am having working with the char array? Thanks.
char[] getLetterGrade(float average)
{
char[] letterGrade;
//this code checks the average passed to it and returns the proper grade
if(average>=93) letterGrade='A';
else
if(average>=87) letterGrade='AB';
else
if(average>=83) letterGrade='B';
else
if(average>=77) letterGrade='CB';
else
if(average>=73) letterGrade='C';
else
if(average>=67) letterGrade='CD';
else
if(average>=60) letterGrade='D';
else
if(average<60) letterGrade='F';
return letterGrade;
}
-
Feb 19th, 2003, 04:16 PM
#2
You have a lot of newbie string errors in there. For passing arrays as well as for using strings.
I suggest you read up on how strings work in C.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 1st, 2003, 12:21 AM
#3
Fanatic Member
Arrays are not pass-by-result, therefore your header should be
void getLetterGrade(float average, char letterGrade[])
That should help a bit.
-
Mar 1st, 2003, 04:08 AM
#4
Thread Starter
Lively Member
-
Mar 1st, 2003, 04:57 AM
#5
Hyperactive Member
You also cannot return an array allocated within a function. This is very unsafe. It should be ever allocated dynamically inside a function or copied to an outside target.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|