The first C++ program that atomicthumbs wrote asks "WHAT YOU SAY", and then, when you type it, says "YOU SAID:" and then what you wrote, unless it was a swear word, in which case it says "YOU SAYED A SWEAR D: !!!!"
I did, but when I typed in "fuck" it would say OH NOES and then do the else function. This makes it work, for some reason, and the book doesn't say anything about that. So I assume I've made some sort of grevious programming faux pas.
Your style sucks. Put it all in a pre tag so that the indentation (you are indenting your code, right?) sticks. And at least be consistent about where you put your curly braces.
Each of those if statements is considered separately, and the else only goes with the last one. the ones after the first should be "else if" In any case, you really really really need to be consistent with your style for readability. I recommend putting all the {}s on their own line at least for now.
Discussion (29)
tell me what I did wrong:
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::string;
using std::endl;
string hello;
int main()
{cout << "WHAT YOU SAY" << endl;
cin >> hello;
cout << endl;
if (hello == "fuck" || hello == "shit" || hello == "crap")
{
cout << "YOU SAYED A SWEAR!!1! OH NOES1!1!! D:" << endl;
return 0;
}
if (hello == "poop")
{
cout << "DREW DID I SAY YOU COULD USE THIS";
return 0;}
if (hello == "hi")
{
cout << "Hi yourself.";
return 0;
}
else
{cout << "I DON'T THINK I HEARD YOU PROPERLY; DID YOU SAY ";
cout << hello;
cout << "?";}
return 0;
}
no u
I did, but when I typed in "fuck" it would say OH NOES and then do the else function. This makes it work, for some reason, and the book doesn't say anything about that. So I assume I've made some sort of grevious programming faux pas.
Your style sucks. Put it all in a pre tag so that the indentation (you are indenting your code, right?) sticks. And at least be consistent about where you put your curly braces.
that's because you have
if (a){}if (b){
} else {
}
And 'fuck' is a and not b
Actually you have:
if (a) {}
if (b) {}
if (c) {
} else {
}
which is pretty silly since the whole thing seems to sit just as a conditional on "hello".
But you did something else wrong as well?
k
I just started doing this today, so don't be too hard on me. :P
Ah. Apologies... when you said "the first program.." I thought this was sometime ago.
Better to say this:
Each of those if statements is considered separately, and the else only goes with the last one. the ones after the first should be "else if" In any case, you really really really need to be consistent with your style for readability. I recommend putting all the {}s on their own line at least for now.
Also, your program is an excellent candidate for a switch statement.
switch and strings. show us how that'd be done rorek!
Does that not work?
I suppose not.
Yeah, It's putting in BRs where it really shouldn't.
No. You can only switch on fundamental types. int and chars and bytes basically. I don't think you can even do it on floats.
It is still possible, but you have to translate the string first, in lisp you'd do something like:
(make-symbol hello) => 'poop
in python you might have to:
But then of course, if you're gonna do that you might as well go the whole hog:
This is like the Jyte Guide to the UK but with less references to Morgan Twattybollocks.
:D
Thanks, guys!
I'm not quite to the part on else ifs yet in my book. :P Jsut elses and ifs.
I tried to make my braces consitent, but it didn't quite work right. I'll work on it. :)
Hopefully I'll know enough in three months to make a game for the Pandora .
(By the way, Drew is my annoying, 12-year-old brother.)
I thought I posted the new code a moment ago. Oh well...
It's Hai, version 1.1!
That's it.
You're style still sucks.
Claims inspired by this comment
Programmers nearly always tell you that your style sucks."You can only switch on fundamental types. int and chars and bytes basically. I don't think you can even do it on floats."
This is correct; case labels have to be integer constants. In C, they weren't even allowed to be constants, but had to be literals.[1]
D'A
[1]: It can be a compile-time-evaluated expression; those turn into literals before the case statement sees them.
I forgot/didn't see a bracket. :P
"{cout << "WHAT YOU SAY" << endl;"
That's what you get for poor bracket style.
Claims inspired by this comment
Poor coding style is its own punishment.Poor brace style. Not brackets.
They're curly brackets. Bite me.
And the thingies before the "define" and "include" at the top are octothorpes.
Also, I think I'm getting better:
http://pastebin.ca/895509
^Wrote that on paper and typed it in, as an exercise to see how much I can do without looking at the book and without syntax highlighting.
Now that I've figured out arrays and for loops, I'm going for vectors. Wish me luck.
I'm learning this faster than I thought I would. :)
Not bad. Well done.
:)