Jump to content

use of unassigned local variable

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

Stupid issue, i cant remember, I am brushing up on my C# so, if anyone can figure out what am I doing wrong and tell me that would be great.

Just a simple calculator which the equations with the variables in a different class called equations (sorry naming convention sucks). but it appears the error message on the answer1, what I want to do is have the user type in a for add, s for subtract etc etc, so i decided to use while loop until user enters correct letter, also how do you set it to where the user can enter a or A, s or S etc?

Thank you


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;


namespace practicecalc

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                equations equat = new equations();

                string answer1;

                while (answer1 != "a" || answer1 != "s" || answer1 != "d" || answer1 != "m")

                {

                    Console.WriteLine("type a for Add, s for Subtract, d for Divide, m for Multiply");

                    answer1 = Console.ReadLine();

                }

                Console.WriteLine("Enter the first number");

                equat.Num1 = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Enter the second number");

                equat.Num2 = Convert.ToInt32(Console.ReadLine());

                if (answer1 == "a")

                {

                    Console.WriteLine("Your Answer is " + equat.addvalues(equat.Num1, equat.Num2));

                    Console.ReadKey();

                }

                if (answer1 == "s")

                {

                    Console.WriteLine("Your Answer is " + equat.subtractvalues(equat.Num1, equat.Num2));

                    Console.ReadKey();

                } 

                if (answer1 == "d")

                {

                    Console.WriteLine("Your Answer is " + equat.dividevalues(equat.Num1, equat.Num2));

                    Console.ReadKey();

                }

                if (answer1 == "m")

                {

                    Console.WriteLine("Your Answer is " + equat.multiplyvalues(equat.Num1, equat.Num2));

                    Console.ReadKey();

                }

            }

            catch(Exception )

            {

                MessageBox.Show("DO IT RIGHT IDIOT!! IT SAID NUMBERS, HOW SIMPLE CAN YOU GET!!");

            }

        }

    }

}



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would set answer1="" before entering your while. You can also add more options for the uppercase values to your while condition and if statements.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

I was looking over my old posts from last year (weird of me saying that) and wondering if I could use .ToLower for the answer1? example: answer1.ToLower?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
That would probably work, too.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog