Class Call problem

A friend and I can not get this program to work. It is supposed to get the Passer rating of two players and then compare them. When we get to the second player input everything starts to mess up. Please help us fix this problem!!! Thanks!



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <cmath>
using namespace std;

class football
{
public:
	string  n, t;
	int interception, td, yrd, PR;
	double a, b, c, d, comp, att, w;

public:
	void input() {
	//	cout << "Input the player's name and the team he plays for\n.";
	//	getline (cin, n );
	   // getline (cin, t );
		cout << "Input the player's attempts and completion" << endl;
		cin >> att;
		cin >> comp;
		cout << "Input the player's Tds and interceptions ." << endl;
		cin >> td; 
		cin>> interception;
		cout << "Input the player's yards gained in the season ." << endl;
		cin >> yrd;
	}


	void playerspass() {
		cout << " The QB has  " << att << "  attepts on the season with "<< comp << " completed for a pass " << endl;
	}
	void TDandInts() {
		cout << " "<< n << "had  " << td <<" touchdowns" << " and " << interception << " interceptions on the season"  << endl;
	}
	void yrds() {
		cout << " the Qb had  " << yrd << " in the season "  << endl;
	}
	void firsta(){
		a= ((comp/att) -.3)*5;
		cout<< a << endl;
	}
	void firstb(){
		b=((yrd/att)-3)*.25;
		cout<< b << endl;
	}
	void firstc(){
		c=(td/att)*20;
		cout<< c << endl;
	}
	void passerating(){
		w= ((a+b+c+d)/6)*100;
		cout<<" the QB's rating is " <<  w << endl;
	}
	void firstd(){
		d= 2.375-((interception/att)*25);
		cout<< d << endl;
	}
};

void comparePlayers (football f,football g ,double firstPlayer, double secondPlayer);

int main()
{
	football f, g; // Creating object of class
		string  n, t, n1, t1;
		cout << "Input the player's name and the team he plays for\n.";
		getline (cin, n );
	    getline (cin, t );
	    
	f.input();
	//f.playername(); 
	cout << " the QBs name is " << n << " and he plays for the " << t << endl;
	f.TDandInts();
	f. playerspass();
	f.yrds();
	f.firsta();
	f.firstb();
	f.firstc();
	f.firstd();
	f.passerating();
		cout << "Input the player's name and the team he plays for\n.";
		getline (cin, n1 );
	    getline (cin, t1 );
	g.input();
//	g.playername();
	cout << " the QBs name is " << n << " and he plays for the " << t << endl;
	g.TDandInts();
	g.playerspass();
	g.yrds();
	g.firsta();
	g.firstb();
	g.firstc();
	g.firstd();
	g.passerating();
	comparePlayers ( f, g ,f.w, g.w);
	return 0;
}

void comparePlayers (football f,football g ,double firstPlayer, double secondPlayer)
{
	string firstName, secondName;
	firstName = f.n;
	secondName = g.n;
	if ( f.w > g.w) {
		cout<< firstName << " had the higher passer rating." << endl;
	}
	else{
		cout<< secondName << " had the higher passer rating." << endl;
	}
}
Last edited on
> When we get to the second player input everything starts to mess up
Provide an example input, the output that generates and the expected output.


1
2
3
	string  n, t;
	int interception, td, yrd, PR;
	double a, b, c, d, comp, att, w;
Use meaningful names for your variables.
Line 81 is reading the newline that you entered after the first player's yards gained. THen line 82 reads the line where you entered the name. When it tries to read attempts and completions, it's reading the line you entered for the city and it goes downhill from there.

To fix this, add cin.ignore() before line 81.

You'll still have problems though because the code is confused about where the names and teams are stored. You have members for them in the football class but you don't use them. I'd restore the code that inputs the name and team in football::input() and get rid of the corresponding code in main(). Then recreate the commented out playername() method and call it.
Topic archived. No new replies allowed.