-2

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.

Vento
  • 25
  • 4
  • 1
    Try using `std::vector` instead of C style arrays. (int name[size]) it will save you a lot of issues. – JVApen Sep 15 '18 at 17:08
  • 1
    `char cstr[str.length];` -- This is not legal C++. If you want a dynamic array, use `std::vector`. Also, it is a good thing you're using a compiler that gives this error. Too many C++ newbies use a brand of compiler that allows this syntax, thus they wind up thinking what they wrote is valid C++. – PaulMcKenzie Sep 15 '18 at 17:09
  • 1
    Also, why use char arrays such as `cstr`? Just stick with all character data being a `std::string`. Then you don't need the unnecessary `strcpy`. – PaulMcKenzie Sep 15 '18 at 17:16
  • `char cstr[str.length]`, `int EvenNo[str.length];` and `int OddNo[str.length];` are called a "variable length array" (VLA). It is a GCC extension. You need to find the MSVC equivalent. You should consider using a `std::vector` instead. – jww Sep 15 '18 at 20:53

2 Answers2

0

The problem (a problem) is here:

string str = to_string(input); //Convert it to string
char cstr[str.length];
int EvenNo[str.length];
int OddNo[str.length];

The str string is initialized run-time, so its length can't be known compile-time.

But str.lenght is used to set the dimension of three C-style arrays.

In C++ (the standard versions) the size of a C-style arrays must be known compile time.

Suggestion: use std::vector for cstr, EvenNo and OddNo

max66
  • 65,235
  • 10
  • 71
  • 111
0

These three lines are not legal C++ syntax:

char cstr[str.length];
int EvenNo[str.length];
int OddNo[str.length];

The first error to point out is that the length() function is a function, not a property. Thus the syntax would be str.length() (note the parentheses to denote a function call).

But beyond that, arrays in C++ must be declared using a constant expression to denote the number of entries, and not a runtime derived value.

The way that dynamic arrays are done in C++ (or the most preferred way) is to use std::vector:

#include <vector>
//...
string str = to_string(input); //Convert it to string
std::vector<char> cstr(str.length());
std::vector<int> EvenNo(str.length());
std::vector<int> OddNo(str.length());

Also, there really is no need for cstr. The next line will cause a failure, now that cstr is a vector<char>:

strcpy(cstr , str.c_str()); //Put it into char array

Since cstr is no longer an array of char, this will not (or should not) compile. The way to solve it is to get rid of cstr altogether, and just use str throughout your program.

However, if you insisted on keeping the std::vector<char>, then the following will work, but really you should just get rid of cstr. But here goes:

strcpy(&cstr[0] , str.c_str()); //Put it into char array

The following would also have worked if cstr remained an array.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45