Results 1 to 5 of 5

Thread: char array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Massachusetts, USA
    Posts
    111

    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;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Arrays are not pass-by-result, therefore your header should be

    void getLetterGrade(float average, char letterGrade[])

    That should help a bit.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Massachusetts, USA
    Posts
    111
    Thank you.

  5. #5
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    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.
    VS.NET 2003

    Need to email me?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width