简体   繁体   中英

vector subscrip out of range error c++

#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.

Now I am getting an error that vector iterator not differenciable

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.

Use < instead of <= .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM