WAP using ternary operator

1.WAP in C++ to enter marks in 3 subject and calculate percentage and grade.cond given below (Using Ternary operator)
Per Grade
90-100 A
80-90 B
70-80 C
0-70 D

2. WAP to enter 3 no.s in and print them in ascending and descending order .(Using Ternary operator)

It would be great to know answers ASAP.
What is WAP?
write a program? and sounds like this is homework or a test?
WAP = "Write A Program"

No thanks OP, we've already gone through the basics ourselves. If you have some code you want help with then we will gladly take a look. Otherwise if you want someone to write it for you then you're looking for the Jobs section.
yes, WAP means "Write a Program"
1
2
3
4
5
6
7
8
9
//1st program
#include<iostream.h>
void main()
{
int m1,m2,m3,p;
cin>>m1>>m2>>m3; //m1,m2,m3 are marks p is %age
p=(m1+m2+m3)/3;
(p>90)?((cout<<'A'):((P>80)?(cout<<'B'):(P>70)?(cout<<'C')):cout<<'D';
}

The line with bold is the one i am facing problem with.
Last edited on
main should return int, not void.

The compiler is probably complaining about the parenthesis. There are 8 opening ( but only 7 closing ).

Also you've defined the variable p as lowercase, but you're using an uppercase P in that line.

That line can be written with just a single cout and single set of parentheses if written like this:
 
    cout << ( put the required code here );
It helps if you order your multiple ternary operators. Rewriting what you've posted (and moving cout out)
1
2
3
4
cout <<
    (p > 90) ? 'A' :
    (P > 80) ? 'B' :
    (P > 70) ? 'C' : 'D';

This way, you can visually verify what you've written.

I have transcribed the code as is, so you have an upper and lower case p.

EDIT: I should point out that your checks aren't quite right. For example, what grade does it print for score 90, and what should it print?
Last edited on
As mentioned by kbw you are not doing what you are required.

90-100 A
80-90 B
70-80 C
0-70 D


You say if p > 90 to output 'A' but you need it to be greater than or equal to 90 so either >= 90 or > 89 you will also have to do this with the other cases.
Topic archived. No new replies allowed.