How do I separate the different if statements so that it only runs one? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I separate the different if statements so that it only runs one?

#include <iostream> using namespace std; int main() { int r; cout<< " The scenario is...\n Katsumoto just died and Nathen Algren is standing before the emperor.\n"; cout<< "in said scenario, on a scale of 1 to 10 how would you rate your emotional pain.\n"; cin>> r; if(r> 2||r<6){ cout<< "you are deeply saddened."; } if (r> 5||r<9){ cout<< "you are heart broken."; } if (r> 8||r<11){ cout<< "you are a drama Queen."; } if (r> -1||r<3){ cout<< "you are nigh on being a sociopath."; } return 0; }

11th Sep 2018, 1:55 PM
Benjamin Burks
Benjamin Burks - avatar
4 Answers
+ 6
use if-else-if if(condition) { //code } else if(condition) { //code } else { //code }
11th Sep 2018, 6:12 PM
blACk sh4d0w
blACk sh4d0w - avatar
0
use the 'switch' command to break them into cases. This will olny let one run.
11th Sep 2018, 2:44 PM
Jason Hoffman
Jason Hoffman - avatar
0
Maybe you need the else keyword
11th Sep 2018, 5:45 PM
Moritz Vogel
Moritz Vogel - avatar
0
Thank you all, I am glad I have such a great online community to help me learn how to program :). May The Lord Jesus Christ be with you always, and may his love reign supreme in you lives.~B.J.Burks
15th Sep 2018, 3:37 PM
Benjamin Burks
Benjamin Burks - avatar