There is a short way to write if statement: ternary if statement.
The format is that (condition) ? [if true, here] : [if false, here]
When using in cout, make sure to use parenthesis like below code.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string weather = "sunny";
double humid = 0.7;
weather = (humid > 0.5) ? "rainy" : "sunny";
cout << "Today'\s weather is " << weather << endl;
//this is the same thing
cout << "Today'\s weather is " << ((humid > 0.5) ? "rainy" : "sunny");
double e = 2.718;
bool isOne = (round(log(e))) ? true : false;
cout << "log(e) is approximatly one: " << isOne << endl;
return 0;
}
cin, getline skipping input (0) | 2020.09.03 |
---|---|
비주얼 스튜디오에서 MinGW를 이용해 c++파일 컴파일하기 (Run c++ file in Visual Studio Code using MinGW) (0) | 2020.08.28 |
Int to string, string to char* in c++ (0) | 2020.07.19 |
Stringstream: cuts the string into parts by space (0) | 2020.07.19 |
Double to int cast (0) | 2020.07.18 |
댓글 영역