I NEED HELP I can't get this to work....

I am writing a code to compare if a number is positive, negative or zero.. I can't get the loop to work, not sure why.. and it always says positive for zero... help?

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>

using namespace std;

float num;
string checkAnotherNum;
string checkNumber;
bool quit = false;

int main()

{

bool quit2 = false;

     cout << " Check whether a number is positive, negative or zero: " << endl;
     cout << " --------------------------------------------------------- " << endl;
     cout << " Input a number: " << endl;
     cout << " " << endl;

     while (true) {
        cin >> num;

        if (num >= 0){
        cout << "The entered number is positive." << endl;
        }
       else if (num <=0){
        cout << "The entered number is negative." << endl;
       }
       else if (num ==0){
        cout << "The entered number is zero." << endl;
       }

       quit = false;

       while (!quit2){
        cout << "Check another number (Y/N)? " ;
        cin >> checkAnotherNum;
        cout << " " << endl;

}

        if (checkAnotherNum == 'Y' || checkAnotherNum == 'y'){
            quit = false;
           quit2 = true;
        }
        else if (checkAnotherNum == 'N' || checkAnotherNum == 'n'){
            quit = false;
           quit2 = true;
        }
        else {
            cout << "Please enter only (Y/N): " << endl;
            quit = false;
        }
     }
}
In what way does the loop not work?


This loop here is nonsensical. If quit2 is false, it loops FOREVER. If quit2 is true, it doesn't even loop once. Why does this loop exist?
1
2
3
4
5
6
 while (!quit2){
        cout << "Check another number (Y/N)? " ;
        cin >> checkAnotherNum;
        cout << " " << endl;

}



Look at the code you posted. Look at line 41. Look at that } all the way over on the left. Doesn't that look odd?
Last edited on
hmmmm (so NEW at this) But... should I delete that loop for it to work?
You should get it working without any loops to start with.

Mkae it take one input number, and tell you the right answer, and then finish.

When you've done that, you're ready to make it loop.
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
#include <iostream>

using namespace std;

float num;
string checkNumber;
string checkAnotherNum;

int main()
{
    cout << " Check whether a number is positive, negative or zero: " << endl;
    cout << " --------------------------------------------------------- " << endl;
    cout << " Input a number: " << endl;
    cout << " " << endl;

     while (true) {
        cin >> num;

        if (num > 0) cout << "The entered number is positive." << endl;
        else if (num < 0) cout << "The entered number is negative." << endl;
        else cout << "The entered number is zero." << endl;


my_label:
        cout << "Check another number (Y/N)? " ;
        cin >> checkAnotherNum;
        if (checkAnotherNum == "Y"|| checkAnotherNum == "y")
            continue;
        if (checkAnotherNum == "N" || checkAnotherNum == "n")
            return 0;  // quits main()
            
        cout << "Please enter only (Y/N): " << endl;
        goto my_label;
    }
}

It's sometimes difficult breaking through nested loops and tracking its conditions. Although some programmers decrying the use of 'goto', in some situations it helps to make the code much clearer.
Last edited on
Topic archived. No new replies allowed.