Will only read one input

I am writing a program that calculates GPA. The user needs to enter four different different data points to determine the GPA but my code is only reading the first user entry and then executing the rest of the program. How can I get it to continue asking for the rest of the input?

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
#include <iostream>
using namespace std;

void main()
{
  const float A=4.0;
  const float B=3.0;
  const float C=2.0;
  const float D=1.0;
  const float F=0.0;

  float FirstClassGrade, SecondClassGrade, GPA, TotalGradePoints;

  int FirstClassHours, SecondClassHours, TotalCreditHours;

  cout << "This program will determine your GPA for two classes.\n";
  cout << "\nEnter letter grade for first class --> ";
  cin >> FirstClassGrade;
  cout << "Enter number of credit hours for first class --> ";
  cin >> FirstClassHours;
  cout << "\n\nEnter letter grade for second class --> ";
  cin >> SecondClassGrade;
  cout << "\nEnter number of credit hours for second class --> ";
  cin >> SecondClassHours;
  
	TotalGradePoints = (FirstClassGrade*FirstClassHours)+(SecondClassGrade*SecondClassHours);
	TotalCreditHours = FirstClassHours+SecondClassHours;
	GPA = TotalGradePoints/TotalCreditHours;
  
  cout << "\n\n"<<FirstClassGrade<< "     " <<FirstClassHours<< endl;
  cout << "\n"<<SecondClassGrade<< "     " <<SecondClassHours<< endl;
  cout << "\nYour GPA = "<<GPA<< endl;

  cin.ignore(2);
}
Can you show an example of what's happening? In other words, what you are typing in and what is coming back on the screen?
Here is what happens when the program runs:

{{{{
This program will determine your GPA for two classes.

Enter letter grade for first class --> B
Enter number of credit hours for first class -->

Enter letter grade for second class -->
Enter number of credit hours for second class -->

7.58306e-039     5411456

3.48432e-039     2486340

Your GPA = 6.29272e-039
Press any key to continue . . .
}}}}

As you can see, it is only letting me enter one grade and then automatically completes the rest of the program.
Last edited on
12
13
14
15
16
17
float FirstClassGrade, /* ... */;

// ...

cout << "\nEnter letter grade for first class --> ";
cin >> FirstClassGrade;

If you're entering a letter grade into a float (number) variable, it won't turn out pretty....

After all, how is your program supposed to know what number corresponds to "B"?
Last edited on
won't it reference the const float at the top of the program?
No.

All your program sees when you input the grades is a "B", so your program will try to cram a "B" into a float variable (and fail).

The compiler pretty much boils away all variable names, so cin will never know whether or not you happened to have a variable named B in your code (and it probably doesn't care even if you do).

If it were possible to do something like this, you'd potentially end up with some strange results:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
    const std::string hello = "goodbye";
    std::cout << "Enter a string: ";
    std::string str;
    std::cin >> str;
    std::cout << "You entered: " << str;
}
Enter a string: hello
You entered: (???)

If cin were to look for a variable with the same name as my input, then if I were to enter "hello" into this program, should it print "hello" (which is the string I entered), or "goodbye" (which is the value of the hello variable)?
Topic archived. No new replies allowed.