PROBLEM REGARDING IF statements

I'm a first timer using if, else , or and and statements.cant make a problem 4 this one .
if grade is 90 output must be A
if grade is below 90 but higher than 80 output must be B
if grade is below 80 but higher than 70 output must be C
if grade is below 70 but higher than 60 output must be D
the hardest part for me is it must have void wich is letterGrade.

PLS..any help will be appreciated.
my program wont work.T_T
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
32
33
34
35
36
include<stdio.h>

void letterGrade(grade);
{
    if (grade=90){
        printf("A");
        return;
}
    if (grade<90||grade<80)
        {printf("B");
        return;
}
    if (grade<80||grade<70){
        printf("C");
        return;
}
    if (grade<70||grade<60){
    printf("D");
    return;
}
    if (grade<60){
    printf("F");
    return;
}


}
int main()
{
float grade;
printf("enter student grade");
scanf("%f",&grade);
letterGrade(grade);
 system("pause");
 return 0;
}

this is totally wrong. is there an easiest way to solve the problem?
first, you're making it much more complicated than it needs to be
second, all you need is #include <iostream>, and dont forget to include your namespace declaration
third, you dont need to call a function to do this
forth simplify your if else statements to a single (initial)if using only the lower delimiters.
theres a couple other things too but this will get you back on the right track

1
2
3
4

if (average  >=90) cout << "A";
else if (average  >=80) cout << "B";
else if (ave,,,,
Last edited on
Also you may look on switch statement, it make easier thn if.Something like this:
switch(mark);
case>=90:
cout<<"A"; and so on.My example is not well coz i type from mobile,but i think it make sense.
if grade is 90 output must be A (i suppose you mean 90 and above)
else (there should be an else here if you expect only one output)
if grade is below 90 but higher than 80 output must be B

1
2
3
4
5
if(grade>=90) {
    printf("A");
} else if(grade<90 && grade>80) {
    printf("B");
}//and so on... 
Last edited on
@Mazd
case>=90:
i dont think you can do that in c++.. but i believe its possible in VB
@Mazd, expressions won't work in switch cases. I just tried it.

Back on main Topic.
@freeze, that code won't work!

Firstly, you haven't specified the type for your argument 'grade'. I see that you are using float so, make it something like this void letterGrade(float grade). Oh and you missed out the # before include<stdio.h>.

Secondly, your statement if(grade=90) assigns the value 90 to grade and will always equate to TRUE. Use the == operator not the assignment (=) operator.

I think you should try to use a nested if else and your code will never print "A" if the grade is above 90. Check line 5 to see why.

Thirdly, you might wanna re-think as to what this (grade<90||grade<80) or any of the below statements do.
Try to look at blackcoder41's sample code, if you still can't figure it out.

Lastly, check this thread http://www.cplusplus.com/forum/beginner/1988/. It tells you a little something about using 'system("pause")'.
Last edited on
thnx everyone guys..
some uses cout and cin instead of printf and scanf,wich is easier?
@black \coder41

no need to write return?after the bracket?
[code]if(grade>=90) {
printf("A");
} else if(grade<90 && grade>80) {
printf("B");
}
[\code]

should quit this course..better shift another course ..hehe C++ is very complicated.
you dont really have to since your function is of type void. though it is also possible to force your function to return to the caller at the point of your choice.
About using 'printf' or 'scanf': something I do is whenever I don't have to do with object oriented programing I use: 'printf' and 'scanf'... They're also easier when it comes to formatting output(printf) but as I said somewhere else it's good thing to learn it all!!!
hey how are you related with bjarne stroustrup. lol
closed account (S6k9GNh0)
Enough is enough...

The use of printf or iostream is preference. Iostream is much more object oriented and a bit more flexible with the use of streams. In C, stdin, stdout, and stderr are file streams that simple point to the console. In C++, it is a IO stream object that is simply defaulted to the console. It's also a bit easier to redirect the stream to something else in C (such as a file or something) as it's it's not much difficult than assigned a FILE pointer. Don't get me wrong, it is NOT hard to do it in C++, just fewer steps.

How to do it in C++:
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
32
#include <iostream>
#include <string>
#include <sstream>

int main(int argc, char ** argv)
{        
    std::cout << "Please enter the grade you recieved in programming: ";
    
    int grade;
    std::string input;
    
    while(true)
    {
        getline(std::cin,input); //Recieve grade from user.
        std::stringstream temp(input);
        if (temp >> grade)
        {
           if (grade >= 0 && grade <= 100)
           {
              if (grade >= 90) std::cout << "Congratulations! You made an A!";
              else if (grade >= 80) std::cout << "Congratulations! You made a B!";
              else if (grade >= 70) std::cout << "Doh! You made a C!";
              else if (grade >= 60) std::cout << "You suck! You made a D!";
              else std::cout << "You fail at life with an F!";     
           }
           else { std::cout << "Fail! Please enter a valid grading number: "; continue; }
           std::cout << std::endl << grade;
           break;
        }
        else std::cout << "Invalid number, please try again: ";
    }
    std::cin.get();


And C++ is difficult and sometimes confusing but it is by no means complicated until you get into advanced arithmetic functions, complex use of pointers, and memory management.
Last edited on

else if (grade >= 60) std::cout << "You suck! You made a D!";
else std::cout << "You fail at life with an F!";

hahaha computerequip,,, did I have you as my teacher at some point in time?
[/edit] or, is that you my older brother???
Last edited on
Oh!God!Ookay!Blackcoder.I'm more adaptive in BASIC,sorry freeze for that mistake.
all you really need to do is change those ors ( || ) into ands ( && ) simple as that. Also in a void function you don't really need those return or curly braces. Here's what it should look like. Hope it helps

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
32
33
34
35
36
37
38
39
include<stdio.h>

void letterGrade(grade);


int main()
{
float grade;
printf("enter student grade ");
scanf("%f",&grade);
letterGrade(grade);
printf("\n\n");
 system("pause");
 return 0;
}

void letterGrade(grade)
{
    if (grade>90) printf("A");
        

    else if (grade<90 && grade<80) printf("B");
        
        

    else if (grade<80 && grade<70)  printf("C");
      
        

    else if (grade<70 && grade<60)  printf("D");
   
   

    else printf("F");
}

    
   
}
@computerequip
thnx man ...

@mozly

this is also good ..thnx
oh god.. what a spoon feed..
Topic archived. No new replies allowed.