Thread: Number Convert function

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    35

    Number Convert function

    Write a function int convert () that reads a decimal number one character(digit) at a time, terminated by a blank, and returns the value of that number.
    I am trying to write program for above
    Code:
    #include <stdio.h>
    int main() 
    {  
    int number=0;    
    int digit=0;   
    while(digit != ' ')   
    {     
    printf("Enter the digit\n");     
    scanf("%d,&digit");     
    number = number*10 + digit;   
    }   
    printf("The entered number is=%d ",number);    
    return 0;
    }
    The code does not exit the while condition.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:9:9: warning: format ‘%d’ expects a matching ‘int *’ argument [-Wformat=]
        9 | scanf("%d,&digit");
          |        ~^
          |         |
          |         int *
    $
    Do you see what's wrong here?
    Code:
    scanf("%d,&digit");
    Compare with
    Code:
    scanf("%d",&digit);
    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.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    35
    I am really sorry how that mistake happened. I will try with the changes.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    35
    Code:
    #include <stdio.h>
    int main()
    {
        int number=0;
        char digit=0;
        
        while(1)
        {
         printf("Enter the digit\n");
         scanf("%c",&digit);
         getchar();
         printf("digit = %d\n", digit-'0');
         if(digit == ' ')
         {
             printf("Space entered\n");
             break;
         }
         else
         {
           number = number*10 + (digit-'0');
           printf("The entered number is = %d\n",number);
         }
         
        }
        printf("The final number is = %d ",number);
        return 0;
    }
    Made a working version but little complex.
    Enter the digit
    3
    digit = 3
    The entered number is = 3
    Enter the digit
    5
    digit = 5
    The entered number is = 35
    Enter the digit
    8
    digit = 8
    The entered number is = 358
    Enter the digit

    digit = -16
    Space entered
    The final number is = 358


    ...Program finished with exit code 0
    Press ENTER to exit console.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    > if(digit == ' ')
    Do this.

    Before you do this
    > printf("digit = %d\n", digit-'0');

    or this
    > number = number*10 + (digit-'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
    Apr 2019
    Posts
    35
    Code:
    #include <stdio.h>
    int main()
    {
        int number=0;
        char digit=0;
        
        while(1)
        {
         printf("Enter the digit\n");
         scanf("%c",&digit);
         getchar();
         if(digit == ' ')
         {
             printf("Space entered\n");
             break;
         }
         else
         {
           number = number*10 + (digit-'0');
           printf("digit = %d\n", digit-'0');
           printf("The entered number is = %d\n",number);
         }
         
        }
        printf("The final number is = %d ",number);
        return 0;
    }
    I hope this is what you mean?
    Enter the digit
    1
    digit = 1
    The entered number is = 1
    Enter the digit
    2
    digit = 2
    The entered number is = 12
    Enter the digit

    Space entered
    The final number is = 12


    ...Program finished with exit code 0
    Press ENTER to exit console.

  7. #7
    Registered User
    Join Date
    May 2020
    Posts
    23
    Instead of stopping the loop with the break statement, why not replace "while (1)" with "while (digit != ' ')"?

  8. #8
    Registered User
    Join Date
    Apr 2021
    Posts
    148
    Quote Originally Posted by Clearner123 View Post
    Write a function int convert () that reads a decimal number one character(digit) at a time, terminated by a blank, and returns the value of that number.
    I'm not seeing a function called convert that returns an int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert a binary number into a decimal number
    By HTHVampire in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2013, 09:53 AM
  2. Convert 8-digit Binary Number to decimal number
    By yongsheng94 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2013, 09:47 AM
  3. Shorter way to convert number to txt
    By someone_stupid in forum C Programming
    Replies: 6
    Last Post: 11-01-2009, 10:52 AM
  4. Replies: 6
    Last Post: 07-15-2008, 02:27 PM
  5. How can i convert negative number to positive number ?
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 08:02 AM

Tags for this Thread