lundi 6 juillet 2020

Is there any way I can store multiple int's from a user input into a vector?

I am trying to store multiple int into a vector by having the user input the grade they have received. However I do believe there is a more simplified version of trying to do this. Would it be better to have separate functions? Is there anyone who can guide me in the right direction?

#include <iostream>
#include <vector>
#include <numeric>
using namespace std; 

int main() {
  cout << "Welcome to the Grade Calculator! \n"
       << "What is the student's full name? ";
  string student_name;
  getline(cin,student_name);
  cout << "\nPlease input the points earned for each assignment.";
  
  vector<int> student_grades(8); 
    cout << "\nLab 2: ";
    cin >> student_grades[0];
    cout << "Lab 3: ";
    cin >> student_grades[1];
    cout << "Lab 4: ";
    cin >> student_grades[2];
    cout << "Lab 5: ";
    cin >> student_grades[3];
    cout << "Lab 6: ";
    cin >> student_grades[4];
    cout << "Lab 7: ";
    cin >> student_grades[5];
    cout << "Lab 8: ";
    cin >> student_grades[6];
    cout << "Lab 9: ";
    cin >> student_grades[7];
    cout << "Lab 10: ";
    cin >> student_grades[8];
    
  //Finding sum of the grades
  int sum_of_grades = accumulate(student_grades.begin(),student_grades.end(),0);
  //Console Output
  cout << '\n'<< student_name << " has earned " << sum_of_grades << " points in the course, so far." << endl;
  cout << "\nThere are 2000 points possible in this course." << endl;
  cout << "\nInput the anticpated score for the final project (200 points possible): ";
  int anticipated_score;
  cin >> anticipated_score;
  int total_score = sum_of_grades+anticipated_score;
  cout << "Total Projected Assignment Points: " << total_score << endl;
  
  cout << "\nFinal Exam Score Needed for Final Course Grade (1000 Points Possible)"
       << "\nTo Earn an A, need at least: " << 1800 - total_score << " points, for a total of: 1800"
       << "\nTo Earn a B, need at least: " << 1600 - total_score << " points, for a total of: 1600"
       << "\nTo Earn a C, need at least: " << 1400 - total_score << " points, for a total of: 1400"
       << "\n To Earn a D, need at least: " << 1200 - total_score << " points, for a total of: 1200" << endl;
       
  return 0;

}

Aucun commentaire:

Enregistrer un commentaire