Help please. New to nested loop

When i run the project, it wont cout the 2nd loop. Thanks in advance

#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;
}

A char is ONE char. Just one. You want to store "yes" in it? How many characters in "yes"? Three. That's too many for a char. If you want to store a string, use a string. Not a char.

'yes' is just plain wrong syntax. Your compiler should have told you this was wrong. ' ' is used for ONE char. ONE. 'y' is valid; 'yes' is not. You're looking for "yes", not 'yes', but even then the comparison won't work because you'd be comparing a char with a char*. Just use string; it's why they exist.

it wont cout the 2nd loop

This code contains no loops.

Last edited on
> 'yes' is just plain wrong syntax. Your compiler should have told you this was wrong.

It is clearly logically incorrect; but in most implementations, there would be no syntax error.
Multicharacter literal is a conditionally-supported feature of the language.
A multicharacter literal, if supported, has an implementation-defined value of type int

A compiler could (ideally should, if warnings are enabled) generate a warning because it is a non-portable construct.
http://coliru.stacked-crooked.com/a/c9c861042f32357d
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.
Last edited on
closed account (48T7M4Gy)
Much the same but choice can be a single char variable. This makes later y/n or Y/N functionality re-useable and capable of error trapping with ease.

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
#include <iostream>

using namespace std;

int main()
{
    char choice;
    
    cout << "Continuous fever? ";
    cin >> choice;
    
    if (choice == 'y')
    {
        cout << "Redness? ";
        
        cin >> choice;
        if (choice == 'y')
            cout << "HEMO dengue\n";
        else
            cout << "NORMAL dengue\n";
        
    }
    else
        cout<<"NORMAL Fever\n";
    
    return 0;
}
It did seem like the four "expected" responses to the "Continuous fever?" were (or could have been):
y
n
I don't have any fever.
input fails completely

kemort's version classifies "I don't have any fever." as "NORMAL Fever".


It is a design decision how a program reacts to valid and invalid input. Crash and burn is not an intuitive reaction.
closed account (48T7M4Gy)
kemort's version classifies "I don't have any fever." as "NORMAL Fever".
I beg to differ and that's why I wrote it that way after careful consideration with what is in front of us.

The OP addresses continuous fever and differentiates on the basis of that.

The way I read OP's attempt is: A normal fever is a fever that is not continuous. And depending on whether there is redness or not the dengue fever is either hemo or normal dengue fever.

It raises the vitally important question of why would you test someone for fever, especially with an 'expert system' who hasn't got one?

Maybe to prevent program failure for beginners we should also suggest an uninterruptible power supply, just to be on the safe side But hey, who knows for sure :)
Topic archived. No new replies allowed.