简体   繁体   中英

C Program Convert float number to letter grade

Hi I am trying to write a program that asks the user for a numeric grade and then converts this to a letter grade basically from a float to a string, I kind of have this working apart from when the grade is say B+ it only returns +. I know this is because I am returning char which is only one character. I have tried changing char letterGrade to char letterGrade[3]; but this gives me the error: incompatible types when assigning to type char[3] from type int. Appreciate if someone could tell me where I am going wrong as I am very new to this.

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

 struct module {
  char moduleid[10];
  int credit;
  float grade;
 };
  //Convert numeric grade to letter grade
  char getGrade (float input){
    char letterGrade[3];
    if(input >= 80&&input<=100)
        letterGrade = 'A';

    else if(input >= 70&&input<=79)
        letterGrade = 'B+';

    else if(input >= 60&&input<=69)
        letterGrade = 'B';

    else if(input >= 55&&input<=59)
        letterGrade = 'B-';

    else if(input >= 50&&input<=54)
        letterGrade = 'c+';

    else if(input >= 40&&input<=49)
        letterGrade = 'c';

    else if(input >= 35&&input<=39)
        letterGrade = 'D';

    else
        letterGrade = 'F';

   return letterGrade;
   }

  int main( ) {
      struct module mod1,mod2;
      printf("Please enter: module id, module credit and module grade\n");
      scanf("%s%d%f",mod1.moduleid,&mod1.credit,&mod1.grade);
      scanf("%s%d%f",mod2.moduleid,&mod2.credit,&mod2.grade);

      printf( "Module id\tCredit\t\tGrade\n");
      printf("%s\t\t%d\t\t%f\t%s\n",mod1.moduleid,mod1.credit,mod1.grade,getGrade(mod1.grade));
      printf( "%s\t\t%d\t\t%f\t%s\n",mod2.moduleid,mod2.credit,mod2.grade,getGrade(mod2.grade));

      return 0;
   }

This is because arrays in C are not assignable. You need to use strcpy() or strncpy() instead.

You can also change your getGrade() function to the following:

//Convert numeric grade to letter grade
char* getGrade (float input){
    if(input >= 80 && input<=100)
        return "A";
    else if(input >= 70 && input<=79)
        return "B+";
    else if(input >= 60 && input<=69)
        return "B";

    else if(input >= 55 && input<=59)
        return "B-";

    else if(input >= 50 && input<=54)
        return "c+";

    else if(input >= 40 && input<=49)
        return "c";

    else if(input >= 35 && input<=39)
        return "D";
    else
        return "F";
}

or use static char letterGrade[3] and return char* instead of char .

You could also change the way user inputs data to:

scanf("%9s%d%f",mod1.moduleid,&mod1.credit,&mod1.grade);
scanf("%9s%d%f",mod2.moduleid,&mod2.credit,&mod2.grade);

This way you'll know get exactly as many characters as you can (no more than 9) for your moduleid field.

char *getGrade (float input){//return type is char *
    static char letterGrade[3];//can't use return of auto array
    letterGrade[0] = letterGrade[1] = 0;
    if(input >= 80)
        *letterGrade = 'A';
    else if(input >= 70){
        *letterGrade = 'B';
        letterGrade[1] ='+';
    } else if(input >= 60)
        *letterGrade = 'B';
    else if(input >= 55) {
        letterGrade[0] = 'B';
        letterGrade[1] = '-';
    } else if(input >= 50) {
        letterGrade[0] = 'C';
        letterGrade[1] = '+';
    } else if(input >= 40)
        *letterGrade = 'C';
    else if(input >= 35)
        *letterGrade = 'D';
    else
        *letterGrade = 'F';

   return letterGrade;
}
char letterGrade[3];

You can't do a assignment to a character array after it is declared.

letterGrade = "A+"; /* Which is wrong */

You need to do a strcpy();

And also you are assigning a character to a string which is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM