简体   繁体   中英

Why is my function not working properly

My function that checks if numbers in the vector are even or odd doesn't work properly. This function prints the numbers that i have typed in the vector and puts them in 2 categories EVENS/ODDS. But if i have a negative number there is a problem with the odds not printing it but evens work. problematic code is here:

void printEvensAndOddsVector(const vector <int>& new_v1)
{
    cout << "Vector Evens: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        if (new_v1.at(i) % 2 == 0)
        {
            cout << new_v1.at(i) << " ";
        }
    }
    cout << " \n";

    cout << "Vector Odds: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        if (new_v1.at(i) % 2 == 1) // Here is the problem.
        {
            cout << new_v1.at(i) << " ";
        }
    }
    cout << " \n";
}

Or more exactly this line: if (new_v1.at(i) % 2 == 1) It prints the numbers that are odd but only the positive ones not the negative ones. But if i change it to if (new_v1.at(i) % 2 != 0) then it works correctly. Why does that happen and is there a problem with the equal operator? If yes then why are the evens getting printed even if they are negatives while still using the equal operator?

Code here for reference.

clude <iostream>
#include <vector>

using namespace std;

void fillVector(vector <int>&);

void printVector(const vector <int>&);

void printEvensAndOddsVector(const vector <int>&); // Prints Evens and Odds.

int main()
{
    vector <int> v1;

    fillVector(v1);
    printVector(v1);
    printEvensAndOddsVector(v1);

    cout << "\n\n\n";
    system( "pause");
    return 0;
}

// Function Definitions
void fillVector(vector <int>& new_v1)
{
    int number;
    cout << "Type in numbers and type -100 to stop: ";
    cin >> number;

    while (number != -100)
    {
        new_v1.push_back(number);
        cin >> number;
    }
}

void printVector(const vector <int>& new_v1)
{
    cout << "\nVector: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        cout << new_v1.at(i) << " ";
    }
    cout << " \n";
}

void printEvensAndOddsVector(const vector <int>& new_v1)
{
    cout << "Vector Evens: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        if (new_v1.at(i) % 2 == 0)
        {
            cout << new_v1.at(i) << " ";
        }
    }
    cout << " \n";

    cout << "Vector Odds: ";
    for (unsigned int i = 0; i < new_v1.size(); ++i)
    {
        if (new_v1.at(i) % 2 == 1)
        {
            cout << new_v1.at(i) << " ";
        }
    }
    cout << " \n";
}

This line: if (new_v1.at(i) % 2 == 1) . It prints the numbers that are odd but only the positive ones not the negative ones.

For negative n , n % 2 returns either 0 or -1 in C++. It never returns 1 , so the condition cannot be true for negative inputs.

As you've discovered, comparing to zero would work.

See Modulo operator with negative values

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