0
How do I make this code to run if I do not know how many index I will have, before the user inputs the index value.
#include<iostream> using namespace std; void printmyArr(int Arr[], int size) { for (int x = 0; x < size; x++) { cout << Arr[x] << endl; } } int main() { int myArr[] = {}; int index; cout << "Please enter the number of iterations: "; cin >> index; for (int x = 0; x < index; x++) { cout << "Please enter the value for index '" << x << "' :"; cin >> myArr[x]; } printmyArr(myArr, index); system("pause"); return 0; }
3 Respostas
+ 3
please check out the code at
https://code.sololearn.com/cDQgDwDIpDWV/?ref=app
+ 1
You use dynamic arrays or a vector.
Example:
cin >> size;
int* arr = new int[size];
arr[0] = 55; //example usage
When you are done with it make sure to delete it and make the pointer null. We delete it because you allocated memory that you now no longer need. If you dont delete it you will have memory leaks. We make the pointer null so that it isnt pointing to memory that doesnt exist anymore.
delete[] arr;
arr = nullptr;
I suggest learning vectors as they are easier and safer to use.
- 1
Make the cin << to cout
exp
int test;
cin >> test_1;
cout << "blalalblalbalbalblbla" << test_1 << endl;