Please use code tags when posting code. See
http://www.cplusplus.com/articles/jEywvCM9/
Your code in tags:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
using namespace std;
int main()
{
char choice,choicee;
cout << "Countinoues fever? \n";
cin >> choice;
{
if (choice == 'yes');
{
cout << "Redness?" << endl;
cin >> choicee;
if (choicee == 'yes' )
{
cout << "hemo dengue";
}
if (choicee =='no')
{
cout << "normal dengue\n";
}
}
if (choice =='no');
{
cout<<" Normal Fever " << endl;
}
}
return 0;
}
|
More readable, is it not? However, with a little bit of indentation ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
using namespace std;
int main()
{
char choice,choicee;
cout << "Countinoues fever? \n";
cin >> choice;
{
if (choice == 'yes');
{
cout << "Redness?" << endl;
cin >> choicee;
if (choicee == 'yes' )
{
cout << "hemo dengue";
}
if (choicee =='no')
{
cout << "normal dengue\n";
}
}
if (choice =='no');
{
cout<<" Normal Fever " << endl;
}
}
return 0;
}
|
Better?
The pair of braces (lines 9 and 27) is not necessary.
The
char choicee
is needed only within body of IF, lines 12-21.
IF "yes"
IF "no"
could be
IF "yes"
ELSE IF "no"
Why? If the first condition is true, then the second condition surely must be false. You have two separate IF-statements and must thus evaluate both conditions. With the ELSE IF you don't have to test for "no" if you already know that it isn't.
(The performance benefit is minimal in this case, but it is important to learn to "see" logic.)
A loop has
for
,
while
, or
do while
.
A nested loop is nothing special. A loop repeats some statements.
A loop is a statement. A statement repeated by a loop X can trivially be a loop Y.
[EDIT]
A compiler says this about the last version:
10:19: warning: multi-character character constant [-Wmultichar]
14:22: warning: multi-character character constant [-Wmultichar]
18:21: warning: multi-character character constant [-Wmultichar]
23:18: warning: multi-character character constant [-Wmultichar]
In function 'int main()':
10:16: warning: comparison is always false due to limited range of data type [-Wtype-limits]
10:25: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
14:19: warning: comparison is always false due to limited range of data type [-Wtype-limits]
18:19: warning: comparison is always false due to limited range of data type [-Wtype-limits]
23:16: warning: comparison is always false due to limited range of data type [-Wtype-limits]
23:23: warning: suggest braces around empty body in an 'if' statement [-Wempty-body] |
All warnings, no errors.
Notice tho line 10 and line 23 warnings:
suggest braces around empty body in an 'if' statement
Empty body? Yes, you have written a semi-colon after parentheses:
if ( condition ) ;
Let me rewrite your code one more time, without unnecessary braces and with conditions as that compiler makes them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
using namespace std;
int main()
{
char choice, choicee;
cout << "Countinoues fever? \n";
cin >> choice;
if ( false )
{
}
cout << "Redness?" << endl;
cin >> choicee;
if ( false )
{
cout << "hemo dengue";
}
if ( false )
{
cout << "normal dengue\n";
}
if ( false )
{
}
cout<<" Normal Fever " << endl;
return 0;
}
|
It appears that the code that you effectively have written is quite different from what you thought that you did write.