### Need help fixing the error thank you

// Lab 3 percentage.cpp
// This program will determine the percentage
// of answers a student got correct on a test.
// PUT YOUR NAME HERE.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
string firstName,
lastName;
int numQuestions,
numCorrect,
totalPoints;
double percentage;

// Get student's test data
cout << "Enter student's first name: ";
cin >> firstName;
cout << "Enter student's last name: ";
cin >> lastName;

// WRITE A STATEMENT TO READ THE WHOLE NAME INTO THE name VARIABLE.

cout << "\nNumber of questions on the test: ";

cin >> numQuestions;

cout << "\nNumber of answers the student got correct: ";

cin >> numCorrect;

// Compute and display the student's % correct
double totalPoints = (double)numCorrect/numQuestions;
percentage = 100 * numCorrect / numQuestions;

// WRITE A STATEMENT TO COMPUTE THE % AND ASSIGN THE RESULT TO percentage.
cout << "\n" << lastName << "," << firstName << " received a score of " << percentage << " on this test.\n";
// WRITE STATEMENTS TO DISPLAY THE STUDENT'S NAME AND THEIR TEST
// PERCENTAGE WITH ONE DECIMAL POINT.

return 0;
}

-------------------------------------------
Show after running need help fixing the code
percentage.cpp: In function ‘int main()’:
percentage.cpp:38:9: error: conflicting declaration ‘double totalPoints’
double totalPoints = (double)numCorrect/numQuestions;
^~~~~~~~~~~
percentage.cpp:17:1: note: previous declaration as ‘int totalPoints’
totalPoints;
^~~~~~~~~~~
http://www.cplusplus.com/forum/beginner/223876/

The problem is that
`totalPoints` was declared near the start as an `int`, and again as a `double`.

You can get rid of the compiler error message by removing one of the two declarations.

If you leave it as a type `int`, as previously explained the value will be truncated (cut short) and therefore the result will be incorrect. Hence it makes sense to remove the first declaration.

This:
 ``123`` `````` int numQuestions, numCorrect, totalPoints;``````
becomes:
 ``12`` `````` int numQuestions, numCorrect;``````

read the error message!

percentage.cpp:38:9: error: conflicting declaration ‘double totalPoints’
double totalPoints = (double)numCorrect/numQuestions;
^~~~~~~~~~~
percentage.cpp:17:1: note: previous declaration as ‘int totalPoints’

its telling you that @line 38 a declaration of totalPoints conflicts with a declaration at line 17

look at them both, you declared totalPoints twice, first as an int (line17) and then as a double (line38) you can only declare a name once at any scope.

I'd remove the on at line 17.
Taking care of those kind of errors is really important and it can steal a lot of your time later. If you need help dealing with those kind of problems there are programs you can use such as checkmarx and others but it's also recommended to learn how to do it on you own!
Good luck.
Topic archived. No new replies allowed.