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
2»

Comments

  • tincat wrote: »
    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 possible
    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);
    }

    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"
  • tincat
    tincat Posts: 935 Forumite
    edited 26 January 2010 at 6:43PM
    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...
  • RobTang
    RobTang Posts: 1,064 Forumite
    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]
    
  • mr_fishbulb
    mr_fishbulb Posts: 5,224 Forumite
    Part of the Furniture Combo Breaker
    Punky_Guy wrote: »
    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.
    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! But I shall take a look at this switch statement of which you speak :)
  • Plasticman
    Plasticman Posts: 2,540 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Combo Breaker
    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!! :D
  • DCFC79
    DCFC79 Posts: 40,641 Forumite
    Part of the Furniture 10,000 Posts Name Dropper
    i did C# for the first time just over 3 years and i have to say ive forgotten most of it
  • david78
    david78 Posts: 1,654 Forumite
    Punky_Guy wrote: »
    We were all there once upon a time :)

    ... and some of us go back to that place from time to time :rotfl:
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

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

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.