I was trying to make a program that get user's integer input and then filter every single digit in that int into even number and odd number. There is no any mistake when I finished the code but error comes out when I run it.
My code:
#include <iostream>
#include <string>
#include <array>
#include <stdio.h>
#include <cstring>
#include <sstream>
using namespace std;
int main() {
int input = NULL;
int EvenNumbering = 0;
int OddNumbering = 0;
cout << "Please input a number: ";
cin >> input;
string str = to_string(input); //Convert it to string
char cstr[str.length];
int EvenNo[str.length];
int OddNo[str.length];
strcpy(cstr , str.c_str()); //Put it into char array
//Now filter Even number and Odd number
for (string x : cstr) {
int z = stoi(x);
if (z % 2 == 0) {
EvenNo[EvenNumbering] += z;
EvenNumbering++;
}
else {
OddNo[OddNumbering] += z;
OddNumbering++;
}
}
cout << endl;
cout << "Even Numbers: ";
for (int x : EvenNo) {
cout << x << ", ";
}
cout << endl;
cout << "Odd Numbers: ";
for (int x : OddNo) {
cout << x << ", ";
}
system("pause");
return 0;
}
My error:
source.cpp(18): error C2131: expression did not evaluate to a constant
source.cpp(18): note: a non-constant (sub-)expression was encountered
source.cpp(19): error C2131: expression did not evaluate to a constant
source.cpp(19): note: a non-constant (sub-)expression was encountered
source.cpp(20): error C2131: expression did not evaluate to a constant
source.cpp(20): note: a non-constant (sub-)expression was encountered
source.cpp(26): error C2065: 'x': undeclared identifier
source.cpp(40): error C2065: 'x': undeclared identifier
source.cpp(47): error C2065: 'x': undeclared identifier
1>Done building project "Question.vcxproj" -- FAILED.
Still new to C++ and this is my first Project so please forgive me if I did some beginner mistake.