-4

I try to create a table wich contans a number choosen by user but it given me an error :

constant expression required

#include <iostream.h>
#include <conio.h>
main(){
clrscr();
int i,k,nval,pos=0,neg=0;
cout<<"Entrer Le nombre de valeur que vous voulez saisir nval = ";
cin>>nval;
int tab[nval];
for (i=0; i<nval; i++){
k=i+1;
cout<<"Le nombre la valeur numeros = "<<k<<"= ";
cin>>tab[i];
if (tab[i]>0) pos+=1;
else if (tab[i]<0) neg+=1;
}
cout<<"Le nombre des valeurs positives = "<<pos<<endl;
cout<<"Le nombre des valeurs negatives = "<<neg;
getch();
return 0;
}

is there any site to try directly the C++ codes ?.

KOTL NIAS
  • 127
  • 4
  • 11
  • 1
    If you format your code with consistent indentation other readers may find it easier to help you. – Galik Nov 24 '18 at 09:02
  • @KOTLNIAS Sorry. My 1st comment didn't apply. To _declare the `std`_ I'd recommend you'll get a newer compiler. Also see: https://stackoverflow.com/questions/29329776/vectors-header-file-in-dosbox – πάντα ῥεῖ Nov 24 '18 at 09:03
  • what you mean exactly? – KOTL NIAS Nov 24 '18 at 09:04
  • C++ have never had [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). If you had an up-to-date compiler and environment I would have told you to use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector), I don't know the equivalent pre-standard container. – Some programmer dude Nov 24 '18 at 09:04
  • @Some With turbo-c++ you're probably forced to implement something _homebrew_. – πάντα ῥεῖ Nov 24 '18 at 09:05
  • i'm obligated by the teacher to use turbo c++ – KOTL NIAS Nov 24 '18 at 09:05
  • 2
    Anyway, you really should consider upgrading to a newer development environment. Turbo-C++ is from before C++ was standardized *twenty years ago*. Nothing but some of the basic syntax you learn with Turbo-C++ will be usable for any real work, you will have a *lot* of studying to do if you want to become a good programmer *after* this course. There are plenty of free compilers and environments that you can use, that are up to date with the latest standards and have very good editors and environments. – Some programmer dude Nov 24 '18 at 09:06
  • @KOTLNIAS _"i'm obligated by the teacher to use turbo c++"_ Well your only chance is to use a buffer with some size big enough to fit in the values given for `nval`e.g. like `int tab[512];` – πάντα ῥεῖ Nov 24 '18 at 09:07
  • i would like to do that but the exercise required to choose the number of table element by the user – KOTL NIAS Nov 24 '18 at 09:08
  • @KOTLNIAS I just told you. An alternative would be to use dynamic memory allocation with `new` and `delete` to have really _"arbitrary size"_ arrays. – πάντα ῥεῖ Nov 24 '18 at 09:08
  • 2
    Don't use TurboC. Use a C++11 conforming compiler like [GCC](http://gcc.gnu.org/) – Basile Starynkevitch Nov 24 '18 at 09:16

1 Answers1

3

You can't create an array with a variable size unknown at compile time. You could try creating an array in heap memory instead.

int *tab = new int[nval];

But then you will have to free it after using.

delete[] tab;
Victor
  • 461
  • 1
  • 5
  • 14
  • it's working thank you!! – KOTL NIAS Nov 24 '18 at 09:14
  • You're welcome. Just don't forget to call delete[] after you're done with this array, otherwise you will lose (leak) memory. – Victor Nov 24 '18 at 09:16
  • 1
    @KOTLNIAS if this answers/solves your question (which it does) you should accept it as such for letting others to see your issue is solved by this ... That is done by clicking on the check icon near the answer score ... – Spektre Nov 24 '18 at 09:24