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?
#include <iostream>
usingnamespace 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;
elseif (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.