I recently began reading my Sam's teach yourself C++ in 24 hours (5th Edition) book and came across Hour 3's first exercise where the question was something along the lines of create const ints for the values of touchdown extra point field goal and safety. I did this for a college game, where I printed the score for each team at the end of each quarter and then added them up for the final score. See code below:
#include <iostream>
int displayScore1(int oregon1, int oregonState1) {
if (oregon1 >= oregonState1) {
std::cout << " Oregon: " << oregon1 << " Oregon State: " << oregonState1 << "\n";
} else {
std::cout << " Oregon State: " << oregonState1 << " Oregon: " << oregon1 << "\n";
}
return 0;
}
int displayScore2(int oregon2, int oregonState2) {
if (oregon2 >= oregonState2) {
std::cout << " Oregon: " << oregon2 << " Oregon State: " << oregonState2 << "\n";
} else {
std::cout << " Oregon State: " << oregonState2 << " Oregon: " << oregon2 << "\n";
}
return 0;
}
int displayScore3(int oregon3, int oregonState3) {
if (oregon3 >= oregonState3) {
std::cout << " Oregon: " << oregon3 << " Oregon State: " << oregonState3 << "\n";
} else {
std::cout << " Oregon State: " << oregonState3 << " Oregon: " << oregon3 << "\n";
}
return 0;
}
int displayScore4(int oregon4, int oregonState4) {
if (oregon4 >= oregonState4) {
std::cout << " Oregon: " << oregon4 << " Oregon State: " << oregonState4 << "\n";
} else {
std::cout << " Oregon State: " << oregonState4 << " Oregon: " << oregon4 << "\n";
}
return 0;
}
int finalScore(int oregonFinal, int oregonStateFinal) {
if (oregonFinal > oregonStateFinal) {
std::cout << " Final Score: " << " Oregon " << oregonFinal << " Oregon State " << oregonStateFinal << "\n";
} else {
std::cout << " Final Score: " << " Oregon State " << oregonStateFinal << " Oregon " << oregonFinal << "\n";
}
return 0;
}
int main () {
const int touchdown = 6;
const int fieldGoal = 3;
const int extraPoint = 1;
const int safety = 2;
int oregon1 = 0;
int oregon2 = 0;
int oregon3 = 0;
int oregon4 = 0;
int oregonState1 = 0;
int oregonState2 = 0;
int oregonState3 = 0;
int oregonState4 = 0;
int oregonFinal = 0;
int oregonStateFinal = 0;
oregon1 = touchdown + extraPoint;
oregonState1 = touchdown + extraPoint;
displayScore1(oregon1, oregonState1);
oregon2 = touchdown + extraPoint;
oregonState2 = touchdown + extraPoint;
displayScore2(oregon2, oregonState2);
oregon3 = touchdown + extraPoint + fieldGoal;
oregonState3 = touchdown + extraPoint;
displayScore3(oregon3, oregonState3);
oregon4 = 0;
oregonState4 = touchdown + extraPoint + fieldGoal + fieldGoal;
displayScore4(oregon4, oregonState4);
oregonFinal = oregon1 + oregon2 + oregon3 + oregon4;
oregonStateFinal = oregonState1 + oregonState2 + oregonState3 + oregonState4;
finalScore(oregonFinal, oregonStateFinal);
return 0;
}
I understand that this is the long way to do it and I got the output to be what I wanted. However, being new to C++, I'm not entirely sure how to write something more flexible for re-use. My question is, is there a more efficient way to do this? Or is there a way to accomplish the same outcome/output in less code? I was happy i figured out the original problem, but I would like to understand/learn efficiency as well as the basics. I understand that vectors and structs/classes may be an avenue, I just don't really understand the reference materials.