Vectors Review - code not working

Anyone know why this doesn’t work?

#include <iostream>
#include <vector>


int main() {
  int num1, num2, num3, num4, num5 = 0;
  
  int eve = 0;
  int odd = 1;
  std::vector<int> num {};
  
  std::cout << "Enter 5 numbers: ";
  std::cin >> num1;
  num.push_back(num1);
  std::cin >> num2;
  num.push_back(num2);
  std::cin >> num3;
  num.push_back(num3);
  std::cin >> num4;
  num.push_back(num4);
  std::cin >> num5;
  num.push_back(num5);
  std::cout << "\n";
  
  for(int i = 0; i < num.size(); i++) {
    if (num[i] % 2 == 0) {
      eve = eve + num[i];
    } else {
      odd = odd * num[i];
    }
  }
  std::cout << "Sum of even numbers is " << eve << "\n";
  std::cout << "product of odd numbers is " << odd << "\n";
}

It’s working, though (numbers separated by a space).

I’m getting a warning when testing this “warning: comparison between signed and unsigned integer expressions [-Wsign-compare]” in cpp.sh (C++ Shell Website) at:

for(int i = 0; i < num.size(); i++) {

Maybe that’s a clue?

yeah i wouldn’t worry about this. i tried the code and it worked for me too.

but if u really want to know more about the i < num.size() warning, take a look here: https://stackoverflow.com/questions/7984955/what-is-wrong-with-my-for-loops-i-get-warnings-comparison-between-signed-and-u

2 Likes

Thanks. When I ran it in codecademy, after putting in the five numbers, it just outputs random numbers for the sum and product. I also ran it in cpp shell and it works now.

edit: When I created the original post I wasn’t specific enough. It executed, but it just outputted random numbers. It seems to work in cpp shell so It’s something with codecademy.