Thread: whats wrong with this simple calculator

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    4

    whats wrong with this simple calculator

    Code:
    #include <stdio.h>
    
    int main(void){
        int fnumber,snumber;
        printf("enter fnumber=");
        scanf("%d",&fnumber);
        printf("enter snumber=");
        scanf("&d",&snumber);
        printf("total=%d",fnumber+snumber);
        return 0;
    }
    when i enter a fnumber, it proceeds immediately to total without asking for snumber.
    Attached Images Attached Images whats wrong with this simple calculator-untitled-png 

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    Look more closely at your second scanf format.
    Also, you should really write a newline at the end of a line of output.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  3. #3
    Banned
    Join Date
    Jul 2022
    Posts
    112
    scanf() typo '&'.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,011
    Quote Originally Posted by jph View Post

    when i enter a fnumber, it proceeds immediately to total without asking for snumber.
    Turn on warnings, and turn up to the highest level. The compiler would show you the error.

  5. #5
    Registered User
    Join Date
    Nov 2022
    Posts
    4

    Big thanks

    Quote Originally Posted by john.c View Post
    Look more closely at your second scanf format.
    Also, you should really write a newline at the end of a line of output.
    Thanks for this!

  6. #6
    Registered User
    Join Date
    Nov 2022
    Posts
    4

    Big thanks

    Quote Originally Posted by kodax View Post
    scanf() typo '&'.
    Thanks for this!

  7. #7
    Registered User
    Join Date
    Nov 2022
    Posts
    4

    Thanks for the tip!

    Quote Originally Posted by rstanley View Post
    Turn on warnings, and turn up to the highest level. The compiler would show you the error.
    Will definitely set that. Thanks!

  8. #8
    Registered User
    Join Date
    Apr 2021
    Posts
    103
    Pro tip: sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong in this simple program
    By san12345 in forum C Programming
    Replies: 3
    Last Post: 12-14-2015, 07:50 AM
  2. simple calculator: what's wrong with this code?
    By zer0_r00t in forum C Programming
    Replies: 14
    Last Post: 06-01-2014, 09:13 PM
  3. whats wrong with this very simple code?
    By sway1 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 12:18 PM
  4. Whats wrong with this simple code?
    By o14v in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 01:46 PM
  5. Whats Wrong in My simple program?
    By MartinLiao in forum C Programming
    Replies: 1
    Last Post: 09-02-2004, 02:30 AM

Tags for this Thread