Question:
What is the problem in this program?
BadSector
2011-07-17 07:26:08 UTC
What is the problem in this program?

using System;
using System.Collections.Generic;
using System.Text;

namespace Simple_Calculator
{
class Program
{
static void Main(string[] args)
{
float a, b, c;
char op;

Console.WriteLine("Select Operator:");
op = Convert.ToChar(Console.ReadLine());

Console.WriteLine("Enter Two Numbers:");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());

if (op = '+')
{
c = a + b;
Console.WriteLine("The Addition of two num are : +c");

}
else if (op = '-')
{
c = a - b;
Console.WriteLine("the Subtraction of two Numbers are = +c");
}
else if (op = '*')
{
c = a * b;
Console.WriteLine("The Multiplication of two Numbers are = +c");
}
else if (op = '/')
{
c = a / b;
Console.WriteLine("The division of two Numbers are = +c");
}
Console.ReadLine();
}
}
}
Four answers:
doug
2011-07-17 07:51:39 UTC
Not sure about the language either, but I think it's C#. Including error messages and more details would help, but I think I can make a guess here:



Console.WriteLine("The division of two Numbers are = +c");



You want concatenation here I suppose: Console.WriteLine("The division of two Numbers are " + c)



If '+' is the correct concatenation operator. And if it is OK to combine a string and a double like that in what I assume is C#.



And your first if statement will be true every time, none of the others will be evaluated



if (op = '+')



instead



if(op == '+')



http://en.wikipedia.org/wiki/C_Sharp_syntax#Operators



== is the comparison operator

= is assignment



so,



if (op = '+') is true if the assignment operation is successful, which it certainly will be.
Silent
2011-07-17 14:50:29 UTC
The problem is that you're trying to use the = operator to compare values.



In C#, the = operator is used to assign a value to a variable. The == operator is used to compare two values.
greigmcl
2011-07-17 14:32:41 UTC
I don't quite recognise the language (if you can tell us that would probably get a more accurate answer), but the Main function looks wrong to me - have you tried

static void main (string[] args)

with a lower case m?
?
2011-07-17 14:54:49 UTC
you need to change how you are writing the output. example:

Console.WriteLine("The Addition of two num are : {0}", c);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...