It allows you to type in your name but following that it goes all wonky. I know I don't have the percentage thing programmed correctly, working on that but even before I added math to it the two cins for numQuestions and numAnswers are not working. Any suggestions?
// 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>
usingnamespace std;
int main()
{
string name;
int numQuestions,
numCorrect,
studentName,
totalPoints;
double percentage;
// Get student's test data
cout << "Enter student's first and last name: ";
cin >> studentName;
// WRITE A STATEMENT TO READ THE WHOLE NAME INTO THE name VARIABLE.
cout << "Number of questions on the test: ";
cin >> numQuestions;
cout << "\n";
cout << "Number of answers the student got correct: ";
cin >> numCorrect;
cout << "\n";
// Compute and display the student's % correct
totalPoints = numQuestions / 100;
percentage = totalPoints * numAnswers;
// WRITE A STATEMENT TO COMPUTE THE % AND ASSIGN THE RESULT TO percentage.
cout << studentName << " 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;
}
// 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>
usingnamespace 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
totalPoints = numQuestions / 100;
percentage = totalPoints * numCorrect;
// 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;
}
I'm not sure that your calculation of the percentage is using the correct method. I suggest you try it out with a piece of paper and a calculator and some actual numbers.
However - an important point, if you try to do a divide with two integers, the result will also be an integer. For example 22/7 results in 3. If you want a floating point result, you must make sure that at least one of the values is of type double (or float). For example 22/3.0 will result in 3.142857. If that result is then stored in a variable of type int, then the decimal part will be lost again.