Home > Back-end >  Moving int from loop to the outside of the loop c#
Moving int from loop to the outside of the loop c#

Time:01-08

I am new to programming and I'm trying to make "calculator".. sort of..

Is there any simple way to move the "Y"s from the loop and add them up and print them outside of the loop?

Screenshot of source code

CodePudding user response:

For future reference, please paste code and format as a code block so it's easier for people to help you.

The easy way to get the values entered outside of the for loop is with a list. You need to declare the variable outside of the loop so that it's scope extends beyond the loop itself, then you can either add your Y to the list or parse directly into the list:

var numbers = new List<int>();
for(int i=1; i<=X; i  )
{
    Console.Write("...");
    numbers.Add(int.Parse(Console.ReadLine()));
}
// Do stuff with "numbers" here.

Do be aware that the solution you are linking to is not great, there is no error checking to ensure the value entered is an int for example (lookup int.TryParse()) and asking how many numbers they want to sum is kind of a waste, if they want to enter one number they just get that number back, no big deal. Also, if you are just attempting to add numbers there is not need to "move the Y's from the loop", you don't care about each individual Y, just about the sum, then you can simplify:

var sum = 0;
for(int i=1; i<=X; i  )
{
    Console.Write("...");
    sum  = int.Parse(Console.ReadLine());
}
// Do stuff with "sum" here.

CodePudding user response:

It's not quite clear what your asking for but... if you want to take number of iterations from the user and then sum the numbers and print the result back after the loop finishes you could do this:

  Console.WriteLine("Enter count of numbers you want to sum...");
        int numberOfIterations = int.Parse(Console.ReadLine());


        while (numberOfIterations <= 1)
        {
            Console.WriteLine($"{numberOfIterations} is too small. Enter positive number or above 1");
            numberOfIterations = int.Parse(Console.ReadLine());
        }

        int sum = 0;

        for (int i = 0; i < numberOfIterations; i  )
        {
            Console.WriteLine("Write the number you want to add...");
            bool isNumber = int.TryParse(Console.ReadLine(), NumberStyles.None, CultureInfo.InvariantCulture,
                out int parsedNumber);
            if (isNumber)
            {
                sum  = parsedNumber;
            }
        }

        Console.WriteLine($"Sum of all numbers is: {sum}");

So you said that you are new to programming thats why I will try to explaing the code as simple as I can: 1.Ask the user how manu iteration does he want 2.Make some simple defenese check with while loop till he enters correct number for iterations...bigger than 1..if not print message and ask him again until he does enter correct number. 3. Define and initialize variable for the sum before entering the loop which will sum the values provided. 4.Create for loop which will do count of iterations that were input by the user. 5.Ask user for number. 6.Check if number is int type with int.TryParse method....user can type anything in the console(sfsd,345,333.333,###$%#$%#) which will fail. TryParse returns bool and if the input was correct in our case "isNumber" then we proceed to summing the input. 7. After loop end print the sum on the console...

  •  Tags:  
  • Related