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
Comments
-
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.
This is what i've done as simply as possiblefloat x;
Console.Write("Please enter number, between 100 and 1000: ");
x = float.Parse(Console.ReadLine());
if ((x >= 100f) && (x <= 1000f))
{
Console.WriteLine("Your number {0} is between 100 and 1000",x);
}
else
{
Console.WriteLine("You did not enter a valid number: ");
}
A few things:
1. Are you sure you want to use float (decimal point numbers like 104.5 rather than integers (whole numbers?)
2. Don't use goto
3. you need to && for a conditional AND. || is a conditional OR (you want AND in this case)
4. Your IF syntax was slightly wrong.
Anythign else you need clarifying, let me know...
p.s. No offence mr_fishbulb but if you have a lot of IF clauses,you can also use a switch statement (google it) which is better."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 -
Thanks again,
I've input your code, and it works, but if I enter a number that isn't valid, I need the user to be prompted to input a number again. I put braces around that section and added a label. If I shouldn't use goto, what do you recommend?
_____________________________
A1:
{
float x;
Console.Write("Please enter number, between 100 and 1000: ");
x = float.Parse(Console.ReadLine());
if ((x >= 100f) && (x <= 1000f))
{
Console.WriteLine("Your number {0} is between 100 and 1000", x);
Console.ReadLine();
}
else
{
Console.WriteLine("You did not enter a valid number: ");
Console.ReadLine();
goto A1;
}
}
_______________________
Yes, I do need the float, as the code will later be used to work out prices.
Will try and find an alternative to goto
I knew there was something missing from the 'if'. Oh this is not easy but you've all really helped...0 -
FYI single & and | is legal but its not normally used
X && Y (does not evaluate Y if x is false)
X & Y (evaluates Y even if x is false)
X || Y (does not evaluate Y if x is true)
X | Y (evaluates Y even if X is true)[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"enter number"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] input = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Int32[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Parse([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine());[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (input < 100 || input > 1000)[/SIZE] [SIZE=2]{[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Invalid Number, try again"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE] [SIZE=2]input = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Int32[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Parse([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine());[/SIZE] [SIZE=2]}[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"number valid"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine();[/SIZE]
0 -
p.s. No offence mr_fishbulb but if you have a lot of IF clauses,you can also use a switch statement (google it) which is better.0
-
mr_fishbulb wrote: »I only put Visual Studio Express on my machine for this thread - first time at programming since C and Pascal at uni 10 years ago!
I can sympathise - the last time I did any programming was 17 years ago! Glad to see it hasn't changed too much!!0 -
i did C# for the first time just over 3 years and i have to say ive forgotten most of it0
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