Dynamic Arrays and User Input

Hi all,

I'm still fairly new to c++ and am having trouble with dynamic arrays. My task is to create an array for test scores. The user needs to define the size of the array and then input data to later sort and calculate the average.

Below is what I have so far. I am struggling with the user input part. I receive the following errors for the data input of the individual scores:

"error C2108:subscript is not of integral type"
"IntelliSense:expression must have integral or unscoped enum type"

I would appreciate any help or advice. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	//user input total number of scores
	cout<<"Please enter the total number of test scores."<<endl<<endl;
	int tnos;    //tnos=total number of scores
	double *sArray=new double[tnos];//sArray= scores array
	cin>>tnos;
	
	
	//input individual scores and store in array
	cout<<"Enter the scores. Press 'Enter' after each entry. No negative entries.";
	double scores;	
	//use a for loop for multiple cin inputs
	for(scores=0;scores<tnos;scores++)
	{
		cin>>sArray[scores];
		

}
You need to get the input before you create the array.

1
2
3
int tnos;
double *sArray = new double[tnos]; //Whoops! tnos hasn't been given a value yet! It's going to default to 0
cin >> tnos; //Well now it has a value, but it's too late :( 


Change to:

1
2
3
int tnos; //Might make this unsigned to be safe
cin >> tnos; //Now we have a value to  use!
double *sArray = new double[tnos]; //Now an array of doubles is created on the heap with the specified number, tnos, of elements! 


So you almost had it. Note, remember to call delete[] on that sArray pointer once you're done with it.
Topic archived. No new replies allowed.