Halp PLEASE !

Hy guys. I am new here and I need your help.Long story short I want to improve my efficiency at work and for that I want to write a small program in C++ , because I learned C++ long time ago in high school..
What do I need from the program :
1.I need to enter several variables from the keyboard , for example today will arrive 4 transports , next week 7 transports and for every transport I need to enter to cost individual . I thought to use array and here is my following try:
1
2
3
4
5
6
7
8
9
10
11
  int n; // n will be the number of transports that will change every time 
    int i;
    cin>>n;
    double receptii[n];
    for(i=0;i<n;i++) 
   {
       cin>>receptii[i];
   }
   
   return 0;
}





2.Of course the transports will suffer some operations (*-+/) and I need them to take individual , sum them and cout<< them .
I hope that express myself correctly.
Thank you in advance.
Last edited on
Hey and welcome. Please edit your post and use code tags for all of your code, to make it more readable - http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
int n;
int i;
cin>>n;
double receptii[n]; // a big nono 

This is forbidden/illegal in c++. Arrays has to be constant at compile time. I would suggest you use an std::vector, but if you insist on using an array, you'd have to dynamically allocate memory.
1
2
3
4
int n;
int i;
cin>>n;
double* receptii = new double[n];


Of course then you'd also need to delete the allocated memory, otherwise you will have memory leak, and you don't want any of those - http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
I agree with @TarikNeaj on using Vectors instead of arrays. In many cases, dynamically allocated arrays can create messes and sometimes memory leaks.
Topic archived. No new replies allowed.