Kept on getting the result "0.0%" as my total

In the code below, my results would always be 0.0%. I'm trying to make it 77.5%
Any ideas? I'm assuming there's something wrong with the math equation:
percentage = numCorrect / numQuestions;


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
string name;
int numQuestions,
numCorrect;
double percentage;

cout << "Enter student's first and last name: ";
getline(cin, name);
cout << "Number of questions on the test: "; // I put 40 here
cin >> numQuestions;
cout << "Number of answers the student got correct: "; // I put 31 here
cin >> numCorrect;

cout << "\n";

percentage = numCorrect / numQuestions; // Here I try to divide 31/40 to get the percentage

cout << name << " " << setprecision(1) << fixed << percentage << "%" << endl; // Trying to get 77.5 percent here next to the name, but got 0.0% instead

return 0;
}
percentage = numCorrect / numQuestions;

You're doing integer division (dividing two integers). The result will always be 0 unless numCorrect >= numQuestions.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.