I want to create an array whose size I will only know at runtime, and then further increase that size during execution of the program.
This is from an /r/dailyprogrammer challenge which can be found here https://www.reddit.com/r/dailyprogrammer/comments/3twuwf/20151123_challenge_242_easy_funny_plant/
MSVisual gives me the error std::badd_array_new_length which means that it's having trouble instantiating the array?
I'm so tired with oftentimes copying code letter for letter from websites where it works and I constantly get errors. Is Visual a bad platform for learning C++? Should I try QT?
#include <iostream> #include <string> void main(int argc, char* argv[]) { int currentPlants = std::stoi(argv[2]), targetPeople = std::stoi(argv[1]), currentProduce = 0, week = 0; int * plants; plants = new int[currentPlants]; for (int i = 0; i < currentPlants; i++) { plants[i] = 0; } if (plants == nullptr) EXIT_FAILURE; while (currentProduce < targetPeople) { currentProduce = 0; for (int i = 0; i < currentPlants; i++) { currentProduce += plants[i]; plants[i]++; } if (currentProduce >= targetPeople) break; else { plants = new int[currentProduce]; for (; currentPlants < currentProduce; currentPlants++) { plants[currentPlants] = 0; } } week++; } std::cout << week; }
<vector>...?new[]and (here evidently missing)delete[]non-sense. You should learn C++ systematically from a good book or tutorial.