Program will not allow for input even with 'cin <<'

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 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 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;
}


Thank you. Total noob here.
Is studentName really an integer?

Also, if you want to enter first and last names, you need either two separate variables, or use getline with one string to hold both names.
Last edited on
I updated the code and it works now! I just have to fix the percentage problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 // 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
   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.
Topic archived. No new replies allowed.