...

View Full Version : [C] Calculator help.



dBx
04-26-2009, 07: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 [1] for Addition\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;

printf("Enter Your Number:\n");

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

sum = num1 + num2;

printf("The Answer is %d!", sum);

}

else if("choice==2")

{

int num1;
int num2;
int sub;

printf("Enter Your Number:\n");

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

sub = num1 - num2;

printf("The Answer is %d!", sub);

}

else if("choice==3")

{

int num1;
int num2;
int multi;

printf("Enter Your Number:\n");

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

multi = num1 * num2;

printf("The Answer is %d!", multi);

}

else if("choice==4")

{

int num1;
int num2;
int dvs;

printf("Enter Your Number:\n");

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, 10: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum