need help using functions and char

I have been spending a couple days working on a final assignment for my intro class

The assignment is :

Write a function that takes one double parameter, and returns a char. The parameter represents a grade, and the char represents the corresponding letter grade. If you pass in 90, the char returned will be ‘A’. If you pass in 58.67, the char returned will be an ‘F’ etc. Use the grading scheme on the syllabus for this course to decide what letter to return.


This is what I have so far. At this point i'm not sure if i'm even going in the right direction. Otherwise i feel i'm missing something
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>



char findGrade(double mark)	{
	
	
	if (mark >= 90)
		return 'A';
	if (mark >= 80)
		return 'B';
	if (mark >= 70)
		return 'C';
	if (mark >= 60)
		return 'D';
	if (mark >= 50)
		return 'F';

}

main()	{
	double mark = 0.0;

	printf("Enter the grade marks : \n", findGrade(mark));
	scanf_s("%f", &mark);
	printf("\nYour grade is:  \n", findGrade(mark));


	system("pause");
}
You should remove line 17, otherwise the function does not return a value for grades less than 50.

Also, you're missing the format specifier for characters on line 27, so you can't even see what the function returns.
Last edited on
so I did what you said but now every value returns F

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>



char findGrade(double mark)	{
	
	
	if (mark >= 90)
		return 'A';
	if (mark >= 80)
		return 'B';
	if (mark >= 70)
		return 'C';
	if (mark >= 60)
		return 'D';
	 
		return 'F';

}

main()	{
	double mark = 0.0;

	printf("Enter the grade marks : \n", findGrade(mark));
	scanf_s("%f", &mark);
	printf("\nYour grade is: %c \n", findGrade(mark));


	system("pause");
}
Surprise. You fall into obscure scanf pit called type mismatch. You are trying to write float value into double.

To prove:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>

char findGrade(double mark)	
{
	if (mark >= 90)
		return 'A';
	if (mark >= 80)
		return 'B';
	if (mark >= 70)
		return 'C';
	if (mark >= 60)
		return 'D';
	return 'F';
}

int main()	{
	double mark = 0.0;

	printf("Enter the grade marks : \n", findGrade(mark));
	scanf("%f", &mark);
	printf("mark: %lf", mark);
	printf("\nYour grade is: %c \n", findGrade(mark));
}
Enter the grade marks :
87
mark: 0.000000
Your grade is: F
Use %lf format tag instead. %f is for floats, not doubles.
Last edited on
wow I can't believe I missed the l in %lf.
Turn on compiler warnings:
\projects\Test\main.cpp||In function 'int main()':|
\projects\Test\main.cpp|20|warning: too many arguments for format [-Wformat-extra-args]|
\projects\Test\main.cpp|21|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double*' [-Wformat=]|
||=== Build finished: 0 errors, 2 warnings (0 minutes, 0 seconds) ===|
thank you now it works just fine
Topic archived. No new replies allowed.