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"
#include<iostream>
#include<iomanip>
usingnamespace 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=newdouble[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 = newdouble[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 = newdouble[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.