7
\$\begingroup\$

This is one of my first projects, just a quick hobby thing for fun.

#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::chrono;
using namespace std::this_thread;
using std::chrono::system_clock;

int random();

int main()
{
    cout << "Choose a JonTron episode!" << endl;
    string JonTron;
    getline(cin,JonTron);
    cout << "You chose " << JonTron << "." << endl;
    if (JonTron == "Aquaman")
        cout << "What's this blue pants? What-- what's this blue pants?" << endl;
else if (JonTron == "Anti-Drug Games")
    cout << "Too late, talking leg!" << endl;
else if (JonTron == "Banjo-Kazooie")
    cout << "CAAAAAAAAAAAAARS??? CARS?!?!?!" << endl;
else if (JonTron == "California Games")
    {cout << "SWOOD THIS! SWOOD THAT! What the FUCK does SWOOD mean?!?!?!" << endl;
    sleep_for(milliseconds(1000));
    cout << "I used to say it all the time in Cali! Grommet this, grommet that!";
    }
else if (JonTron == "Nightshade")
    {
    cout << "Congratulations! You've just won the Jef and Paul Award for Excellence in Shopping Centers!"<< endl;
    sleep_for(milliseconds(3000));
    cout << "Actually, the crowbar snaps in two." << endl;
    sleep_for(milliseconds(3000));
    cout << "Just kidding." << endl;
    sleep_for(milliseconds(5000));
    cout << "Fool me once, I'm mad." << endl;
    sleep_for(milliseconds(5000));
    cout << "Fool me twice, how could you?" << endl;
    sleep_for(milliseconds(5000));
    cout << "Fool me three times, you're officially that guy, okay--" << endl;
    sleep_for(milliseconds(3000));
    cout << "You know the one, you go to a bar, and he's like," << endl;
    sleep_for(milliseconds(3000));
    cout << "'This suit is ehh, official, it's a Giorgio Armani, actually, my dad knows him'," << endl;
    sleep_for(milliseconds(3000));
    cout << "FUCK YOU!" << endl;
    sleep_for(milliseconds(1000));
    cout << "I AIIIIIIIIIN'T HAVIN' THAT SHIT!!" << endl;
    }
else if (JonTron == "The Skateboard Kid")
    cout << "You just wanna hang up somebody's coat, and the couch falls out!" << endl;
else if (JonTron == "The Zoo Race")
    cout << "What'd they program this in, C--??" << endl;
    else
        cout << "What the fuuuuuck?" << endl;
cout << "You done now? We done? (Press any key [and then hit enter, duy] to be done.)" << endl;
float fuckingdummyvar;
cin >> fuckingdummyvar;
}

My current main concern is the very large block of if-elseif-else. To put things concisely, is there any less gigantically ugly way to do this?

\$\endgroup\$

1 Answer 1

13
\$\begingroup\$

You could use a switch statement instead of the if elses to make it prettier.

Or, you could use a map<string, vector<string>>

  1. Instead of using whole namespaces, restrict usage to single items.

    In order to not pollute your own namespace with names, do single 'using' instead of 'using namespace'. The following will suffice for your code.

    using namespace std;
    using chrono::milliseconds;
    using chrono::system_clock;
    using this_thread::sleep_for;
    
  2. Instead of float fuckingdummyvar; cin >> fuckingdummyvar;, do a single getchar() or use any other method described here.

  3. Use consistent bracing.

    This is a matter of opinion, but I prefer to always have braces around if and else statements, even when only one statement follows. I used to not put any braces, but then I ran into a nice bug that was seriously puzzling me when I added a return to an else and it would always return for some reason. That's because the return was the second statement after the else and there weren't brackets around it.

  4. Use a map to avoid big 'n' dirty blocks of code..

    #include <iostream>
    #include <string>
    #include <chrono>
    #include <thread>
    #include <map>
    #include <vector>
    
    using namespace std;
    using chrono::milliseconds;
    using chrono::system_clock;
    using this_thread::sleep_for;
    
    
    int main()
    {
        map<string, vector<string>> quotes{
                                            {"Aquaman", {"What's this blue pants? What-- what's this blue pants?"}},
                                            {"California Games", { "SWOOD THIS! SWOOD THAT! What the FUCK does SWOOD mean?!?!?!", "I used to say it all the time in Cali! Grommet this, grommet that!" }},
                                            {"The Zoo Race", {"What'd they program this in, C--??"}}
                                          }; //In C++11 and up {"String1", "String2"} is a vector<string> = {"String1", "String2"}
    
        cout << "Choose a JonTron episode!" << endl;
        string JonTron;
        getline(cin, JonTron);
    
        if (quotes.count(JonTron)) { //Checks if there is a map key with the episode that the user inputted.
            for (auto &s : quotes[JonTron]) { //Iterates over every string in the vector<string> that's stored in quotes[JonTron].
                cout << s << endl;
                sleep_for(milliseconds(1000)); //If you want custom sleep times as well, do a map<string, customClass with vector of quotes and vector of milliseconds.
            }
        }
        else {
            cout << "Episode not found!.";
        }
    
        getchar();
    }
    
  5. Use a text file with all the quotes and episodes.

    The best way would be to create a text file and retrieve the quotes from there.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ A csv-file might be a good idea since you can open it in excel or similar programs \$\endgroup\$
    – Gigala
    Jul 7, 2016 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.