user122876 user122876 - 22 days ago 4
C# Question

C# IF/ELSE statemenets

I need help with some of my code for a game calculator. So I wrote all this code, but the IF/THEN statement is acting weird.

Screenshot: https://gyazo.com/490c221cc8cc35b2c102831244eaab1b -
I typed in P and it should have went to the ELSE part of the code, but instead it continued onto the if part. Please help!

{
class MainClass
{
public static void Main(string[] args) // IF YOU ARE TO REWRITE FROM MY SOURCE, ALL ORIGINAL CREDITORS MUST GO INTO THE CREDITS!
{

double num01;
double num02;
double num03;
double num04;
double num05;
double num06;
string CD = null;
string P = null;
string answer = null;



Console.Write("Diogenes's Calculator 1.0\n\nCredits: DoS (#57714)\n DZ(#54689)");
Console.WriteLine();
Console.WriteLine();


Console.Write("Hello! Would you like Charity Donation or Propaganda Calculator? (CD or P): ");
Console.ReadLine();
answer = Convert.ToString();
if(answer == CD) {
Console.WriteLine();
Console.Write("Howmuch influence does the target have?: ");
num01 = Convert.ToDouble(Console.ReadLine());
Console.Write("Howmuch is the cost of Charity Donatins? (Gold): ");
num02 = Convert.ToDouble(Console.ReadLine());
Console.Write("What % of influence does Charity Donation give (Made if value does change)?: ");
num03 = Convert.ToDouble(Console.ReadLine());
Console.Write(num02 + num03);
Console.ReadKey();

} else if(answer == P) {
answer = Convert.ToString();
Console.WriteLine();
Console.Write("Howmuch influence does the target have?: ");
num04 = Convert.ToDouble(Console.ReadLine());
Console.Write("Howmuch influence do you want the target to have?: ");
num05 = Convert.ToDouble(Console.ReadLine());
Console.Write("What % of influence does Propaganda take off (Made if value does change)?: ");
num06 = Convert.ToDouble(Console.ReadLine());
Console.Write(num04 + num05);
Console.ReadKey();

} else {
Console.WriteLine("Looks like you didn't type in CD or P. Buh Bye!");
Console.ReadKey();
}



}
}
}

Answer

you are checking if answer is equal to the value stored in the field P, not if answer is equal to the actual string "P". And P the variable is null.

Secondly, you are throwing away your user input here:

Console.ReadLine();
answer = Convert.ToString();

you are reading the console input into nothing, then assigning answer to the Convert object's string representation, which is probably a fully qualified namespace. You want:

answer = Console.ReadLine();
Comments