Error in code

This is a discussion on Error in code within the C# Programming forums, part of the General Programming Boards category; Code: Can anyone find logical error in this code,explain it and how to fix??? int myNumber; int sum = 0; ...

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Ireland
    Posts
    13

    Question Error in code

    Code:
    Can anyone find logical error in this code,explain it and how to fix???
    
    int myNumber;
    int sum = 0;
    
    Console.Write("Enter value : " );
    myNumber = int.Parse(Console.ReadLine());
    
    while (myNumber != -999
    {
      Console.Write(Enter value :");
      myNumber = int.Parse(Console.ReadLine());
    
    sum = sum + myNumber;
    }
    Console.WriteLine("Sum is {0}", sum);

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I don't know about logical, but you've got a syntactical problem:
    Code:
    while (myNumber != -999)
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Ireland
    Posts
    13
    what is the problem with this??

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    You don't add the first number input to the sum since you ask for a new number before the 'summing'

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,459
    - Also you aren't validating user input. If the user inputs "joe", your code will crash with an unhandled exception (System.FormatException). Either wrap the Parse statement inside a try-catch clause, or use Int32.TryParse().

    - Also, you aren't testing for -999 inside the while statement, which means at least one instance in which the user inputs -999 will be accepted.

    Code:
    while (myNumber != -999) {
        Console.Write("Enter value : " );
        if (int.TryParse(Console.ReadLine(), myNumber) && myNumber != -999) // C# supports short-circuit evaluation, no harm done
            sum = sum + myNumber;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    This sounds like homework or a programming test.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Ireland
    Posts
    13
    Just cycling through past exams papers and came across it...
    not homework

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error in code
    By cole in forum C Programming
    Replies: 6
    Last Post: 09-26-2008, 09:51 PM
  2. What error in this code?Help me!
    By KySiRo in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2006, 02:45 PM
  3. error with code
    By dayknight in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2004, 01:43 PM
  4. Icq: error code 0
    By hermit in forum Tech Board
    Replies: 4
    Last Post: 04-24-2003, 09:43 PM
  5. i have an error in this code help?
    By ssjnamek in forum C++ Programming
    Replies: 12
    Last Post: 01-26-2002, 09:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21