vector subscrip out of range error c++

时间:2021-04-25 16:39:51
#include <iostream>  
#include <vector>  
#include <iomanip>  
#include <cstdlib> 
#include <algorithm> 

using namespace std;  


void determine_grade (double &, char & );  
double calc_average (vector <double> averagevector);  
double find_highest (vector <double> highestvector);  
double find_lowest (vector <double> lowestvector);  




int main ()  
{  
double average = 0.00;  
double highest = 0.00;  
double lowest = 0.00;  
double input;   
char letter;
double score = 0;
vector <double> vecinput(0);  

for (int i = 0; i < vecinput.size(); i++)  
 {  
  cout << " Enter result " << vecinput[i] << "(or -1 if no more result):";
  cin >> input;  
  vecinput[i] = input; 
  if (input < 0 || input > 100) 
  {
      cout << " Invalid Input";
  }
  else(input == -1);
  {
      break;
  }


}

determine_grade(score, letter);

cout << " Summary of the Results:" << endl;  
cout << " Result" << vecinput.size()<<   
cout << setprecision(2) << fixed << score << "Grade" << letter << "\n";

average = calc_average(vecinput);  
cout << " The average of the results =" << setprecision (2) << fixed  << average <<       "\n";  


lowest = find_lowest(vecinput);  
cout << " The lowest of the results =" << setprecision (2)<< fixed << lowest << "\n";  


highest = find_highest(vecinput);  
cout << " The highest of the results =" << setprecision (2) << fixed << highest << "\n";  


system ("Pause");  
return 0;  
}



double calc_average(vector <double> averagevector) // This function will find the  average off the results.  
{  
double total = 0.00;  
double average = 0.00;  
for (int i = 0; i < averagevector.size(); ++i)  
{  
    total += averagevector[i];  
}  
average = (double) total / averagevector.size();   
return average;  
  }  


  double find_highest (vector <double> highestvector) // This function will find the  higest grade of the results.  
{  
double max= 0.00;  
max = *max_element(highestvector.begin(),highestvector.end());

 return max;
}  
double find_lowest (vector <double> lowestvector) // This function will find the lowest     grade of the results.  
{  
 double min = 0.00;
min = *min_element(lowestvector.begin(),lowestvector.end());

 return min;
}  


void determine_grade (double &num, char &grade)  
{  

if (num >= 90 && num <= 100)  
    grade = 'A';  
    else if (num >= 70 && num <= 89)  
    grade = 'B';  
    else if (num >= 60 && num <= 69 )  
    grade = 'C';  
    else if (num >= 50 && num <= 59)  
    grade = 'P';  
    else   
    grade = 'U';  



    } 

I am trying to write a program which has to display the highest, the lowest grade. I have to also display the average, and also the letter grade. I am getting a vector subscript out of range error. on line 932 whenever I try to run this program. I am not sure what I have done wrong.

我正在尝试编写一个必须显示最高,最低等级的程序。我还要显示平均值,也要显示字母等级。我得到一个矢量下标超出范围错误。每当我尝试运行此程序时,在第932行。我不确定我做错了什么。

Now I am getting an error that vector iterator not differenciable

现在我收到一个错误,矢量迭代器不可区分

1 个解决方案

#1


2  

i <= vecinput.size()

.size() returns the size of the vector, and their indices are zero-based, so you are going one past the end of the vector.

.size()返回向量的大小,它们的索引从零开始,所以你要经过向量的末尾。

Use < instead of <=.

使用 <而不是<=。< p>

Also learn how to use a debugger, because you can figure out exactly where the error is occurring with one.

还要学习如何使用调试器,因为您可以准确地找出错误发生的位置。

#1


2  

i <= vecinput.size()

.size() returns the size of the vector, and their indices are zero-based, so you are going one past the end of the vector.

.size()返回向量的大小,它们的索引从零开始,所以你要经过向量的末尾。

Use < instead of <=.

使用 <而不是<=。< p>

Also learn how to use a debugger, because you can figure out exactly where the error is occurring with one.

还要学习如何使用调试器,因为您可以准确地找出错误发生的位置。