简体   繁体   中英

enter Odd, Even, zero and negative numbers and count using for and while loop in C++ without using arrray

I've met some problem during programming. I want to write a program to differentiate even numbers, odd numbers, zero values and negative numbers by using while and for loop. 1st question: However, when I try to run my program, the last number I've entered will not be counted. I know it occur because of my o++ put at the top of the if condition, how should I solve my problem? 2nd question: For the for loop parts, actually it may ignored those negative values. How should I solve it to let the negative numbers also count in loop? May I changed the num>0 to num < 100000 to let the for loop works?

#include<iostream> 
using namespace std;

#include<iostream> 
using namespace std;

int main ()
{
    int num ,numbers = 1 ;
    char answer = 'Y' ;
    int o=0, e=0, z=0 ,n=0 ;
    // o for odd numbers, e for even numbers, z for zero values, n for negative numbers 
    cout << "Enter number" << numbers << ": " << endl ;
    cin >> num ;
    for ( num = num ; num >0; num++)
    while (answer == 'y' || answer == 'Y')
    {
        if (num % 2 == 0 && num > 0)
            {
                e++ ;
                cout<< "The number of even numbers is :" << e << endl; 
                numbers ++ ;
                cout<<"Please enter number" << numbers  << endl ;
                cin >> num ;
                cout<<"If you wish to continue, Please enter y or Y to continue this program : "<< endl ;
                cin>> answer ;
            } 
        else if (num % 2 == 1 && num > 0)
            { 
                o++; 
                cout<< "The number of odd numbers is :" << o << endl; 
                numbers ++ ;
                cout<<"Please enter number" << numbers  << endl ;
                cin >> num;
                cout<<"If you wish to continue, Please enter y or Y to continue this program : "<< endl ;
                cin>> answer ;
            }
        else if (num == 0)
            {               
                z ++;
                cout<< "The total of 0 is :" << z << endl; 
                numbers ++ ;
                cout<<"Please enter number" << numbers  << endl ;
                cin >> num;
                cout<<"If you wish to continue, Please enter y or Y to continue this program : "<< endl ;
                cin>> answer ;
            } 
        
    }
        
    
    cout << "The total even numbers is :" << e << endl;
    cout << "The total odd numbers is :" << o << endl ;
    cout << "The total negative numbers is :" << n << endl ; 
    cout << "The total zero number is:" << z << endl; 
    return 0; 
}

This line, in main() is really puzzling:

// ...
for ( num = num ; num >0; num++)
while (answer == 'y' || answer == 'Y')     

The for(;;) statement is your main loop. The while statement will be executed as long as num is positive.

Let's look at this for() statement in detail:

for (num = num;   // num = num ???  this statement does nothing.
     num > 0;     // the while statement (and the contents of the whule() loop block) 
                  // will only execute if num is > 0.
     ++num)       // if num was > 0 then this loop will run until num overflows...

Removing the for(;;) statement will make your program run a lot better.

Your o++ has nothing do with it.
(Perhaps you have been so convinced about that being the problem that you didn't think of looking elsewhere. It happens to everyone.)

The problem is that your sequence is this:

  1. Check the most recently entered number and print the result
  2. Ask the user for a number, but don't do anything with it
  3. Ask the user whether they want to continue
    • If they want to continue, repeat from item 1
    • If they don't, stop counting

And since you stop counting if the user doesn't want to continue, the last number seems to have disappeared. Fixing it left as an exercise.
(Think more carefully about which order you need to do things in.)

Handling negative numbers requires you to write some code to do that - you handle two cases of positive numbers, and one for zero, but you must have forgotten about the negatives.
Fixing this also left as an exercise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM