Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 8th, 2005, 11:52 PM   #1
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
99 Bottles of bear on the wall

For some reason, this program returns the weirest results. Not sure where I'm going wrong.

Here is the code:
[php]
using System;
class beer
{
public static void Main( )
{
double bottles;
Console.Write("How many bottls of beer are on the wall?\t");
bottles = Console.Read();


for (int i = 0; i < bottles; i++)
{
Console.Write("{0} bottles of bear on the wall.\n{0} bottles of bear.\nTake one down, pass it around.\n",bottles);
bottles = bottles - 1;
Console.Write("{0} bottles of bear on the wall.\n",bottles);
}
}
}
[/php]

Yet, Any number I put in gives results that start in the 50 and go to the twenties. For example, if I put in 99, I would get.

57 bottles of beer on the wall, 57 bottles of beer, takes one.......
and it would end at 28 bottles.

I'm sure it's something extreemly simply that I'm missing, but any ideas?
thechristelegacy is offline   Reply With Quote
Old Aug 9th, 2005, 12:47 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
57 is the ascii code for '9'. The Console.Read() function gives you back one character, not a number entered by the user.
You can use something like
		bottles = Convert.ToInt32(Console.ReadLine());

Your loop is also wrong, as it stops when "i" is greater than "bottles". Each time round the loop i is incremented and bottles is decremented, so your loop will stop half way (e.g. at 49 when you enter 99)
Try something like
while (bottles > 0)

Also, why use a double for the bottles variable? Are you expecting 99.435 bottles of beer on the wall? I think using "int" is better here.

One last thing, you have "bottls" with no e in the first question of the program and it is outputting "bottles of bear", not "beer". I'm not sure what a bottle of bear looks like, but I doubt there is 99 in the world.
The Dark is offline   Reply With Quote
Old Aug 9th, 2005, 8:30 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
The Dark is right on the money...

using System; 

class beer 
{ 
	public static void Main( ) 
	{ 
		uint bottles; 
		Console.Write("How many bottles of beer are on the wall?\t"); 
		bottles = Convert.ToUInt32(Console.ReadLine()); 
         
   		while (bottles != 0)
		{ 
			Console.WriteLine("{0} bottles of bear on the wall.\n{0} bottles of beer.\nTake one down, pass it around.\n",bottles); 
			bottles = bottles - 1; 
			Console.WriteLine("{0} bottles of bear on the wall.\n",bottles); 
		} 

	} 
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 9th, 2005, 7:37 PM   #4
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
Thanks. I figured it would be an easy program to write, but once I got going, it turned out a bit more complicated than I expected. Thanks for the help though guys

The reason for the idea is I was on a site where it had this program in like 200 languages. I didn't read the C#, because I wanted to see if I could do it myslelf.

Last edited by thechristelegacy; Aug 9th, 2005 at 7:55 PM.
thechristelegacy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC