i dont know why no one want to answer this question right for me?

 Mahmoud Abdel Ghani · February 3, 2015 actually i made a calculator using class and functions,i created class and inside it functions...function for get numbers and function for summation..etc..okay...now the user made some operation and he want to make another operation with last result..like 1+1=2..and i want to make another operation by using the last result like...2+3=5....so how to return the answer to continue??

Replies

- page 1

 Gabriel Marius Popescu · February 23, 2015 @Mahmoud Abdel GhaniHere is the code you wanted! The user can choose his / her numbers. Enjoy!Console.WriteLine("Select the numbers you want to " + choice);                        int num1 = Convert.ToInt32(Console.ReadLine());            int num2 = Convert.ToInt32(Console.ReadLine());            Console.WriteLine(" First number is " + num1 + " and second number is " + num2);                        int[] nums = new int[] { num1, num2 }; Mahmoud Abdel Ghani · February 10, 2015 @Gabriel Pirizit works good bro and thanks a lotfor your attention Gabriel Piriz · February 9, 2015 ``using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Project1{    //this is the class and his functions    class Calculator    {        float numb1;        float numb2;        public float answer;        public float lastAnswer = 0;        bool test1 = true;        bool test2 = true;        string first_input;        string second_input;        public void initalization(float x, float y, ref float ans)        {            numb1 = x;            numb2 = y;            answer = ans;        }        public void getnumbers()        {            while (test1)            {                Console.WriteLine("Please Enter first Number or ANS to use the last Result: ");                Console.WriteLine("Last Result Value: " + lastAnswer);                first_input = Console.ReadLine();                if (true == float.TryParse(first_input, out numb1))                {                    test1 = false;                }                else if (first_input.Equals("ANS"))                {                    test1 = false;                    numb1 = lastAnswer;                }                else                {                    test1 = true;                    Console.WriteLine("Error!-->please enter something numeric");                }            }            while (test2)            {                Console.WriteLine("Please Enter Second number or ANS to use the last Result: ");                Console.WriteLine("Last Result Value: " + lastAnswer);                second_input = Console.ReadLine();                if (true == float.TryParse(second_input, out numb2))                {                    test2 = false;                }                else if (second_input.Equals("ANS"))                {                    test2 = false;                    numb2 = lastAnswer;                }                else                {                    test2 = true;                    Console.WriteLine("Error!-->please enter something numeric");                }            }            test1 = test2 = true;        }        public float sum()        {            answer = numb1 + numb2;            Console.WriteLine("Answer = " + answer);            return answer;        }        public float subtract()        {            answer = numb1 - numb2;            Console.WriteLine("Answer = " + answer);            return answer;        }        public float divide()        {            if (numb2 == 0)            {                Console.WriteLine("You cannot divide by 0 answer is infinity");            }            else            {                answer = numb1 / numb2;                Console.WriteLine("Answer = " + answer);            }            return answer;        }        public float multiply()        {            answer = numb1 * numb2;            Console.WriteLine("Answer = " + answer);            return answer;        }    }    //this is the main................................................    class StupidThing    {        static void Main(string[] args)        {            string choice;            char con = 'y';            string continueornot;            bool testcontinue = true;            Calculator calculator = new Calculator();            while (con == 'y')            {                Console.Write("Select the operation that you to do(+,-,/,*):");                choice = Console.ReadLine();                switch (choice)                {                    case "+":                        calculator.getnumbers();                        calculator.sum();                        calculator.lastAnswer = calculator.answer;                        Console.WriteLine("Sumar");                        break;                    case "-":                        calculator.getnumbers();                        calculator.subtract();                        calculator.lastAnswer = calculator.answer;                        break;                    case "/":                        calculator.getnumbers();                        calculator.divide();                        calculator.lastAnswer = calculator.answer;                        break;                    case "*":                        calculator.getnumbers();                        calculator.multiply();                        calculator.lastAnswer = calculator.answer;                        break;                    default:                        Console.WriteLine("Error!-->please select one of shown operation!");                        break;                }                while (testcontinue)                {                    Console.WriteLine("Press y to continue or anykey to exit");                    continueornot = Console.ReadLine();                    if (char.TryParse(continueornot, out con))                    {                        testcontinue = false;                    }                    else                    {                        testcontinue = true;                        Console.WriteLine("Not Allowed!!! ");                    }                }            }        }    }}`` Mahmoud Abdel Ghani · February 9, 2015 @Gabriel Piriz bro in main you try to access a variable from class and this isnt allowed??? unerstand? Gabriel Piriz · February 9, 2015 Here's another solution. Didn't do anything complex, and it's not the best one you could make.(Made it in NotePad++ might have some mistakes at compiling or running time)``//this is the class and his functions class Calculator    {        float numb1;        float numb2;        float answer; float lastAnswer = 0;        bool test1 = true;        bool test2 = true;        string first_input;        string second_input;        public void initalization(float x, float y, ref float ans)        {            numb1 = x;            numb2 = y;            answer = ans;        }        public void getnumbers()        {            while (test1)            {                Console.Writeln("Please Enter first Number or ANS to use the last Result: "); Console.Writeln("Last Result Value: " + lastAnswer);                first_input = Console.ReadLine();                if (true == float.TryParse(first_input, out numb1))                {                    test1 = false;                }                else if (first_input = "ANS") { test1 = false; numb1 = lastAnswer; } else                {                    test1 = true;                    Console.WriteLine("Error!-->please enter something numeric");                }            }            while (test2)            {                Console.Writeln("Please Enter Second number or ANS to use the last Result: "); Console.Writeln("Last Result Value: " + lastAnswer);                second_input = Console.ReadLine();                if (true == float.TryParse(second_input, out numb2))                {                    test2 = false;                } else if (second_input = "ANS") { test2 = false; numb2 = lastAnswer; }                else                {                    test2 = true;                    Console.WriteLine("Error!-->please enter something numeric");                }            }        }        public float sum()        {            answer = numb1 + numb2;            Console.WriteLine("Answer = " + answer);            return answer;        }        public float subtract()        {            answer = numb1 - numb2;            Console.WriteLine("Answer = " + answer);            return answer;        }        public float divide()        {            if (numb2 == 0)            {                Console.WriteLine("You cannot divide by 0 answer is infinity");            }            else            {                answer = numb1 / numb2;                Console.WriteLine("Answer = " + answer);            }            return answer;        }        public float multiply()        {            answer = numb1 * numb2;            Console.WriteLine("Answer = " + answer);            return answer;        }    }//this is the main................................................  static void Main(string[] args)        {            string choice;            char con = 'y';                        string continueornot;            bool testcontinue = true; Calculator calculator = new Calculator();             while (con == 'y')            {                             Console.Write("Select the operation that you to do(+,-,/,*):");                choice = Console.ReadLine();                                switch (choice)                {                    case "+":                         calculator.getnumbers();                        calculator.sum(); calculator.lastAnswer = calculator.ans;                        break;                    case "-":                         calculator.getnumbers();                        calculator.subtract(); calculator.lastAnswer = calculator.ans;                        break;                    case "/":                                                calculator.getnumbers();                        calculator.divide();                        calculator.lastAnswer = calculator.ans; break;                    case "*":                                                calculator.getnumbers();                        calculator.multiply();                        calculator.lastAnswer = calculator.ans; break;                    default:                        Console.WriteLine("Error!-->please select one of shown operation!");                        break;                }                while (testcontinue)                {                    Console.WriteLine("Press y to continue or anykey to exit");                    continueornot = Console.ReadLine();                    if (char.TryParse(continueornot, out con))                    {                        testcontinue = false;                    }                    else                    {                        testcontinue = true;                        Console.WriteLine("Not Allowed!!! ");                    }                }            }        }`` Mahmoud Abdel Ghani · February 9, 2015 @Branislav Lazicit doesnt work bro it runs and ask me which operation you want and i choiced the operation and and ask me to insert the values and after it didnt do anything Abdullah Nauman · February 8, 2015 could i please have the build error. Mahmoud Abdel Ghani · February 8, 2015 @ Branislav Lazici think you didnt solve the problem and made everything static Abdullah Nauman · February 8, 2015 I have not done C# in 1 year, I might have done something strong. Could I please have the error message, and location. Mahmoud Abdel Ghani · February 8, 2015 its wrong bro
• 1
• 2

C#

131,284 followers