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: !!!!"

By 3 atomicthumbs on February 06, 2008

Embed Claim Make a related claim

Discussion (29)

http://printeronfire.org/

3 atomicthumbs who agreed, says

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;
}

Make a related claim 10 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

no u

Make a related claim 10 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

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.

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

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.

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

that's because you have

if (a){}

if (b){
} else {
}

And 'fuck' is a and not b

Make a related claim 10 months ago (link)
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

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?

Make a related claim 10 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

k


#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;
}

Make a related claim 10 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

I just started doing this today, so don't be too hard on me. :P

Make a related claim 10 months ago (link)
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

Ah. Apologies... when you said "the first program.." I thought this was sometime ago.

Better to say this:


if (hello == ...)
{
cout >> "swear word";
}
else if (hello == "poop")
{
cout >> "drew!";
}
else if (hello == "hi")
{
cout >> "hi yourself";
}
else
{
cout >> "get the idea?";
}

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

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.

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

Also, your program is an excellent candidate for a switch statement.

Make a related claim 10 months ago (link)
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

switch and strings. show us how that'd be done rorek!

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

Does that not work?

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

I suppose not.

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

Yeah, It's putting in BRs where it really shouldn't.

Make a related claim 10 months ago (link)
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

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:


a= { 1: "poop",
2: "fuck",
3: "wierd" }.get(hello);
switch (a)

Make a related claim 10 months ago (link)
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

But then of course, if you're gonna do that you might as well go the whole hog:


{"fuck": "whoops! you said a naughty word",
"poop": "you're that guy again",}.get(hello, "well, hi, how are you?")

Make a related claim 10 months ago (link)
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

This is like the Jyte Guide to the UK but with less references to Morgan Twattybollocks.

Make a related claim 10 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

: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.)

Make a related claim 10 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

I thought I posted the new code a moment ago. Oh well...

It's Hai, version 1.1!

#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") //Encourage civility.
{
cout << "YOU SAYED A SWEAR!!1! OH NOES1!1!! D:" << endl;
}

else if (hello == "poop") //Surprise Drew.
{
cout << "DREW DID I SAY YOU COULD USE THIS";
}

else if (hello == "hi") //Be friendly.
{
cout << "Hi yourself.";
}

else
{
cout << "I DON'T THINK I HEARD YOU PROPERLY; DID YOU SAY "; //Demonstrate virtual hearing loss.
cout << hello;
cout << "?";
}


return 0;
}

Make a related claim 10 months ago (link)
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

That's it.

You're style still sucks.

http://darch.myopenid.com/

6 D'Archangel who hasn't voted, says

"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.

Make a related claim 10 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

I forgot/didn't see a bracket. :P

"{cout << "WHAT YOU SAY" << endl;"

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

That's what you get for poor bracket style.

Make a related claim 10 months ago (link)

Claims inspired by this comment

Poor coding style is its own punishment.
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

Poor brace style. Not brackets.

Make a related claim 10 months ago (link)
http://rorek.org/

8 Rorek who hasn't voted, says

They're curly brackets. Bite me.

Make a related claim 10 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

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. :)

Make a related claim 10 months ago (link)
http://www.tapsellferrier.co.uk/nicferrier/

8 nic who disagreed, says

Not bad. Well done.

Make a related claim 9 months ago (link)
http://printeronfire.org/

3 atomicthumbs who agreed, says

:)

Make a related claim 9 months ago (link)
Sign in in to leave a comment.