What is the error??

This is a discussion on What is the error?? within the C Programming forums, part of the General Programming Boards category; My problem is this. First I need to read a single integer type digit from the keyboard. Then I need ...

  1. #1
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Unhappy What is the error??

    My problem is this.

    First I need to read a single integer type digit from the keyboard.

    Then I need to print that digit in character form.ie. if I entered 1 ,I need to get the character constant '1' as output.

    In the second case I need to read a character from keyboard .If it is any one of characters 0 - 1, it will printed in the decimal form.
    ie. if I enter character 1 , I need to get decimal value 1 as output.

    The program is simple but I dont get correct output.
    ie. I got output of first case .
    In the second case I 'm not getting any output.ie program doesn't stops.
    I checked whether there is an infinite loop by putting a printf() statement .
    But there is no infinite loop.
    Then what is my problem.

    Code is this:
    Code:
    #include<stdio.h>
    
    main()
    {       int digit , n = 0;
            char ch;
            printf("ENTER DIGIT:");
            scanf("%d",&digit);
    
             for( ch = 48;n < digit;++n , ch++ );
             printf("ENTERED DIGIT IS  %c \n",ch);
    
             printf("NOW ENTER A CHARACTER, IF IT IS A DIGIT THAT DIGIT IS PRINTED IN DECIMAL FORM:");
             scanf(" %c ",&ch);
    
             for( digit = 0  ; digit < ch - 48 ; digit++ );         
             printf("ENTERED CHARACTER  IS  %d \n",digit);
    
    }



    OS Linux
    compiler g++
    Last edited by linuxlover; 12-01-2010 at 12:45 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Code ending tag is the other slash

    main() should be int main(void) and should return 0.

    Compiler should be gcc not g++. That's a c++ compiler.

    I think the most you can learn from this is by observing (via debugging) the values of your variables while the program is running. Step through the code and see how they change. What can you notice?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Smile

    Quote Originally Posted by claudiu View Post
    Code ending tag is the other slash

    main() should be int main(void) and should return 0.

    Compiler should be gcc not g++. That's a c++ compiler.

    I think the most you can learn from this is by observing (via debugging) the values of your variables while the program is running. Step through the code and see how they change. What can you notice?
    First suggestion accepted
    But why main() should explicitly specified int.If nothing is specified default type is int .Then why....
    Change of which variable you mean?????????????
    Functions whose return type is int can return nothing..Then why main()
    should return sth..?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Instead of using for loops (which waste a ton of time) why don't you just add and subtract?

    Code:
    // decimal to ascii
    digit += '0';
    
    // acii to decimal
    letter -= '0';
    (Laserlight will now correct me, of course)

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by linuxlover View Post
    First suggestion accepted
    But why main() should explicitly specified int.If nothing is specified default type is int .Then why....
    Change of which variable you mean?????????????
    Functions whose return type is int can return nothing..Then why main()
    should return sth..?
    Read this:

    Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

Tags for this Thread


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