WAP using ternary operator

Aug 13, 2014 at 4:38pm
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.
Aug 13, 2014 at 4:41pm
What is WAP?
Aug 13, 2014 at 4:45pm
write a program? and sounds like this is homework or a test?
Aug 13, 2014 at 4:46pm
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.
Aug 13, 2014 at 5:15pm
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 Aug 13, 2014 at 5:19pm
Aug 13, 2014 at 5:32pm
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.

Aug 13, 2014 at 6:22pm
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 );
Aug 14, 2014 at 8:43am
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 Aug 14, 2014 at 8:46am
Aug 14, 2014 at 8:54am
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.