display | more...
Note, before the pointful part of the writeup:
I was going to put this under "Stupidest thing you've coded just to see if you could", but it doesn't precisely describe the story, and that node is too full already. A better name for this node would be "The worst way to tell singular from plural", but that's long and unwieldy.
I'm a high school senior at the time of this writing, and am taking a C++ class. On a recent quiz, the teacher told us to write a fragment of code that did the same thing as the following:

if (i == 1)
    cout << "You have " << 1 << " thing." << endl;
    cout << "You have " << i << " things." << endl;

...without using if, while, do/while, for, goto, or switch. The point of the exercise, as might be obvious, was to get us to use the choice operator. However, even if I remembered that the choice operator existed at the time, I certainly didn't remember how to wrangle my condition and choices around that question mark and colon. Therefore, I wrote this monstrosity:
    bool a = (1 - i);
    char sornot[2];
    sornot[0] = ('s' * a + '.' * (1 - a));
    sornot[1] = ('.' * a + ' ' * (1 - a));
    cout << endl << "You have " << i << " item" << sornot[0] << sornot[1] << endl;

...and it worked!

I stuck this at the start:

#include <iostream>
using namespace std;

int main ()
        int i;
        cout << "How many items do you have? ";
        cin >> i;

...and this at the end:
        return 0;

...and it compiled and ran. Frankly, I astonished myself that something so ridiculous-looking worked, when the programs I write using the methods we're supposed to usually don't work.

I owe my success to two facts that I happened to remember. Despite the fact that I knew very little C++ at the time (and still do!), I did know that character variables are just numbers, and can be manipulated as such, and that if a boolean variable is set equal to any number other than zero, it is really set equal to one.

I got full credit for my answer, since it did work perfectly, disregarding the extra space that is output at the end of the sentence when i equals one.

I'm proud of my achievement - that it's some of the ugliest, most confusing functional code that my teacher's ever seen.
At the risk of sounding pretentious, I honestly don't know if the function of the code I wrote is apparant. It did take me about twenty minutes to come up with the premise and write it so it works. I was thinking of explaining how I actually thought of what I did and why it works, but I don't know if that's necessary, and if no one reading this writeup actually wants to read an explanation, it would seem long-winded and boring. If anyone doesn't get it, or thinks it could use an explanation, msg me and I'll add one.
December 3, 2001: This just caught my eye in the Random Nodes nodelet. It always bothers me when a writeup of mine gets only one or two votes, or none. I'm much more satisfied with, say, +4/-4 than a writeup that no one apparantly read. But no one still read it.

Log in or register to write something here or to contact authors.