Page 1 of 1 [ 16 posts ] 

Roxas_XIII
Veteran
Veteran

User avatar

Joined: 8 Jan 2007
Age: 33
Gender: Male
Posts: 3,217
Location: Laramie, WY

08 Apr 2010, 8:52 pm

#07: Compile and run this program in Visual Studio 2008:

Code:
//Annoying Program
//Guaranteed to annoy you to kingdom come.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

const char b = 7;

int main()
{
   char a;
   int iquit;
   cout << "Welcome to the most annoying computer program ever." << endl;
   cout << "Enter annoyance level." << endl;
   cin >> iquit;
   cout << "Press 'a' to annoy." << endl;
   cin >> a;
   while(a == 'a') {
      for(int i = 0; i <= iquit; i++) {
         cout << b;
      }
   }
   return 0;
}


It causes your computer to emit a constant beeping sound while using up massive system resources. Only known remedy is shutting down the system. ^^v

It was supposed to only beep a set number of times, but thanks to an unexpected bug it wouldn't stop. Can any programmers here spot the bug?


_________________
"Yeah, so this one time, I tried playing poker with tarot cards... got a full house, and about four people died." ~ Unknown comedian

Happy New Year from WP's resident fortune-teller! May the cards be ever in your favor.


Eggman
Veteran
Veteran

User avatar

Joined: 17 Jul 2008
Gender: Male
Posts: 4,676

08 Apr 2010, 8:55 pm

oh there are more then 101 ways


_________________
Pwning the threads with my mad 1337 skillz.


ValMikeSmith
Veteran
Veteran

User avatar

Joined: 18 May 2008
Age: 54
Gender: Male
Posts: 977
Location: Stranger in a strange land

08 Apr 2010, 9:20 pm

FLASHPLAYER JUST DID IT AGAIN!

NUKEWAR!

NETWORK DISCONNECT IN 3...2...1...ETX



lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 75
Gender: Male
Posts: 9,795
Location: Somerset UK

08 Apr 2010, 10:02 pm

Roxas_XIII wrote:
... but thanks to an unexpected bug ...

... which would have been reported as a warning by your compiler, if ypu were using a sensible compiler, and had warnings turned on. (Constant expression used as loop termination condition.)


_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer


Roxas_XIII
Veteran
Veteran

User avatar

Joined: 8 Jan 2007
Age: 33
Gender: Male
Posts: 3,217
Location: Laramie, WY

08 Apr 2010, 11:27 pm

lau wrote:
Roxas_XIII wrote:
... but thanks to an unexpected bug ...

... which would have been reported as a warning by your compiler, if ypu were using a sensible compiler, and had warnings turned on. (Constant expression used as loop termination condition.)


Really? That's curious. I'll have to ask Jerry* about that.

*Professor Hamann, my Computer Science instructor and head of the CoSci department at UW's College of Engineering.

At any rate, I fixed it so that it only beeps for the number of times you specify it to (the "annoyance level") plus one for good luck:

Code:
//Annoying Program
//Guaranteed to annoy you to kingdom come.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

const char b = 7;

int main()
{
   char a;
   int iquit;
   cout << "Welcome to the most annoying computer program ever." << endl;
   cout << "Enter annoyance level." << endl;
   cin >> iquit;
   cout << "Press 'a' to annoy." << endl;
   cin >> a;
   if(a == 'a') {
      for(int i = 0; i <= iquit; i++) {
         cout << b;
      }
   }
   return 0;
}


_________________
"Yeah, so this one time, I tried playing poker with tarot cards... got a full house, and about four people died." ~ Unknown comedian

Happy New Year from WP's resident fortune-teller! May the cards be ever in your favor.


oliverthered
Veteran
Veteran

User avatar

Joined: 8 Apr 2010
Age: 48
Gender: Male
Posts: 617
Location: southport, uk

09 Apr 2010, 12:00 am

//Annoying Program
//Guaranteed to annoy you to kingdom come.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

const char b = 7;

int main()
{
char a;
int iquit;
cout << "Welcome to the most annoying computer program ever." << endl;
cout << "Enter annoyance level." << endl;
cin >> iquit;
cout << "Press 'a' to annoy." << endl;
cin >> a;
while(a == 'a') {
for(int i = 0; i <= iquit; i++) {
cout << b;
}
cout << "Press 'a' to annoy." << endl;
cin >> a;
a = a != 'a' ? 'a' : 'b';
}
return 0;
}



one-A-N
Veteran
Veteran

User avatar

Joined: 2 Mar 2010
Age: 70
Gender: Male
Posts: 883
Location: Sydney

09 Apr 2010, 5:14 am

I can spot the bug!

You are running Microsoft Windows.

That aside, you don't read a new value for the variable a while inside the loop, so the variable a always == 'a', and the loop is infinite.

Cue infinite buzz.



lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 75
Gender: Male
Posts: 9,795
Location: Somerset UK

09 Apr 2010, 6:43 am

Roxas_XIII wrote:
lau wrote:
Roxas_XIII wrote:
... but thanks to an unexpected bug ...

... which would have been reported as a warning by your compiler, if ypu were using a sensible compiler, and had warnings turned on. (Constant expression used as loop termination condition.)


Really? That's curious. ...

And... you are right. I did look again, and even with a better compiler (gcc), there was no warning. The bug is indeed subtle, in this case.

Your code combined the decision whether to initiate an infinite loop with the same test (on a non-volatile location) being used to maintain that loop. It's something a compiler could spot as unlikely to be what was intended. I'm all in favour of intelligent compilers.

This bug is related to why I dislike (and won't use) the "while(TRUE)" sort of infinite loop: because it relies on an optimiser to remove the test. I always use "for (;;)" - which explicitly shows that no test is present. It comes from a lot of embedded coding... and I don't mean the modern sort of embedded - I mean the sort where you are lucky to count your code space in kilobytes. (Oh... and ALL the compilers were utter rubbish and they could be relied on to regularly produce completely wrong object code.)


_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer


lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 75
Gender: Male
Posts: 9,795
Location: Somerset UK

09 Apr 2010, 6:48 am

Roxas_XIII wrote:
...
It causes your computer to emit a constant beeping sound while using up massive system resources. Only known remedy is shutting down the system. ^^v
...

I just typed CTRL/C. Why would I want to do anything more drastic?


_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer


justMax
Veteran
Veteran

User avatar

Joined: 23 Nov 2009
Age: 44
Gender: Male
Posts: 539

09 Apr 2010, 9:55 am

Image



Roxas_XIII
Veteran
Veteran

User avatar

Joined: 8 Jan 2007
Age: 33
Gender: Male
Posts: 3,217
Location: Laramie, WY

09 Apr 2010, 10:29 am

lau wrote:
Roxas_XIII wrote:
...
It causes your computer to emit a constant beeping sound while using up massive system resources. Only known remedy is shutting down the system. ^^v
...

I just typed CTRL/C. Why would I want to do anything more drastic?


Correction: Only known remedy for those who don't know Command Prompt shortcuts.


_________________
"Yeah, so this one time, I tried playing poker with tarot cards... got a full house, and about four people died." ~ Unknown comedian

Happy New Year from WP's resident fortune-teller! May the cards be ever in your favor.


LordoftheMonkeys
Veteran
Veteran

User avatar

Joined: 15 Aug 2009
Age: 35
Gender: Male
Posts: 927
Location: A deep,dark hole in the ground

09 Apr 2010, 11:44 am

C++? Yuck!

Anyway, you could do the beeping thing in pretty much any programming language that has ASCII escape sequences:

Code:
#include <stdio.h>

int main(){
   for(;;) printf( "\a" );
   return 0;
}



pakled
Veteran
Veteran

User avatar

Joined: 12 Nov 2007
Age: 67
Gender: Male
Posts: 7,015

10 Apr 2010, 1:52 pm

Here's the same thing, without programming.
Make sure your speaker's on.
Lay something heavy on the keyboard.
Wait...;)


_________________
anahl nathrak, uth vas bethude, doth yel dyenvey...


CockneyRebel
Veteran
Veteran

User avatar

Joined: 17 Jul 2004
Age: 50
Gender: Male
Posts: 116,836
Location: In my little Olympic World of peace and love

12 Apr 2010, 2:07 pm

Have a hot drink beside your computer, and don't watch what you're doing, when you stand up.


_________________
The Family Enigma


Apple_in_my_Eye
Veteran
Veteran

User avatar

Joined: 7 May 2008
Gender: Male
Posts: 4,420
Location: in my brain

13 Apr 2010, 4:32 am

A few from my highlights reel of computer killage:

* Plug an old hard drive (laptop drive) into the cable 1 pin off. Watch the controller smoke when the computer is turned on.

* Stick (accidentally) a screwdriver into the power supply cooling fan, causing it to short, resulting in the entire power supply overloading and burning out.

* In high school I wrote a machine code program (6502) that would tic the speaker -- only way to stop it was a reset.



sarek
Pileated woodpecker
Pileated woodpecker

User avatar

Joined: 18 Apr 2010
Age: 60
Gender: Male
Posts: 190
Location: Noord-Holland or thereabouts

20 Apr 2010, 12:53 pm

pakled wrote:
Here's the same thing, without programming.
Make sure your speaker's on.
Lay something heavy on the keyboard.
Wait...;)


Your sig:

anahl nathrak, uth vas bethude, doth yel dyenvey...

Thats from an old Arthur movie right?. Its a spell spoken by Merlin.
I have remembered those lines for decades ever since I first saw that movie!


_________________
It is time
To break the chains of life
If you follow you will see
What beyond reality