简体   繁体   中英

Assign a value to a Character String..?

Here's my problem.. I have two arrays: one has the total marks of students and another is a character string. So, I want the computer to assign a letter grade to the character string when the array with the total marks is in within a range..

Here's what a came up with but it won't compile and I don't have anything else in mind:

尝试这个:

sprintf(grade[i], "%s", "A+");

What is the compilation error message?

I'll assume you've allocated resources properly. Even if you have and everything is compiling this line of code will cause an array bounds bug (as you've defined things):

grade[i][MAX_LETTER_SIZE]

should be:

sprintf(grade[i], "A+");

I really would send arrays as pointers. It is much cleaner and more efficient. You might also wish to create an LUT that maps scores to grades. This will reduce the amount of code you need to write. Otherwise you'll require a series of if statements rather than an inner loop with a single if statement. Good luck.

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