STYLE ALERT
Things you should consider.
#1: INDENT. Don't be afraid of whitespace. Obeying simple indentation rules and adding a few blank lines here and there makes your code infinitely easier to read and understand. It'll also help you catch mismatching {} errors.
Cramming as much as you can on as few lines as possible does not do anything for your program other than make it harder to read.
1 2 3 4 5 6 7 8
|
// BAD
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{ cout << "What is your favourite colour? ";
string colour;
| |
1 2 3 4 5 6 7 8 9 10
|
// BETTER
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
cout << "What is your favourite colour? ";
string colour;
| |
#2: Don't embed variable definitions in output statements. It makes them very hard to see and clutters what you're doing:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// BAD
cout << "Are you an \n"; int adventurer; cout << "adventurer, "; int politician; cout << "policitian, or "; int dreamer; cout << "dreamer? ";
// BETTER
int adventurer;
int politician;
int dreamer;
cout << "Are you an\n"
"adventurer, politician, or dreamer? ";
// (note I added a line break in the output there for clarify but you can cram it all on one
// line if you want. But I don't recommend it. Clarity is huge!)
| |
Though I'm not sure why you think you need those adventurer, etc variables anyway....
#3: Don't duplicate code. You're already checking for the strings "adventurer", "dreamer", etc.
If you're already doing it once, don't do it again!.
#4: You want else/if chains here. else
only binds to the
one previous
if
. It does not bind to all of them. In your code example... if the player inputs "adventurer", it will display the adventurer message
as well as the "pick again" message because the pick again message will display if the user inputs anything other than "politician"
(also you are spelling politician wrong in a few places).
Example of #3 & #4:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// BAD
do {
cout << "Are you an \n"; int adventurer; cout << "adventurer, "; int politician; cout << "policitian, or "; int dreamer; cout << "dreamer? ";
string mystring;
cin >> mystring;
if (mystring == "adventurer")
cout << "That's far out, man. You have +2 in all attributes.\n";
if (mystring == "dreamer")
cout << "That's dreamy, man. You have +5 in skill.\n";
if (mystring == "policitian")
cout << "That's psychadelic, man. You have +5 in IQ.\n";
else cout << "That's not an option, man. Pick again.";
} while(mystring != "adventurer" && mystring != "dreamer" && mystring != "politician");
| |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// BETTER
bool valid; // variable to track whether the input was valid or not
do
{
valid = true; // assume input is going to be valid
cout << "Are you an\n"
"adventurer, politician, or dreamer? ";
string mystring;
cin >> mystring;
if( mystring == "adventurer" )
cout << "That's far out, man. You have +2 in all attributes.\n";
else if( mystring == "dreamer" )
cout << "That's dreamy, man. You have +5 in skill.\n";
else if( mystring == "politician")
cout << "That's psychadelic, man. You have +5 in IQ.\n";
else
{
cout << "That's not an option, man. Pick again.";
valid = false; // input was not valid
}
}while(!valid); // keep looping until input was valid
| |