If else shortcut
is this true ?
// input price
// when price >= 90000 give 20% discount
// suppose the input price is 100000
then i do this
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
#include <string>
using namespace std;
int main(){
int price = 100000;
cout << "Total price : " << (price >= 90000) ? cout << (price / 5) * 4 : cout << price << endl;
cin.get();
return 0;
}
|
why the program is error ?
the output is
This make the program works...
1 2
|
cout << "Total price : ";
price >= 90000 ? cout << (price / 5) * 4 << endl : cout << price << endl;
|
but in java we can do directly on the print so the code is just 1 line
System.out.println(.... ? .... : ...);
is there any other way for c++ ?
Last edited on
cout << (price >= 90000) ? cout << (price / 5) * 4 : cout << price << endl;
shouldn't have cout first. shorthand if-else formats are usually
condition ? value_if_true : value_if_false
and with that, the code should have been
(price >= 90000) ? (cout << ((price / 5) * 4)) : (cout << price << endl);
Topic archived. No new replies allowed.