5 Replies - 634 Views - Last Post: 27 March 2012 - 10:26 AM Rate Topic: -----

#1 RyanRahl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-March 12

Question about data type error from a newbie

Posted 27 March 2012 - 09:40 AM

I started to teach myself C with the book Practical C Programming and I haven't had any trouble until Exercise 6-3, which seems to be much simpler than the other exercises I have completed. The book asked me to write a program that to generate letter grades from a numerical score so I wrote the following program:

/** program for displaying grade letters based on numerical user input **/

#include <stdio.h>

char c_score[4];                        /*User input score*/
int score;                              /*Score converted to integer*/
char grade[3];                          /*Letter grade*/

int main()
{
   /*Get score from user input*/
   printf("Enter numerical score to get grade letter: \n");
   fgets(c_score, sizeof(c_score), stdin);
   sscanf(c_score, "%d", &score);
   /*Calculate grade letter from score*/
   if(score <= 60)
      grade = "F ";
   if (61 <= score <= 63)
      grade = "D-";
   if (64 <= score <= 67)
      grade = "D ";
   if (68 <= score <= 70)
      grade = "D+";
   if (71 <= score <= 73)
      grade = "C-";
   if (74 <= score <= 77)
      grade = "C ";
   if (78 <= score <= 80)
      grade = "C+";
   if (81 <= score <= 83)
      grade = "B-";
   if (84 <= score <= 87)
      grade = "B ";
   if (88 <= score <= 90)
      grade = "B+";
   if (91 <= score <= 93)
      grade = "A-";
   if (94 <= score <= 97)
      grade = "A ";
   if (97 <= score <= 100)
      grade = "A+";
   if (score > 100)
      printf("Error: Score must be between 0 and 100");
   else
      printf("A score of %d is an %s grade", score, grade);
   return(0);
}



and the compiler gave the following set of errors:

ryan@the-ship:~/programming$ nano grades.c
ryan@the-ship:~/programming$ gcc grades.c -o grades64
grades.c: In function ‘main’:
grades.c:17:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:19:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:21:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:23:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:25:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:27:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:29:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:31:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:33:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:35:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:37:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:39:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’
grades.c:41:13: error: incompatible types when assigning to type ‘char[3]’ from type ‘char *’



I'm guessing that "incompatible types" means I am trying to assign the wrong data type to the variable. I think my real problem is my inexperience in interpreting the error messages I receive. Any clarification on this would be great, thanks. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Question about data type error from a newbie

#2 Evales  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-March 12

Re: Question about data type error from a newbie

Posted 27 March 2012 - 09:52 AM

I might be a missing something, but why are you scanning to char and then converting it to int?
Why not just ask for a score and scan to int.

For example instead of:
printf("Enter numerical score to get grade letter: \n");
fgets(c_score, sizeof(c_score), stdin);
sscanf(c_score, "%d", &score);


You remove fgets and sscan and replace them with:
scanf("%d", score);

scanf("%d", &score);

Sorry, I'm a super noob do not know how to edit my own posts.

Just use:

scanf("%d", &score);

?
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 653
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Question about data type error from a newbie

Posted 27 March 2012 - 09:54 AM

A char is a single character. You try to assign more than one character to the grade array. You could either set the array manually or use a function like strcpy to assign more than one character to grade.

This is what I mean by setting the array manually:
if(score <= 60)
{
    grade[0] = 'F';
    grade[1] = ' ';
    grade[2] = '\0';       // NULL terminating character
}



In my opinion, using the strcpy function would be easier:
if(score <= 60)
    strcpy(grade, "F ");    // strcpy inserts a NULL character for you


You just need to make sure that you don't copy anything that is too large to fit into grade, that is, anything that has 3 or more characters(NOT counting the NULL character).

*EDIT*: The reason for the NULL character is so that certain functions can know when they reach the end of the array(C-style string) without needing to know the array's size.

This post has been edited by vividexstance: 27 March 2012 - 09:56 AM

Was This Post Helpful? 1
  • +
  • -

#4 RyanRahl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-March 12

Re: Question about data type error from a newbie

Posted 27 March 2012 - 09:55 AM

Quote

I might be a missing something, but why are you scanning to char and then converting it to int?
Why not just ask for a score and scan to int.


The only answer I have for that is that the book I am going through taught me that way. I'm going to guess it taught me that way because when dealing with input from a user, it can't be assumed the user will always input a type of data that is compatible with what is in the program. So to make up for that, I start as a char because that could be anything from the keyboard and then safely convert it into something the program can use. I could be wrong though, I'm pretty new to this.
Was This Post Helpful? 0
  • +
  • -

#5 RyanRahl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-March 12

Re: Question about data type error from a newbie

Posted 27 March 2012 - 10:15 AM

Quote

In my opinion, using the strcpy function would be easier


Yes, that was perfect. I forgot about the strcpy, I remembered the need for the null character at the end and I defined space for that in the array when I declared the variable but overlooked the fact that character strings in the format I was using is not part of the standard library. I added "#include <string.h>" to the preproccessor and used "strcpy" and everything went perfectly. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 653
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: Question about data type error from a newbie

Posted 27 March 2012 - 10:26 AM

If I helped you, or anyone else helps you on here, then click the '+' button at the bottom-right of each post where it says "Was This Post Helpful?".

Quote

overlooked the fact that character strings in the format I was using is not part of the standard library.


Actually, string.h is part of the standard C library. Here is a good reference site for both C/C++: http://cplusplus.com/reference/. Even though it might seem like it's only for C++, you can use it for C just as well. To tell if the header is a C standard library header, just take the old name of the header (string.h), drop the ".h" part and add a 'c' to the front to get cstring as the header name.

This post has been edited by vividexstance: 27 March 2012 - 10:56 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1