![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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? |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#4 |
|
Expert Programmer
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|