Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on an arithmetic program, and i'm trying to make it validate the user inputs than do the math in a separate class. Problem is I can't get the ints to the arithmetic class after validation and i'm not sure why. Here is the code.
    static void Main(string[] args)
        {
            

            Console.WriteLine("Enter the first integer");
            string numberOne = Console.ReadLine();

            Console.WriteLine("Enter the second integer");
            string numberTwo = Console.ReadLine();

            Console.WriteLine("Enter the third integer");
            string numberThree = Console.ReadLine();

            Console.WriteLine("Enter the fourth integer");
            string numberFour = Console.ReadLine();

            Console.WriteLine("Enter the fifth integer");
            string numberFive = Console.ReadLine();


            Validate validate = new Validate();
            validate.ValidateMethod();
        }
    }
}


public void ValidateMethod()
       {
           string line = Console.ReadLine();
           int numberOne;
           int numberTwo;
           int numberThree;
           int numberFour;
           int numberFive;

           if (int.TryParse(line, out numberOne))
           {

               if (int.TryParse(line, out numberTwo))
               {

                   if (int.TryParse(line, out numberThree))
                   {
                       if (int.TryParse(line, out numberFour))
                       {
                           if (int.TryParse(line, out numberFive))
                           {

                               Arithmetic arithmetic = new Arithmetic();
                               arithmetic.ArithmeticMethod();

                           }
                       }
                   }
               }
           }


           else
           {
               Console.WriteLine("The entered integer is not valid.");
           }

       }


public class Arithmetic
  {
      public void ArithmeticMethod()
      {

          int numberOne;
          int numberTwo;
          int numberThree;
          int numberFour;
          int numberFive;

          numberOne = Convert.ToInt32(Console.ReadLine());

          numberTwo = Convert.ToInt32(Console.ReadLine());

          numberThree = Convert.ToInt32(Console.ReadLine());

          numberFour = Convert.ToInt32(Console.ReadLine());

          numberFive = Convert.ToInt32(Console.ReadLine());

          numberOne = Validate.numberOne;

           Sum(numberOne, numberTwo, numberThree, numberFour, numberFive);
           Average(numberOne, numberTwo, numberThree, numberFour, numberFive);
           Product(numberOne, numberTwo, numberThree, numberFour, numberFive);
           SmallestNumber(numberOne, numberTwo, numberThree, numberFour, numberFive);
           LargestNumber(numberOne, numberTwo, numberThree, numberFour, numberFive);


      }
      public static int Sum(int numberOne, int numberTwo, int numberThree, int numberFour, int numberFive)
      {
          int sum;
          sum = numberOne + numberTwo + numberThree + numberFour + numberFive;
          Console.WriteLine($"the sum of the integers is {sum}");
          return sum;
      }

      public static double Average(int numberOne, int numberTwo, int numberThree, int numberFour, int numberFive)
      {
          int average;
          int sum;
          sum = numberOne + numberTwo + numberThree + numberFour + numberFive;
          average = sum / 5;
          Console.WriteLine($"the average of the integers is {average}");
          return average;
      }

      public static int Product(int numberOne, int numberTwo, int numberThree, int numberFour, int numberFive)
      {
          int product;
          product = numberOne * numberTwo * numberThree * numberFour * numberFive;
          Console.WriteLine($"the product of the integers is {product}");
          return product;
      }


The specific error I get is "'Validate' does not contain a definition for 'number one'"

What I have tried:

things suggested from similiar questions on this site and others
Posted
Updated 23-Feb-20 10:02am

1 solution

You should study the notion of variable scope.
The variables you declare and assign in your Main method are not available to other methods.
You can have a look at my answer to your former question (Error CS7036 no argument corresponds to the required formal parameter[^]), which I edited to advise you a way to validate your inputs.
And your actual code is having the very same issue: you are caling methods but not storing their results. I can only suggest that you go back to a beginner's tutorial on methods, return types, variables, scopes, etc., because what you have now is showing that you are lacking some very essential concepts.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900