I’m new to C++ and am not sure what’s wrong. This is a task I have been given in my programmging course at uni which is meant to take user input of a vector of grades and determine whether the grade is a passing one. When I compile I end up getting an error stating q1.cpp:30:21: error: could not convert ‘y’ from ‘int’ to ‘std::vector’
Not overly sure why. Sorry about the bad formatting.
I’ve added the code but not sure how to wrap it.
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
int calcNumberOfPasses(vector<int> grades){
int x;
for (int i=0; i<grades.size(); i++){
cin >>grades[i];
}
cin >> x;
}
int main() {
int y;
vector<int> nGrade;
nGrade.push_back(y);
cout << "Enter how many grades you want to enter";
for (int i=0; i<nGrade.size();i++){
cin >> nGrade[i];
}
cin >> y;
if (y>=50){
cout << "this is a passing grade";
}
calcNumberOfPasses(y);
}
The function
calcNumberOfPassesis expecting a parameter of typevector<int>, you are passing it a parameter of typeint. That much you can work out from the error message.You are copying an undefined value into the vector on this line:
Following that you are looping over the size of the grades vector, which hasn’t been initialised yet.
Chances are, you want to do
calcNumberOfPasses(nGrades);.As an aside, you should use a reference to the vector, to avoid copying it.
In summary, I would through all of this code away and start again. No offence!