-1

So this is a very simple code that i can for the love of me get right because of some small mistake but the point here is the first int the user gives is the amount of numbers you will give after it so i have made this but for some reason the program wants 1 more int than it should. Lets say i enter 3 at first. That should mean that it would take 3 ints after that and then print them and end the program but fore some reason if i enter 3 after that it wants 4. if i enter 4 after that it wants 5 and you get the point. I have tried changing the whole code but i think the problem is that i am just making it to long and to complicated. here is the code

    using System;

namespace Homework_PrPr
{
    class Program
    {
        static void Main(string[] args)
        {
            int ammount = int.Parse(Console.ReadLine());
            int Enters = int.Parse(Console.ReadLine());
            int[] numbers = new int[ammount];
            for(int i = 0; i < ammount; i++)
            {
                numbers[i] = Enters;
                Enters = int.Parse(Console.ReadLine());

            }
            int ammountEntered = 0;
            while(ammountEntered < ammount)
            {
                Console.WriteLine(numbers[ammountEntered]);
                ammountEntered++;
            }
        }
    }
}
Umseizure
  • 63
  • 5
  • 1
    well, you have twice `Console.ReadLine` before you even iterate your loop. Then when iterating you overwrite that value again and again. However I have no idea what `Enters` should be. – MakePeaceGreatAgain Oct 18 '22 at 13:39
  • 5
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Oct 18 '22 at 13:39
  • you are requesting 1 input number before you get in the loop. So if you enter 3 for the `ammount` variable, it will accept 4 inputs but only the first 3 will be put in the `numbers` array. And then it should only print out 3 numbers. So why not just remove the first line of `int Enters = int.Parse(Console.ReadLine());` then swap the order of those two lines in the `for` loop. – frankM_DN Oct 18 '22 at 13:40

2 Answers2

2
int ammount = int.Parse(Console.ReadLine());
int Enters = int.Parse(Console.ReadLine()); 

Take a moment to think about this. If the user enters amount = 1, what would Happen? You would first ask a number, then enter the loop one time, where it asks for another number. Why is it asking for another number in this case? Why not just skip the Enter variable and assign numbers inside the loop?

for(int i = 0; i < ammount; i++)
{
    numbers[i] = int.Parse(Console.ReadLine());
}

I would highly recommend reading eric lipperts article on How to debug small programs, since problems like this should be very easy to diagnose when stepping thru a program. You might also consider reading the coding convensions guide, local variables should for example use "camelCasing".

You might also consider using a foreach loop when printing variables:

foreach(var number in numbers){
    Console.WriteLine(number);
}
JonasH
  • 28,608
  • 2
  • 10
  • 23
1

I think i understand your code!

This is how i would do it, and do read my explanation down below aswell!

using System;

namespace Homework_PrPr
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many numbers do you want to add to your array?");
            int amount = int.Parse(Console.ReadLine());
            int[] numbers = new int[amount];
            for (int i = 0; i < amount; i++)
            {
                Console.WriteLine($"Numer {i+1}/{amount}"); // Just tells the user what I of number their adding. aka unessecary
                int Enters = int.Parse(Console.ReadLine());
                numbers[i] = Enters;
            }
            Console.WriteLine("\n");

            int ammountEntered = 0;
            while (ammountEntered < amount)
            {
                Console.Write($"Number {ammountEntered + 1}/{amount}  :  ");
                Console.WriteLine(numbers[ammountEntered]);
                ammountEntered++;
            }
        }
    }
}

The first problem was that you asked for a unnesecary ReadLine in the beggining, so that i removed! Then inside you for-loop i switched the position of the adding, aswell as the one asking for a new number. I also added a few counters so that you can see a bit more in detail what the code does!

21sili
  • 35
  • 9