You are using a version without Ads of this website. Please, consider donating:

### Football calculator

Hello guys!

I am a complete beginner at C++ and have run into a lot of problems, but google has solved most of them, but not this one.

I am trying to make a really basic calculator that instantly tells me how far a football run was. For example, a football field is divided into two halves of 50 yards. If a player runs the ball from the 20 yardline to the 40 yardline, on the opposite side of the 50 yardline, he ran 40 yards. I have successfully implemented that part, but I can't seem to get it so I can chose whether I want to calculate the yards ran on plays that cross the 50, and plays that don't cross the 50, since each one requires different formulas. I hope that makes sense.

Being a noob I'm sure that I've completely messed up the code, and most of it has been copy and pasted from varying codes around the web. Well anyway, here it is.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697`` ``````// Includes #include #include #include // Use standard namespace using namespace std; // Prototypes int add(int n1, int n2); int sub(int n1, int n2); // Main int main(void) { int x, y; char y_or_n; do { cout << "Enter a 1 for runs past the 50 yardline/n"; cout << "Enter a 2 for runs inside the 50 yardline/n"; cin >> num; if (num < 1 || num > 3) cout << "Sorry, you put in the wrong number!" << endl; } while (num <1 || num >2); if (num == 1) {do { cout << "Yardline of reception: "; cin >> x; cout << "Yardline of tackle: "; cin >> y; //cout << x << " + " << y << " = " << add(x,y) << endl; //cout << 100 << " - " << x+y << " = " << sub(100,x+y) << endl; cout << sub(100,x+y) << "_" << "Yard Return" << endl; //cout << x << " - " << y << " = " << sub(x, y) << endl; cout << "Go again (Y/n)? "; cin >> y_or_n; cout << endl; } } while (tolower(y_or_n) != 'n'); else { cout << "What yardline did the run start?/n"; cin >> x; cout << "What yardline was the runner tackled?/n"; cin >> y; //cout << x << " - " << y << " = " << sub(x,y) << endl; } // Return to OS return 0; } // add ///////////////////////////////////////////////////// int add(int n1, int n2) { int carry, sum; // Find out which bits will result in a carry. // Those bits will affect the bits directly to // the left, so we shall shift one bit. carry = (n1 & n2) << 1; // In digital electronics, an XOR gate is also known // as a quarter adder. Basically an addition is performed // on each individual bit, and the carry is discarded. // // All I'm doing here is applying the same concept. sum = n1 ^ n2; // If any bits match in position, then perform the // addition on the current sum and the results of // the carry. if (sum & carry) { return add(sum, carry); } // Return the sum. else { return sum ^ carry; } } // sub ///////////////////////////////////////////////////// int sub(int n1, int n2) { // In binary arithmetic, subtraction is simply adding the // two's complement. The two's complement is simply one // added to the one's complement. return add(n1, add(~n2, 1)); }``````

There are some error messages that I just simply cannot fix.

Any help is much appreciated!

Cheers
Steve
Hello Steve!
What you might want to do is for a play which crosses the half, 50, you might want to first the difference of the starting point and 50, and the ending point and 50.

For example, if the Patriots stared at the 40 line, and and crossed over to the Giants 30 line, you would find the difference between the start, 40, and the end, 30.

(50-40)+(50-30)=x

Its up to you on how you want to display x.

For the mistakes, I noticed alot that for a newline, you did "/n"
Thats incorrect, use "\n", the type of slash makes the difference.

I recommend reading the tutorial on this site, its short, to the point, and cover alot of material.

Good job, see alot of good stuff in your program.
I see, your formula would be fine as well, but is there any reason to use yours, rather than mine?

Ok, I'll definitely check out the tutorial, I didn't even see it.

And my main point is having the option to chose which formula to use, runs that cross the 50, and runs that don't. I have the formula for both, but I can't get that option thing ( my terminology is bad ;) ) to work correctly.

Here is a program I made that is actually working, but it only works for runs that cross the 50:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````// Includes #include #include // Use standard namespace using namespace std; // Prototypes int add(int n1, int n2); int sub(int n1, int n2); // Main int main(void) { int x, y; char y_or_n; do { cout << "Yardline of reception: "; cin >> x; cout << "Yardline of tackle: "; cin >> y; //cout << x << " + " << y << " = " << add(x,y) << endl; //cout << 100 << " - " << x+y << " = " << sub(100,x+y) << endl; cout << sub(100,x+y) << "_" << "Yard Return" << endl; //cout << x << " - " << y << " = " << sub(x, y) << endl; cout << "Go again (Y/n)? "; cin >> y_or_n; cout << endl; } while (tolower(y_or_n) != 'n'); // Return to OS return 0; } // add ///////////////////////////////////////////////////// int add(int n1, int n2) { int carry, sum; // Find out which bits will result in a carry. // Those bits will affect the bits directly to // the left, so we shall shift one bit. carry = (n1 & n2) << 1; // In digital electronics, an XOR gate is also known // as a quarter adder. Basically an addition is performed // on each individual bit, and the carry is discarded. // // All I'm doing here is applying the same concept. sum = n1 ^ n2; // If any bits match in position, then perform the // addition on the current sum and the results of // the carry. if (sum & carry) { return add(sum, carry); } // Return the sum. else { return sum ^ carry; } } // sub ///////////////////////////////////////////////////// int sub(int n1, int n2) { // In binary arithmetic, subtraction is simply adding the // two's complement. The two's complement is simply one // added to the one's complement. return add(n1, add(~n2, 1)); }``````
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: