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.
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(); } } }