Thread: Unable to accept input

  1. #1
    Registered User
    Join Date
    Jun 2021
    Posts
    2

    Question Unable to accept input

    Hell all, I have never experienced this issue before. I am unable to accept input for
    Code:
    divisor
    in the following code block:
    Code:
    #include<stdio.h>
    void divide(int dividend, int divisor, int *quotient, int *remainder)
    {
      *quotient = dividend / divisor;
      *remainder = dividend % divisor;
    }
    
    int main()
    {
      double dividend, divisor, quotient = 0;
      int remainder = 0;
      printf("Enter Dividend: ");
      scanf("&d", &dividend);
      printf("Enter Divisor:");
      scanf("&d", &divisor);
    
    }
    I have tried to but a blank space and a "\n" before %d on the divisor input but to no avail. This is very odd. The terminal accepts the value for dividend, prints "Enter divisor: " and just terminates like so:
    Code:
    Enter Dividend: 6
    Enter Divisor:
    C:\cProg>
    Any help appreciated.
    Last edited by Salem; 06-20-2021 at 12:51 AM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    scanf("&d", &dividend);
    You likely wanted
    Code:
    scanf("%d", &dividend);
    Notice I used "%d" instead of "&d".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    On top of that, your divide() function takes ints and int pointers, however, in main() they are declared as doubles, except for the int reminder.

    You need to be consistent.

  4. #4
    Registered User
    Join Date
    Jun 2021
    Posts
    2
    Quote Originally Posted by stahta01 View Post
    Code:
    scanf("&d", &dividend);
    You likely wanted
    Code:
    scanf("%d", &dividend);
    Notice I used "%d" instead of "&d".

    Tim S.
    My God after countless hours of coding, this is the biggest brain freeze I have experienced since...thank you kind sir.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Compiling with warnings enabled would have told you something was wrong from the start, without the "hours of coding".

    Code:
    $ cat foo.c
    #include<stdio.h>
    void divide(int dividend, int divisor, int *quotient, int *remainder)
    {
      *quotient = dividend / divisor;
      *remainder = dividend % divisor;
    }
    
    int main()
    {
      double dividend, divisor, quotient = 0;
      int remainder = 0;
      printf("Enter Dividend: ");
      scanf("&d", &dividend);
      printf("Enter Divisor:");
      scanf("&d", &divisor);
    
    }
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:13:9: warning: too many arguments for format [-Wformat-extra-args]
       scanf("&d", &dividend);
             ^
    foo.c:15:9: warning: too many arguments for format [-Wformat-extra-args]
       scanf("&d", &divisor);
             ^
    foo.c:11:7: warning: unused variable ‘remainder’ [-Wunused-variable]
       int remainder = 0;
           ^
    foo.c:10:29: warning: unused variable ‘quotient’ [-Wunused-variable]
       double dividend, divisor, quotient = 0;
                                 ^
    $
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Shoudn't be %f instead of %d?

  7. #7
    Registered User
    Join Date
    Apr 2021
    Posts
    23
    Quote Originally Posted by flp1969 View Post
    Shoudn't be %f instead of %d?
    Nope, everything should be an int here, especially since % is used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i make my readline function accept empty input?
    By kuasimikua in forum C Programming
    Replies: 2
    Last Post: 10-27-2020, 08:43 PM
  2. Replies: 3
    Last Post: 04-07-2016, 06:49 AM
  3. Can a pointer accept user input?
    By Programmer_P in forum C++ Programming
    Replies: 24
    Last Post: 07-31-2009, 10:16 AM
  4. Replies: 8
    Last Post: 02-14-2009, 11:33 PM
  5. How to accept input in graphics mode
    By nurulsiddik in forum C Programming
    Replies: 1
    Last Post: 02-13-2002, 10:40 AM

Tags for this Thread