patterncppModerate
Very small JonTron quoter
Viewed 0 times
quotersmalljontronvery
Problem
This is one of my first projects, just a quick hobby thing for fun.
My current main concern is the very large block of
#include
#include
#include
using namespace std;
using namespace std::chrono;
using namespace std::this_thread;
using std::chrono::system_clock;
int random();
int main()
{
cout > 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?Solution
You could use a switch statement instead of the if elses to make it prettier.
Or, you could use a
-
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.
-
Instead of
-
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
-
Use a map to avoid big 'n' dirty blocks of code..
-
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.
Or, you could use a
map>-
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;-
Instead of
float fuckingdummyvar; cin >> fuckingdummyvar;, do a single getchar() or use any other method described here.-
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.-
Use a map to avoid big 'n' dirty blocks of code..
#include
#include
#include
#include
#include
#include
using namespace std;
using chrono::milliseconds;
using chrono::system_clock;
using this_thread::sleep_for;
int main()
{
map> 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 = {"String1", "String2"}
cout 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();
}-
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.
Code Snippets
using namespace std;
using chrono::milliseconds;
using chrono::system_clock;
using this_thread::sleep_for;#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();
}Context
StackExchange Code Review Q#134129, answer score: 13
Revisions (0)
No revisions yet.