How to display output using puchar() only ???

This is a discussion on How to display output using puchar() only ??? within the C Programming forums, part of the General Programming Boards category; Hello , My program read in input using getchar() and then convert it into integer value. After converting into integer ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    21

    How to display output using puchar() only ???

    Hello ,

    My program read in input using getchar() and then convert it into integer value. After converting into integer value , i will have to convert it into hexadecimal.

    My question is : how to output the result (hexadecimal value) using only putchar() ???

    I tried to output it but invalid result appear instead. ( see source code )

    Code:
    #include<stdio.h>
    
    main()
    {
    
     int i,j,ch,decimal,count;
     
     int temp;
    
     int hex[10], number[10];
    
     i=0;
     count=0;
     decimal=0;
     
     printf("Please enter your input in decimal form: ");
     ch = getchar();
    
     while(ch != '\n')
        {
            if('0' <= ch && ch <= '9')
             {
              decimal = decimal * 10;
              decimal = decimal + (ch - '0');
             }
          ch = getchar();
        }
    
     printf("Input? %d\n",decimal);
    
     while(decimal != 0)
        {
         hex[i] = decimal%16;
         decimal= decimal/16;
    //     printf("remainder is %d\n",hex[i]);
    //     printf("after divided is %d\n",decimal);
         i++;
         count++;
    	}
    	
       i=i-1;		
      for(j=0; j < count; j++)
         {
          number[j]=hex[i];
    	  i--;
         }	
    
      for(j=0; j < count; j++)
      {
       putchar(number[j]);
      } 
     putchar('/n');	 
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You probably need to add '0' for the same reason you substract it.

    Code:
      for(j=0; j < count; j++)
         {
          number[j]=hex[i] + '0';
    	  i--;
         }
    EDIT: Not only that. You need to check if hex[i] > 10. In that case you need to add something else rather than '0'. I will not spoil the answer.
    EDIT2: Thought to test it. I got n in the end all the time. I thought somethig was wrong, but then I realised that you had
    Code:
    putchar('/n');
    It is
    Code:
    putchar('\n');
    Last edited by C_ntua; 11-12-2009 at 04:59 AM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    First of all in my view you should use gets and puts or scanf and printf instead of putchar and getchar for above mentioned problem

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by RockyMarrone View Post
    First of all in my view you should use gets and puts or scanf and printf instead of putchar and getchar for above mentioned problem
    I guess he is a beginner and wants to use putchar. Otherwise you can use printf() and itoa() and only need this

    Code:
    int main() 
    {
     int in; char buf[10];
     printf("Please enter your input in decimal form: "); 
     scanf("%d", &in);
     printf("%s", itoa(in,buf,16));
    }
    edit: gets is never good, fgets is ok

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by C_ntua View Post
    I guess he is a beginner and wants to use putchar. Otherwise you can use printf() and itoa() and only need this

    Code:
    int main() 
    {
     int in; char buf[10];
     printf("Please enter your input in decimal form: "); 
     scanf("%d", &in);
     printf("%s", itoa(in,buf,16));
    }
    edit: gets is never good, fgets is ok
    You told that he is a beginer and you are giving an example with scanf and printf LOL

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by RockyMarrone
    You told that he is a beginer and you are giving an example with scanf and printf
    Yes, beginners should learn how to use scanf and printf appropriately. Note that itoa() is non-standard, if we look strictly into the C standard library.
    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
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by laserlight View Post
    Yes, beginners should learn how to use scanf and printf appropriately. Note that itoa() is non-standard, if we look strictly into the C standard library.
    Hey Laserlight don't try to act very very smart here check out my previous post first then before writing this one here

    First of all in my view you should use gets and puts or scanf and printf instead of putchar and getchar for above mentioned problem

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by RockyMarrone
    Hey Laserlight don't try to act very very smart here check out my previous post first then before writing this one here
    I did, and I found your suggestion of gets() to be in need of correction, which was provided by C_ntua.
    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

  9. #9
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by laserlight View Post
    I did, and I found your suggestion of gets() to be in need of correction, which was provided by C_ntua.
    But you were talkin abt the printf and scanf i am getting what C_ntua is saying that is acceptable to me

    and in that case also a newbie must know why he/she should use fgets instead of gets

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by RockyMarrone
    But you were talkin abt the printf and scanf i am getting what C_ntua is saying that is acceptable to me
    So, what is your point? I do not see what there is to "LOL" about.

    Quote Originally Posted by RockyMarrone
    and in that case also a newbie must know why he/she should use fgets instead of gets
    That's right. spurs01 and RockyMarrone, read why gets() is bad.
    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

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    I can only use getchar() and putchar() for taking into and displaying output. Because this is the requirement.

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    Hello ,

    Here is my code. I revise it but why the putchar() can only display one character only ??

    Please see the last part of the code .


    Code:
    #include<stdio.h>
    
    main()
    {
    
     int i,j,ch,decimal,count,search;
     
     int temp;
    
     int hex[10];
     int number[10];
    
     i=0;
     count=0;
     decimal=0;
     
     printf("Please enter your input in decimal form: ");
     ch = getchar();
     
     while(ch != '\n')
        {
            if('0' <= ch && ch <= '9')
             {
              decimal = decimal * 10;
              decimal = decimal + (ch - '0');
             }
          ch = getchar();
        }
    
     printf("Input? %d\n",decimal);
    
     while(decimal != 0)
        {
         hex[i] = decimal%16;
         decimal= decimal/16;
         printf("remainder is %d\n",hex[i]);
         printf("after divided is %d\n",decimal);
         i++;
         count++;
    	}
    	
       i=i-1;		
      for(j=0; j < count; j++)
         {
          number[j]=hex[i];
    	  i--;
    	  printf("number[%d] is %c\n",j, number[j]);
         }	
      printf("\n");
      for(j=0; j < count; j++)
      {
       if(number[j]==0) {putchar('0');}
       else if(number[j]==1) {putchar('1');}
       else if(number[j]==2) {putchar('2');}
       else if(number[j]==3) {putchar('3');}
       else if(number[j]==4) {putchar('4');}
       else if(number[j]==5) {putchar('5');}
       else if(number[j]==6) {putchar('6');}
       else if(number[j]==7) {putchar('7');}
       else if(number[j]==8) {putchar('8');}
       else if(number[j]==9) {putchar('9');}
       else if(number[j]==10) {putchar('A');}
       else if(number[j]==11) {putchar('B');}
       else if(number[j]==12) {putchar('C');}
       else if(number[j]==13) {putchar('D');}
       else if(number[j]==14) {putchar('E');}
       else if(number[j]==15) {putchar('F');}
       j++;
       putchar('\n');
      } 
     
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 10:20 AM
  2. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 07:39 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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