Jamie Keeling
Jamie Keeling

Reputation: 9966

Compiler error using "if" statements in C

I am trying to write a very simple application that allows me to enter a number which will allocate a particular grade.

I've not used the C language very much as i primarily use C# however i still don't seem to be able to get around the errors:

They are all syntax errors, ranging from "if" to "{" although i'm sure everything is as it should be.

One i don't understand is the "void illegal with all types" at the grade = assess(mark); section.

I understand the program may not product the correct output but im simply trying to get it to compile.

Thank you for your help, i imagine I'm doing something REALLY obvious.

Task.c

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

//Protoype
void assess(int* mrk);

// Main method (start point of program)
void main()
{

 int mark;
 char grade;


 printf("enter a word: ");
 scanf("%d", &mark);

 grade = assess(mark);



 printf("That equals ");
 printf("%c", grade);
 printf(" when marked\n");
}


char assess(int* mrk)
{
 char result;

 if(mrk > 0 && <= 100)
 {
  if(mrk < 35)
  {
   result = "f";
  }
  if(mrk >= 35 && <= 39)
  {
   result = "e";
  }
  if(mrk >= 40 && <= 49)
  {
   result = "d";
  }
  if(mrk >= 50 && <= 59)
  {
   result = "c";
  }
  if(mrk >= 60 && <= 69)
  {
   result = "b";
  }
  if(mrk > 70)
  {
   result = "a";
  }
 }
 else
 {
  result = "error";
 }

 return result;
}

Upvotes: 1

Views: 319

Answers (3)

t0mm13b
t0mm13b

Reputation: 34592

Have a look at the fixed version:

  • Prototype assess was using a pointer to int, but it was not needed as you were calling the function with a parameter as being pass-by-value, not pass-by-reference.
  • The function was of the wrong return type, in your prototype you had void yet coded the function to return a char, that explains the error message your compiler notified about.
  • In the function assess, incorrect usage of the quotes for the grades, a single quote is a character, a double quote is a string (type would be char string[] or char *ptrStr), and the function returned a string type which collided with the char return type as per the function signature.
  • Lastly but not least, you returned a "error", again a string type, I made that to be 'n' to represent 'not good'
#include <stdio.h>
#include <string.h>

//Protoype
char assess(int mrk);

// Main method (start point of program)
void main()
{

    int mark;
    char grade;


    printf("enter a word: ");
    scanf("%d", &mark);

    grade = assess(mark);



    printf("That equals ");
    printf("%c", grade);
    printf(" when marked\n");
}


char assess(int mrk)
{
    char result;

    if(mrk > 0 && <= 100)
    {
        if(mrk < 35)
        {
            result = 'f';
        }
        if(mrk >= 35 && <= 39)
        {
            result = 'e';
        }
        if(mrk >= 40 && <= 49)
        {
            result = 'd';
        }
        if(mrk >= 50 && <= 59)
        {
            result = 'c';
        }
        if(mrk >= 60 && <= 69)
        {
            result = 'b';
        }
        if(mrk > 70)
        {
            result = 'a';
        }
    }
    else
    {
        result = 'n';
    }

    return result;
}

Hope this helps, Best regards, Tom.

Upvotes: 1

jason
jason

Reputation: 241583

mrk is declared as a pointer to an int but you are not dereferencing it.

Replace

char assess(int* mrk) 

with

char assess(int mrk)

in the definition of assess

Similarly, you declared (prototyped) assess as

void assess(int* mrk)

Replace with

char assess(int mrk)

Next,

if(mrk >= 35 && <= 39)

is not legal syntax. I know it reads like mrk is greater than or equal to 35 and less than or equal to 39 but you have to be more explicit for the compiler. So

replace

if(mrk >= 35 && <= 39)

with

if(mrk >= 35 && mrk <= 39)

and similarly throughout.

Next, in assess you have declared result as a char but you are assigning char *s to result. Replace

result = "f";

with

result = 'f';

and similarly for all assignments to result. In particular

result = "error";

should be something like

result = 'z'; /* 'z' indicates failure */

Upvotes: 7

Chris H
Chris H

Reputation: 6581

Your function prototype is void and the implementation is char as a return type. Also you're passing a parameter as a pointer....you should not use a pointer to pass ints unless you want to change the int that was passed in. Passing pointers for ints isn't a speedup since you end up passing a pointer that is the same size as an int anyway.

Upvotes: 2

Related Questions