Question

I am trying to make a simple program that asks the user to enter an integer. Once the program receives the input it takes and stores it and then counts from 1 to the input integer and sums the total of the count. Then it displays the results in a meaningful way to the user and prompts them if they would like to process another number. The point of this program is to use loops and multiple classes. I know that I am really close to the desired end product but cannot figure out why the AccumulateValue() method is not working properly. It does not seem to be going into the conditional while statement that I made. If anyone could give me some insight to my problem that would be great!

Here is my code:

AccumulatorApp.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    class AccumulatorApp
    {


        static void Main(string[] args)
        {
            string loopControl = "Y";
            int sum;
            int enteredValue;
            DisplayTitle();

            while (loopControl == "Y" || loopControl == "YES")
            {
                enteredValue = InputInteger(0);
                Accumulator number = new Accumulator(enteredValue);
                sum = number.AccumulateValues();
                DisplayOutput(sum, enteredValue);
                Console.Write("\tWould you like to process another number? \n\t\t<Y or N>: ");
                loopControl = Console.ReadLine().ToUpper();
            }

        }


        public static void DisplayTitle()
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            Console.Clear();
            Console.WriteLine();
            Console.WriteLine("\tProgramming Assignment 05 - Accumulator - Robert");
            DrawLine();

        }


        public static int InputInteger(int enteredValue)    
        {

            Console.Write("\tPlease enter a positive integer: ");
            enteredValue = Convert.ToInt32(Console.ReadLine());
            if (enteredValue > 0)
            {
                return enteredValue;
            }
            else 
            {
                Console.WriteLine("\tInvalid input. Please enter a POSITIVE integer: ");
                enteredValue = Convert.ToInt32(Console.ReadLine());
            }
            return enteredValue;


            /*
            Console.Write("Please enter a positive integer: ");
            int enteredValue = Convert.ToInt32(Console.ReadLine());
            return enteredValue;
             * */
        }


        public static void DisplayOutput(int sum, int inputValue)
        {
            Console.WriteLine("\tThe inputed integer is: {0}", inputValue);
            Console.WriteLine("\tThe sum of 1 through {0} = {1}", inputValue, sum); 
            DrawLine();
        }


        public static void DrawLine()
        {
            Console.WriteLine("\t______________________________________________________");
            Console.WriteLine();
        }

    }
}

Accumulator.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    class Accumulator
    {
        int integerEntered; 

        public Accumulator()
        {
        }

        public Accumulator(int integerEntered)
        {
            int enteredInteger = integerEntered;
        }

        public int AccumulateValues()
        {
            int accumulatedValue = 0;
            int counterValue = 1;
            while (counterValue <= integerEntered)
            {
                Console.WriteLine("\tPasses through loop = {0}", accumulatedValue);
                accumulatedValue = accumulatedValue + counterValue;
                counterValue = counterValue + 1;
            }
            return accumulatedValue;
        }

    }
}
Was it helpful?

Solution

When you are instantiating a new instance of Accumulator through it's constructor containing one int argument you were setting the passed value equal to the field within the class (Setting them both to 0.)

Your accumulator class should look like this:

class Accumulator
{
    int integerEntered;

    public Accumulator()
    {
    }

    public Accumulator(int passedInteger)
    {
        //Local field is equal to passedInteger, not the other way around.
        integerEntered = passedInteger;
    }

    public int AccumulateValues()
    {
        int accumulatedValue = 0;
        int counterValue = 1;
        while (counterValue <= integerEntered)
        {
            Console.WriteLine("\tPasses through loop = {0}", accumulatedValue);
            accumulatedValue = accumulatedValue + counterValue;
            //Increment does the same thing you were doing
            counterValue++;
        }
        return accumulatedValue;
    }

}

OTHER TIPS

It looks like the problem may actually be with your value constructor. When this line is called: Accumulator number = new Accumulator(enteredValue);

A new Accumulator is being made with your value constructor:

public Accumulator(int integerEntered)
{
    int enteredInteger = integerEntered;
}

The problem is that integerEntered isn't really saved anywhere and once enteredInteger goes out of scope (end of constructor), the value that was entered is essentially lost as far as the Accumulator object is concerned. I think what you want is:

public Accumulator(int integerEntered)
{
    integerEntered = integerEntered;
}

As a heads up, you may have to do this.integerEntered = integerEntered; Also I think you want to subtract 1 from integerEntered each iteration of your while loop in AccumulateValues().

There 2 -3 things needs to be changed

1) you are not assigning values to integerEntered in your constructor so I have changed it

2) you should integerEntered as a property so i have changed it to public int integerEntered { get; set; }

3) the logic of calculating to count AccumulateValues .. actually mathematical formula is the sum up to integer n is = (n * (n+1))/2 so i have changed it too

try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    class Accumulator
    {

        public int integerEntered { get; set; }     

        public Accumulator()
        {
        }

        public Accumulator(int integerPassed)
        {
            integerEntered = integerPassed;
        }

        public int AccumulateValues()
        {
            int accumulatedValue = 0;
            if(integerEntered > 0)
            {
                accumulatedValue = (integerEntered * (integerEntered + 1))/2;
            }
            return accumulatedValue;

        }

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top