0

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.

  • How about learning about arrays? – David H Aug 08 '17 at 20:01
  • 5
    Considering this code is a flagrant violation of the [Zero, One or Infinity Rule](https://en.wikipedia.org/wiki/Zero_one_infinity_rule) of software, yeah, there is. Hint: Use `std::vector` instead of piles of variables called `oregon1` through `oregon4`. You must familiarize yourself with the [Standard Library](http://en.cppreference.com/w/) to be [effective with C++](http://www.aristeia.com/books.html). – tadman Aug 08 '17 at 20:02
  • 2
    Those four `displayScore` functions seem to do exactly the same thing. You only need one function which you can call four times. – interjay Aug 08 '17 at 20:04
  • looking into std::vector as we speak and i tried to use arrays but as new as i am to C++ when i implemented them at first i just confused myself and broke my code so in my head this version was as caveman as I could come up with and it worked for me, but I am still continuously researching information to understand the language as a whole and practicing what i can. P.S. thank you for the reference links! – 3monkeys1gorilla Aug 08 '17 at 20:07
  • Investigate reducing code duplication. – Jesper Juhl Aug 08 '17 at 20:07
  • if your code works post it [here](https://codereview.stackexchange.com/) for code review. – Vanity Slug - codidact.com Aug 08 '17 at 20:09
  • 3
    As stated, you can do a lot better, following some basic principles. One thing should be said tho, if you just started programming this is no surprise that you make such mistakes. Realizing by yourself that your code has a number of problems is a really good and healthy thing. Keep questionning yourself, improve readability, self documenting, and DRY code, you'll get good at it :) – Logar Aug 08 '17 at 20:11
  • @interjay Im trying to think in my head how i would go back and make one displayScore function that computes the different outputs for each quarter, but again that may just be answered with some more research and a better understanding of practices of the language. – 3monkeys1gorilla Aug 08 '17 at 20:20
  • @Jesper Juhl I'm trying to do so as I type (figuratively) – 3monkeys1gorilla Aug 08 '17 at 20:20
  • 1
    A note on your choice of textbook. It's not very good. It rushes though and outright skips some important concepts. This shouldn't be a surprise given the name, but once you're done, or whenever you feel like it, fill in a few of the gaps with [Stroustrup's Tour of C++](https://isocpp.org/tour) or any of the books listed in [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Aug 08 '17 at 20:21
  • @Logar I'm trying, but thank you! – 3monkeys1gorilla Aug 08 '17 at 20:22
  • @user4581301 I actually own that book and have begun to read it, but I bought the aforementioned book first so I'm finally taking the time to go through it before moving on and overwhelming my bookshelf. However, I do appreciate the advice! – 3monkeys1gorilla Aug 08 '17 at 20:23
  • 2
    Pretty sure your book spelled "years" wrong. – Galik Aug 08 '17 at 20:25
  • @Galik well played! – 3monkeys1gorilla Aug 08 '17 at 20:27
  • @3monkeys1gorilla The four functions are all exactly the same because the choice of parameter names doesn't change the function's behavior. So Just leave one of them and call it four times, passing in different values for the parameters each time. The parameter names in the function definition don't need to be the same as the names of the values you pass in. – interjay Aug 08 '17 at 21:27

1 Answers1

1

So first things first, strings are expensive, consider using an enum and then using a map to translate that to a string, for example:

enum Teams {
    OREGON,
    OREGON_STATE
};

const map<Teams, string> Teams2string = { { OREGON, "Oregon" }, { OREGON_STATE, "Oregon State" } };

Then consider that the best way to store scores would be in a vector<pair<int, int>> particularly since we don't know how many, if any, overtime periods will be played. Thus the vector could be resized on a per-game basis.

Finally we need to make a game object which incorporates this information, and provides it's own displayScore method:

struct Game {
    const Teams first;
    const Teams second;
    const vector<pair<int, int>> score;

    void displayScore(const int period) {
        if(period < size(score)) {
            if(score[period].first >= score[period].second) {
                cout << Teams2string[first] << ": " << score[period].first << ' ' << Teams2string[second] << ": " << score[period].second << endl;
            } else {
                cout << Teams2string[second] << ": " << score[second].first << ' ' << Teams2string[first] << ": " << score[first].second << endl;
            }
        }
    }
};
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288