PDA

View Full Version : Ten Green Bottles...


Ebbsy
11-08-2004, 06:02 PM
I'm trying something out where in a console application, I can start by asking how many verses of the nursery rhyme "Ten Green Bottles" I want to display. I can type in the number of how many verses I want and by pressing enter, it will display the relevant amount of verses. So if I typed in 7, it will display from 7 Green Bottles down to zero.

I'm also trying to get an error message to appear if the number entered is lower than 2. Also need to try and get the grammar correct for the final verse e.g. 'one green bottle' instead of 'one green bottles'

If anyone can provide any help that would be great, has to be within a .cs file, I'd really like to try and progress with this, but I just can't even get started :(

Hopefully thanks in advance,
Ebbsy

bioteq
11-08-2004, 06:05 PM
The answer is quite simple, but sadly enough, I do not know C# (and would rather claw my eyes out that to know it) but if you wish, I can offer an example in C if you are able to read it.

Ebbsy
11-08-2004, 08:38 PM
I think its gotta be in C#, but the solution in C may help me. Thanks :)

nevarmore
11-08-2004, 09:35 PM
The solution to this should be farily language independent. The question is rather homeworkish so I'll only give you some hints:

1. Loops do not neccesarily have to go from 0 or 1 to a number nor from a number down to 0 or 1 they can stop at any arbitrary point.

2. Input checking is very simple and is usually accomplished with an IF statement which will spit out an error and halt. A more elegant solution is to run your input in a do..while loop. Start the loop, ask for input, test it and if it is valid BREAK out of the loop and if it is invalid continue the loop.

Virus
11-09-2004, 07:44 PM
To add along with nevarmore..well, first off i dont know c#, know c++ and java, which c# is sorta similar..anywho,

1) input the number of verses, throw an error if input < 2

2) i would suggest a for loop, as it is fairly simple and easy to do, i would start (int count = input; count > 0; count --)

3) to do the last "one green bottle", i would use the conditional operator... result += (count == 1) ? "." : "s.";...basically saying..(result is the verse you want to output, being a string).. if your on the last verse (1 bottle left = count being 1), add a period to the end of your sentence, otherwise add a "s." to the end (bottle"s.")

Oh well, like I said I dont know any c#, wish I could help out, but thats basically how i would do it if i was in java or c++

bioteq
11-09-2004, 07:50 PM
printf("%d green %s", number, number == 1 ? "bottle." : "bottles");

Basically what Virus said, but in C.

Re-reading your task does sound homeworkish. But I think with the hints, so to speak, given here you should be able to interpret them.

Ebbsy
11-09-2004, 08:11 PM
Thanks alot for the support guys, i'll give it a go.

tomcant
01-08-2005, 07:55 PM
Sorry to dig up such an old topic, but I am new to C# and wanted to try my hand at the OP's problem. I come from C/C++, so it was very straight forward, however, my app will only read values 0-9. I know why, it's because I use Console.Read() to read a number. My question is: How do I read in an integer, without using Console.Read()?

using System;

namespace ten_green_bottles
{
class MainClass
{
public static void Main()
{
char cNum; int iNum;
Console.WriteLine("How many times? ");
cNum = (char)Console.Read();
iNum = Convert.ToInt32(cNum-'0');
if(iNum>1)
for(int i=0; i<iNum; i++)
Console.WriteLine("There "+(iNum-i==1?"was ":"were ")
+(iNum-i).ToString()+" green bottle"+(iNum-i==1?"":"s"));
}
}
}

tomcant
01-14-2005, 05:05 PM
Anyone?

Ethraim
01-15-2005, 07:38 AM
it because Console.Read() only takes the next character so you need Console.ReadLine()

using System;

namespace ten_green_bottles
{
class MainClass
{
public static void Main()
{
int iNum;
Console.WriteLine("How many times? ");
iNum = Convert.ToInt32(Console.ReadLine());
if(iNum > 1)
for(int i=0; i<iNum; i++)
Console.WriteLine("There "+(iNum-i==1?"was ":"were ")
+(iNum-i).ToString()+" green bottle"+(iNum-i==1?"":"s"));
}
}
}
o to put it in a string before you convert it will also work
int iNum;string sNum;
Console.WriteLine("How many times? ");
sNum = Console.ReadLine();
iNum = Convert.ToInt32(sNum);

tomcant
01-15-2005, 05:16 PM
Thanks for the reply. :)