We'd like to remind Forumites to please avoid political debate on the Forum... Read More »
We're aware that some users are experiencing technical issues which the team are working to resolve. See the Community Noticeboard for more info. Thank you for your patience.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
Microsoft Visual C#2008 Homework Help please
Options

tincat
Posts: 935 Forumite
Hi - if anyone can get me started on this I'd be very grateful. I'm just starting a course in C#, and am stuck on the start of a problem:
It's a console application
I need to declare a string array of 3 options for a user to choose from:
small, medium and large
So far I've got the following code below, but it's not working:
When I enter a number, it comes up with the line
"You have chosen the {0} size". How can I get it to say "you have chosen the small size or similar?
Many thanks
_______________________________________________
string[] size = new string[3];
size[0] = "Small";
size[1] = "Medium";
size[2] = "Large";
Console.WriteLine("Enter size required: Small = 1, Medium = 2, Large = 3: {0},{1},{2}", size[0], size[1], size[2]);
Console.ReadLine();
Console.WriteLine("You have chosen the {0} size");
Console.ReadLine();
It's a console application
I need to declare a string array of 3 options for a user to choose from:
small, medium and large
So far I've got the following code below, but it's not working:
When I enter a number, it comes up with the line
"You have chosen the {0} size". How can I get it to say "you have chosen the small size or similar?
Many thanks
_______________________________________________
string[] size = new string[3];
size[0] = "Small";
size[1] = "Medium";
size[2] = "Large";
Console.WriteLine("Enter size required: Small = 1, Medium = 2, Large = 3: {0},{1},{2}", size[0], size[1], size[2]);
Console.ReadLine();
Console.WriteLine("You have chosen the {0} size");
Console.ReadLine();
0
Comments
-
I'm guessing you can't put the {0} in the quotation marks.
Maybe something like this:
Console.WriteLine("You have chosen the" {0} "size");
Or you may need to write the first part:
Console.WriteLine("You have chosen the"
Then a command to output the {0}
Then a command to put the final "size"0 -
Thanks for your help, unfortunately by doing that brought up loads of errors.
I have now got the following, but somehow it always reads as though 'you have chosen the small size', regardless of whether I enter 1, 2, or 3 in the console.
If you can suggest anything else I'll try. Thanks
______________________
string[] size = new string[3];
size[0] = "small";
size[1] = "medium";
size[2] = "large";
Console.WriteLine("Enter size required: small = 1, medium = 2, large = 3:");
Console.ReadLine();
Console.WriteLine("You have chosen the {0} size", size[0], size[1], size[2]);
Console.ReadLine();0 -
I'm not a programmer, but having another look the second line Console.ReadLine(); it looks like it takes what number the user enters, but doesn't do anything with it.
Don't you need to use the input from that to set the size variable?0 -
Probably. That could be it. I'll see if I can look that up, thanks0
-
Firstly your second writeline doesn't pass in the size array parameter so it just echoes out {0}.
The code should be:string[] size = new string[3];
size[0] = "Small";
size[1] = "Medium";
size[2] = "Large";
Console.WriteLine("Enter size required: Small = 1, Medium = 2, Large = 3: {0},{1},{2}", size[0], size[1], size[2]);
int result = int.Parse(Console.ReadLine());
Console.WriteLine("You have chosen the {0} size", size[result-1]);
Console.ReadLine();
Anything else you need, just shout."We're not here for a long time, we're here for a good time"
"Reach for the Sky, 'cause tomorrow may never come"0 -
Thank you very very much:T
It's a complete mystery to me how programs work, and can only hope I'll understand by course end.0 -
Thank you very very much:T
It's a complete mystery to me how programs work, and can only hope I'll understand by course end.
No worries mate. If you have any more questions just give me a shout. We were all there once upon a time"We're not here for a long time, we're here for a good time"
"Reach for the Sky, 'cause tomorrow may never come"0 -
BooYa!!!!
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string sizeNum; // create the variable called sizeNum as a string System.Console.WriteLine("Enter size required: Small = 1, Medium = 2, Large = 3 : "); sizeNum = System.Console.ReadLine(); // sets sizeNum as what the user enters if (sizeNum == "1") { System.Console.WriteLine("You have chosen the Small size."); } else if (sizeNum == "2") { System.Console.WriteLine("You have chosen the Medium size."); } else if (sizeNum == "3") { System.Console.WriteLine("You have chosen the Large size."); } else { System.Console.WriteLine("You have entered the wrong size!!!. Program will now fall over."); } } } }
OK. Not as nice as Phunky Guy's but it works.0 -
If you could be so helpful again:
How do you phrase it if you want the user to enter a size, which would be assigned to X if it falls between 100 and 1000, but would be an invalid entry if any other value was entered?
What I am trying is the following:
________________________
float X;
A1: Console.Write("Please enter number, between 100 and 1000: ");
X = float.Parse(Console.ReadLine());
if (X<"100")|(X>"1000");
goto B1;
B1:
{
Console.WriteLine("You did not enter a valid number: ");
goto A1;
}
_______________
But I get stuck there. It also looks v. clumsy
I don't know if 'if' is the best one to use, and I want to be able to assign the value between 100 and 1000 to X.0 -
DO NOT LET ANYONE ELSE SEE THAT YOU USE GOTO STATEMENTS, GET RID OF THEM AND NEVER USE THEM AGAIN (except in switch)
ok with that over, what your codes does is
IF (x is betwwen 100 and 1000) then do nothing.
GOTO B (goes to
Writes the line
GOTO A1 (goes back to a1)
a while construct is better in this case, you dont need an if0
This discussion has been closed.
Confirm your email address to Create Threads and Reply

Categories
- All Categories
- 350.8K Banking & Borrowing
- 253.1K Reduce Debt & Boost Income
- 453.5K Spending & Discounts
- 243.8K Work, Benefits & Business
- 598.7K Mortgages, Homes & Bills
- 176.8K Life & Family
- 257.1K Travel & Transport
- 1.5M Hobbies & Leisure
- 16.1K Discuss & Feedback
- 37.6K Read-Only Boards