[completed]tiny C++ proggie returning a weird result

My program returns everything correctly until the average percent (pct). It comes out with a single digit with a long string of crap at the end.

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
#include <iostream>

using namespace std;

int main ()
{

 double t1, t2, t3, a1, a2, a3, a4, pct;

  cout << "Enter Scores Test 1: \n";
  cin >> t1;
  cout << "Enter Scores Test 2: \n";
  cin >> t2;
  cout << "Enter Scores Test 3: \n";
  cin >> t3;
  
  cout << "Enter Scores Assignment 1: \n";
  cin >> a1;
  cout << "Enter Scores Assignment 2: \n";
  cin >> a2;
  cout << "Enter Scores Assignment 3: \n";
  cin >> a3;

 pct = (  (  (t1 + t2 + t3  ) / 3.0 ) * .70 ) + ( ( ( a1 + a2 + a3 + a4  ) / 4.0 ) * .30 );

cout <<"Summary Report"<<endl;
cout <<"Test Scores: "<<t1<<", "<<t2<<",and "<<t3<<"\n";
cout <<"Assignment Scores: "<<a1<<", "<<a2<<", and "<<a3<<"\n";
cout <<"Overall Percentage: "<<pct<<"\n";

if (pct >= 90)
   cout << "Final Grade: A"<<endl;
else if (pct >= 80)
     cout << "Final Grade: B"<<endl;
else if (pct >= 70)
     cout << "Final Grade: C"<<endl;
else if (pct >= 60)
     cout << "Final Grade: D"<<endl;
else if (pct >= 50)
     cout << "Final Grade: F"<<endl;
   
cin.get ();
cin.get ();
 
  return 0;
}    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Enter Scores Test 1:
90
Enter Scores Test 2:
90
Enter Scores Test 3:
90
Enter Scores Assignment 1:
90
Enter Scores Assignment 2:
90
Enter Scores Assignment 3:
90
Summary Report
Test Scores: 90, 90,and 90
Assignment Scores: 90, 90, and 90
Overall Percentage: 5.30153e+267
Final Grade: A



Last edited on
Change line 29 to read as follows

cout <<"Overall Percentage: "<<fixed<<pct<<"\n";

That should help you identify the problem. :-)

Good luck!
I don't get it (only my second week of learning C++), but now my proggie closes by itself
That is a separate problem.

The first, with the extra characters, is a notation for N x 10M. So
5.30153e+267

is saying
5.30153 x 10267

...which means that there is something wrong with the way you are calculating pct.

The second problem is because you have too much in the input buffer. I don't know why, but fix it by changing lines 42 and 43 to
1
2
3
4
5
6
  // Get rid of the newline left in the input from the last "cin >> foo".
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  // Tell the user how to quit the program.
  cout << "Press ENTER to quit";
  // Wait for him to obey.
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

You will have to #include <limits> to use the changes.

Hope this helps.
Get rid of the a4 there or have the user input the value for a4. That should work.

1
2
3
4
5
6
7
8
cout << "Enter Scores Assignment 1: \n";
  cin >> a1;
  cout << "Enter Scores Assignment 2: \n";
  cin >> a2;
  cout << "Enter Scores Assignment 3: \n";
  cin >> a3;

 pct = (  (  (t1 + t2 + t3  ) / 3.0 ) * .70 ) + ( ( ( a1 + a2 + a3 + a4  ) / 4.0 ) * .30 );
Last edited on
thank you everyone!
Topic archived. No new replies allowed.