I was looking for uses for pointers and this turned out to be one of them. Dynamically allocating memmory. I am a little confused with the keyword new
, and when adding [number] in the end. new int[3]
. I do understand that this question might be bad. I'm only 13.
#include <iostream>
using namespace std;
int main() {
int* scores;
cout << "Enter top 3 scores: ";
//dynamically allocate memory
scores = new int[3];
for (int i = 0; i < 3; i++) {
"Enter score: ";
cin >> scores[i];
}
cout << endl << "Scores are: ";
for (int i = 0; i < 3; i++) {
cout << scores[i] << " ";
}
delete[] scores;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire