PDA

View Full Version : [C] Calculator help.

dBx
04-26-2009, 08:50 PM
//Calculator written in C
//Coded by dBx @ h4ckinab0x
#include <stdio.h>
#include <conio.h>

int main()
{

printf("On Your Marks. Get Set. Calculate!\n\n");
printf("Press [2] for Subtraction\n");
printf("Press [3] for Multiplication\n");
printf("Press [4] for Division\n");
getch();

if("choice==1")

{

int num1;
int num2;
int sum;

scanf("%d", &num1);
scanf("%d", &num2);

sum = num1 + num2;

}

else if("choice==2")

{

int num1;
int num2;
int sub;

scanf("%d", &num1);
scanf("%d", &num2);

sub = num1 - num2;

}

else if("choice==3")

{

int num1;
int num2;
int multi;

scanf("%d", &num1);
scanf("%d", &num2);

multi = num1 * num2;

}

else if("choice==4")

{

int num1;
int num2;
int dvs;

scanf("%d", &num1);
scanf("%d", &num2);

dvs = num1 / num2;

printf("The Answer is %d!", dvs); }
getch();
}

What am i doing wrong...when i compile it,
the exe shows up. but the functions dont work.
i think i have to set them as for example;
sub = subtraction;
etc.
also i think i should use switch/case statements.
or do while loops..
but im having trouble with it ]:

oracleguy
04-26-2009, 11:05 PM
I don't see how this code could have compiled at all. You are manipulating variables you never declared.

And if statements like this:

if("choice==1") are always going to return true because it will evaluate the address of that string literal as a boolean. And since no string literal will have an address of zero, it will always be true.