Reputation: 45
Here's my question:
Suppose your program contains code to create a dynamically allocated array as follows:
int * entry;
entry = new int [10];
so that the pointer variable entry is pointing to this dynamically allocated array. Write code to fill this array with ten numbers typed in at the keyboard.
I've gone through the book for two days now and still can't figure this out.
Here's the code I was trying, but it is giving me an error on line 17 that says: No operator matches these operands "<<". I've checked the msdn and a couple other websites, but I cant figure this out. Any help would be appreciated.
#include <iostream>
using namespace std;
int main()
{
int * entry;
entry = new int [10];
int array_size = 10;
int num;
for(int i = 0; i< array_size; i++)
entry[i] = i;
for(int i = 0; i < array_size; i++)
{
cout << "Enter an int into the array: " << endl;
cin << entry[i] << endl;
}
return 0;
}
Upvotes: 2
Views: 554
Reputation: 16253
You have the wrong direction of the the stream operator: Use cin >> entry[i];
. A good way of remembering this is thinking of the operator as an arrow: For output, you point the stuff you want to output towards cout
, for input, you point the values from cin
towards the variable that should store the input.
By default, cin >> ...
handles whitespace (spaces, tabs, newlines) automatically, so there is no need for the >> endl
either.
Finally, the previous loop setting entry[i] = i;
does not do anything useful in your current program as all the entries are overwritten anyways when the users input their values.
Upvotes: 6