. Advertisement .
..3..
. Advertisement .
..4..
I get an error:
expression must have integral or unscoped enum type.
when I try to run the following code:
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
int main(){
float size;
float sumNum = 0;
float maxNum, minNum;
float mean;
float totalDev = 0;
float devSqr = 0;
float stdDev;
//Create a user input size
std::cout << "How many number would you like to enter? ";
std::cin >> size;
float *temp = new float[size];
//Getting input from the user
for (int x = 1; x <= size; x++){
cout << "Enter temperature " << x << ": ";
cin >> temp[x];
}
//Output of the numbers inserted by the user
cout << endl << "Number --- Temperature" << endl << endl;
for (int x = 1; x <= size; x++){
cout << " " << x << " --- " << temp[x] << endl;
sumNum = sumNum + temp[x];
}
//Calculating the Average
mean = sumNum / size;
maxNum = minNum = temp[1];
for (int x = 1; x <= size; x++){
if (maxNum < temp[x]){
maxNum = temp[x];
}
if (minNum > temp[x]){
minNum = temp[x];
}
}
//Calculating Sample Standard Deviation
for (int x = 1; x <= size; x++){
totalDev = totalDev + (temp[x] - mean);
devSqr = devSqr + (pow((temp[x] - mean), 2));
}
stdDev = sqrt((devSqr / (size - 1)));
cout << endl << "The sum: " << sumNum << endl; //the sum of all input
cout << "The mean: " << mean << endl; //calculate the average
cout << "Maximum number: " << maxNum << endl; // print biggest value
cout << "Minimum number: " << minNum << endl; // print smallest value
cout << "The range between the maximum and the minimum: " << maxNum - minNum << endl; //the range
cout << "Deviation: " << totalDev << endl;
cout << "The squares of deviation: " << devSqr << endl;
cout << "The Standard Deviation: " << setprecision(1) << fixed << stdDev << endl;
system("pause");
}
How to fix the c++ expression must have integral or unscoped enum type. Please give me some good ideas.
The cause:
After researching your error for hours, I found that the size of your variable is expressed as float size. However, the size of an array cannot be a floating point variable, it must be an integer value.
Another issue is perhaps caused by writing outside the array’s bounds:
Because C++ arrays are zero-based, this will write past the end of the array and never write the first element in your original function.
These are the cause of your error.
Solution:
To solve this error, you have to replace float size by an integer value.
And remember not to write outside the limits of the array.