#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    48
    Rep Power
    10

    Trying to exit while loop using c#


    Hi everyone,
    I'm trying to exit a while loop using any negative number.

    When programming with c++ i used
    int number;
    while(number!=-1)
    {
    cout<<"Enter number";
    cin>>number;
    }
    and that loop would loop until -1 was entered

    Now, i'm writing code with the same concept except its being programmed in c#. I used the above logic except the compiler returned with an error.

    Thanks for your reply in advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Location
    INDY, USA
    Posts
    174
    Rep Power
    0
    Originally Posted by mavirhino
    Hi everyone,
    I'm trying to exit a while loop using any negative number.

    When programming with c++ i used
    int number;
    while(number!=-1)
    {
    cout<<"Enter number";
    cin>>number;
    }

    Thanks for your reply in advance.
    "number" needs to get given a value before it will compile.
    I'm not a console person so I am not for sure here but I'm not familar with cout and cin , c++ yes C# not sure.
    Last edited by neonland; March 20th, 2006 at 12:09 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    9
    Rep Power
    0
    Would have been useful if you stated what the error was, but this code works in a C# console application:

    Code:
                int number = 0;
                while (number != -1)
                {
                    Console.WriteLine("Enter number");
                    number = Int32.Parse(Console.ReadLine());
                    Console.WriteLine("You entered: '{0}'", number);
                }
  6. #4
  7. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,399
    Rep Power
    1232
    Originally Posted by InternetUser
    Would have been useful if you stated what the error was
    Originally Posted by neonland
    "number" needs to get given a value before it will compile
    I thought he did a prefectly well statement of what the error was, especially you didn't provide any code.
    this might be a good thing to read to get questions answered better in the future.

    -MBirchmeier
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    9
    Rep Power
    0
    Hi there!

    Don't mean to be rude or anything, but I'm pretty sure at least one of us is smoking crack.

    Originally Posted by MBirchmeier
    I thought he did a prefectly well statement of what the error was
    I think you read the second post in this thread, not the first which contained this:

    Originally Posted by mavirhino
    I used the above logic except the compiler returned with an error.
    Also

    Originally Posted by MBirchmeier
    ...especially you didn't provide any code.
    I did provide code. C# code .. that works.
  10. #6
  11. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,399
    Rep Power
    1232
    Originally Posted by InternetUser
    Hi there!

    Don't mean to be rude or anything, but I'm pretty sure at least one of us is smoking crack.
    I would say this is a pointed attempt to be rude.

    However you are right, I thought you were the original poster, complaining about the quality of the response, and not continueing on to offer more advice, more often than not it is that scenario. I just glazed over the usernames and looked at the type of response.

    -MBirchmeier
    <edit> my advice still stands though, the OP should read on how to better post a question
    Last edited by MBirchmeier; March 20th, 2006 at 09:31 AM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    48
    Rep Power
    10
    oh yeah thanks guys,
    makes more sense now thanks, what i was doing was not letting number = 0.
    so i got the "Use of unassigned local variable 'number'" error.
    But now i'm getting an exception. Is it possible to exit with any negative number in stead of just 1( which in my case is -1).

    thanks for your reply in advance
  14. #8
  15. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,399
    Rep Power
    1232
    You could just change the while loop to be
    Code:
    while(number >= 0 )
    but where are you getting an exception?

    -MBirchmeier
  16. #9
  17. Dev Shed Ninja
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2005
    Location
    British Columbia, CANADA!
    Posts
    581
    Rep Power
    43
    If you just want to exit the loop at any point use:

    break;

    Or if you want to exit the method you're currently in:

    return;
    killer ninja coding monkeys do exist!
    My Website |My Blog | My Projects

IMN logo majestic logo threadwatch logo seochat tools logo