numbers printed to console not the ones I entered

rbalbat

New member
Joined
Aug 25, 2018
Messages
3
Programming Experience
Beginner
I wrote a simple program that prompts user for 5 numbers, I add them to a list, then I use a for loop to print each element (integer) in the List. The problem is the numbers printed are not the ones I entered. Seems 28 was added to each number. Not sure why.

Any help is appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sort_5_numbers_in_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            // 3- Write a program and ask the user to enter 5 numbers. If a number has been previously entered, display an error message
            // and ask the user to re-try. Once the user successfully enters 5 unique numbers, sort them and display the result on the console.
            var numbers = new List<int>();
            string input;
            int totalenteredthusfar = numbers.Count;
            while (totalenteredthusfar < 5)
            {
                Console.WriteLine("totalenteredthusfar: " + totalenteredthusfar);
                Console.WriteLine("Please enter a unique number");
                input = Console.ReadLine();
                //Convert string to integer
                int num = Convert.ToInt32(input);
                totalenteredthusfar = numbers.Count;
                
                //Console.WriteLine("numbers[0] is still: " + numbers[0]);
                for (int i = 0; i < 6; i++)
                {
                    if (totalenteredthusfar != 0 && numbers[i] == num)
                    {
                        Console.WriteLine("Please don't enter duplicates numbers!!: ");
                        break;
                    }
                    else
                    {
                        numbers.Add(num);
                        Console.WriteLine("Number was just added to List ");
                        totalenteredthusfar = numbers.Count;
                        break;
                    }
                }
            }
            //numbers.Sort();
            Console.WriteLine("The numbers you entered were: ");
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(numbers[i] + ' ');
            }
            // Added these 2 lines to keep cmd window from closing before I can see response.
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }
    }
}
 
Don't just read your code. That's a first step but if nothing obvious jumps out then the ne4xt step is to debug it, i.e. set a breakpoint and then step through the code line by line, testing the state at each step. You must know exactly what you expect to happen at each step and you can check that what you expected actually did happen. As soon as you detect an unexpected state, you have found an issue and you can investigate that specifically. If you don;t know how to debug, now is the time to learn. It's a vital skill for any developer. Can you imagine trying to find a bug simply by reading code when that code is thousands or millions of lines?
 
That said, you can see potential candidates simply by reading the code. You said that it seems like 28 is being added to each number. Are there any places that you're using an addition operator in your code? Wouldn't they be immediate candidates for investigation? Consider the fact that an operator may behave differently based on the data types of the operands. For instance, would you expect string concatenation to occur when neither operand of an addition operator is a string?
 
Back
Top Bottom