0

I've got some code that defines a variable called 'size' then uses it but for some reason it doesn't work ; the error is "Expression did not evaluate to a constant"

int main()
{
    int size;
    int userValue;
    char input;

    do
    {

        cout << "Enter an int for the size of the array:" << endl;
        cin >> size;

        int a[size];

        cout << "Enter an integer between 1 and " << size << " to search for in the array." << endl;
        cin >> userValue;

        populateArray(a, size);

        linearSearch(a, size, userValue);
        binarySearch(a, size, userValue);

        cout << "Press 'y' and enter to run again or just enter to quit";
        cin.sync();
        cin.get(input);

    }while(input == 'y');
}

You can ignore the other functions as they are not called before the defining of my array, Ive tried setting size to a value, still doesn't work.

EDIT: forgot to mention

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
Wintér
  • 3
  • 5
  • `int a[size]` is a variable length array - which is not a part of standard C++. You should use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – Fureeish Oct 03 '17 at 20:48
  • Technically you can't. An array size must be known at compile time. Some compilers offer an extension that allows Variable Length Arrays, but once you set the size, the size is fixed. Anyway, it looks like your compiler does not offer the VLA extension. Use a `std::vector` instead. – user4581301 Oct 03 '17 at 20:48
  • Ahh, so I do have to use Vectors ; alright, thanks anyways I was just making sure I had no other way of doing this! – Wintér Oct 03 '17 at 20:49
  • @Wintér There are a lot of ways of doing this without `std::vector` but it's the easiest solution. If you refuse to use the standard library you can get away with `new[]` and `delete[]` but it's not recommended. – François Andrieux Oct 03 '17 at 20:55
  • @FrançoisAndrieux Memory managment would just cost more time I'll use vector for now, thanks anyways! – Wintér Oct 03 '17 at 20:56
  • Micronag: Show a little caution with `using namespace std;` it's usually OK in small programs, but can result in some interesting errors. More here: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – user4581301 Oct 03 '17 at 21:01
  • @user4581301 I know that, right now I'm writing small programs to practice. (as you can see I just got done with Binary and Linear searching but I also covered alot of topics) – Wintér Oct 03 '17 at 21:03

1 Answers1

-1

Why not use a vector?

std::vector<int> a(size);

Also in C++, you cannot define an array with a variable like that.

Alternative:

int* a = new int[size];
// Do stuff
delete [] a;
Adrien Logut
  • 812
  • 5
  • 13
  • In C you can. In C++ you can't. C/C++ isn't a thing. – juanchopanza Oct 03 '17 at 20:51
  • 1
    variable length arrays **are allowed** in `C`. Not in `C++` though. – Fureeish Oct 03 '17 at 20:51
  • C99 has VLA as a mandatory feature while it's an optional feature in C11. – François Andrieux Oct 03 '17 at 20:51
  • I didn't use a vector beacuse Im studying via an old youtube series, I coded everything myself (this is a program that detects how many iterations a binary search would do and a linear one) untill I came up with this problem, he leaves his code in a pastebin paste so I coppied it and when it still didn't work I just asked here – Wintér Oct 03 '17 at 20:52
  • @Wintér Some compilers allow VLA as an extension, but it's not standard. If your instructor is using them without at least a disclaimer, I would strongly recommend looking for a new source of instruction. – François Andrieux Oct 03 '17 at 20:53
  • My bad, old C doesn't allow this. I've edited it – Adrien Logut Oct 03 '17 at 20:53
  • @FrançoisAndrieux his name on youtube is ReelLearning he uses eclipse and showed how to install it all but I use Visual Studio. He explains extremly well but my plan is to finish his youtube series and then move on to something bigger not sure how Im going to find something though – Wintér Oct 03 '17 at 20:55
  • What if exception happened while "Do stuff"? Use [std::unique_ptr](http://en.cppreference.com/w/cpp/memory/unique_ptr) – boriaz50 Oct 03 '17 at 20:56
  • @Wintér Odds are the Youtuber uses the g++ compiler. Recommend checking their teachings [against a good book.](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Oct 03 '17 at 20:56
  • @boriaz50 Definitely, but that's more advanced C++ – Adrien Logut Oct 03 '17 at 20:57
  • @user4581301 He is and thank you alot for the question you sent its going to be very very helpful for me! – Wintér Oct 03 '17 at 21:01
  • @Wintér One place to find further skill-building projects is open source. Take a look at some of the projects that build tools that you are interested in or like using. Take a really close look at the project's mailing list, forum, newsgroup, whatever they use and get a read on how welcoming they are to newcommers first though. I think most are good, but some are sociopathic snake pits that are just not a good environment whether you're learning or not. – user4581301 Oct 03 '17 at 21:07