C Calculator Problem

This is a discussion on C Calculator Problem within the C Programming forums, part of the General Programming Boards category; hi, every1, my name is ShadeS, i'm new here and this is my first post.. hope that we'll get along ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool 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. #2
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    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. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.


    [edit] 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]
    Last edited by dwks; 07-09-2008 at 12:26 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    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. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.

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

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Talking 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 subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21