# Thread: C Calculator Problem

1. ## C Calculator Problem

hi, every1, my name is ShadeS, i'm new here and this is my first post.. hope that we'll get along with each other soon.. thx...

uhmm.. i made a calculator program in c, but there's something wrong.. here's the code:

Code:
```#include<stdio.h>

main()
{
int num1, num2, num3;

clrscr();

num1 = 0;
num2 = 0;
num3 = num1 + num2;

printf("Enter number: ");
scanf("%d", &num1);

printf("\nEnter another number: ");
scanf("%d", &num2);

printf("\n\nThe answer is: %d", &num3);

getch();
}```
..i dnt know exactly what's wrong with it, but it seems that im using the printf() and scanf() functions incorrectly.. this is jus a basic approach, so yeah, i'm a beginner... if any1 would post a complete code for a simple/basic calculator, that is, a calculator that would perform simple mathematical functions such as addition, subtraction, multiplication, and division... i would really appreciate it.. thanks in advance for helping... peace out, yo... -_+

2. Hello!
I'm sort of a beginner as well, but can see what is your problem. You correctly initialize the num values with zero. and then:
Code:
`num3 = num1 + num2`
which means that num3 holds the value of 0. However, after this, the value of num3 is not changed anywhere, and that's why it stays as 0.

Also
Code:
`printf("\n\nThe answer is: %d", &num3);`
should be like:
Code:
`printf("\n\nThe answer is: %d", num3);`
These two solutions would work:
Code:
```#include<stdio.h>

main()
{
int num1=0, num2=0, num3=0;

clrscr();

printf("Enter number: ");
scanf("%d", &num1);

printf("\nEnter another number: ");
scanf("%d", &num2);

num3=num1+num2;

printf("\n\nThe answer is: %d", num3);

return 0;
}```
or

Code:
```#include<stdio.h>

main()
{
int num1=0, num2=0;

clrscr();

printf("Enter number: ");
scanf("%d", &num1);

printf("\nEnter another number: ");
scanf("%d", &num2);

printf("\n\nThe answer is: %d", num1+num2);

return 0;
}```
Hope this helps!

Spiros

3. A few other notes:
• clrscr() is non-standard, and you don't really need it anyway. Do you?
• getch() is non-standard as well. You can use something like this to keep your console window open if you like:
Code:
`while(getchar() != '\n');`
If you insist upon using getch(), at least #include <conio.h>, where it resides.
• As s_siouris has shown, main() ought to return 0. main() should also be declared as "int main()", not "main()"; the implicit int rule, whereby a function with an unspecified return type defaults to "int", is deprecated.

 Just to expand a little: the variables num1 and num2 only get the values that you enter into them at the scanf calls. Before that, they contain 0. When you go "num3 = num1 + num2", you're taking the current values of num1 and num2. If those variables should change later on, num3 will not be affected. Therefore, you need to do your addition after num1 and num2 have the values that you entered, i.e., after the scanf() calls.

Of course, you also have to do it before the printf() call, because printf() will be examining the value of num3 as it appears at that point in the program. [/edit]

4. hi

In ShadeS_07 example I add the conio.h to the code but still I get this warning

Code:
`warning C4013: 'clrscr' undefined; assuming extern returning int`
I'm using VC++ 6.0
Is it because Microsoft version of conio.h file doesn't contain any definition for the
clrscr() ???

5. Yup, probably. http://www.computing.net/answers/pro...c-60/4522.html

A simple alternative is to use system("cls") instead.

Better yet, don't bother. Why do you need to clear the screen right after your program starts, anyway? Because you're using getch() to pause the console, you're probably running it from Windows or from MSVC. So there will never be anything on the console to begin with anyway.

 Sorry, didn't notice you weren't the OP. [/edit]

6. Is it because Microsoft version of conio.h file doesn't contain any definition for the
clrscr() ???
Maybe, but since you do not need clrscr() and getch(), you might as well remove them and thus not have to bother with conio.h.

7. ## Thank you for the help and advise... ^.^

thanks evry1 for your help and advise.. i really appreciate it.... thank you so much.. ^.^

Popular pages Recent additions