problem in char variable

This is a discussion on problem in char variable within the C Programming forums, part of the General Programming Boards category; Hello friends, I am making a simple to calculate the digits of a number. The basic code is fine and ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    problem in char variable

    Hello friends,

    I am making a simple to calculate the digits of a number. The basic code is fine and it is working.

    Now I want that if the user is interested he can input another no and found the sum of digit for it.

    For this I am using a while loop with condition that a char variable x has the value 'y'. At the end of the loop I ask the user to press 'y' if he is interested in running the program again.

    But the while loop does not run again. Here is the code

    Code:
    main()
    {
       int num, n, rem, sum = 0;
       char x = 'y';
    
        while(x == 'y')
         {
              printf("enter a number less than equal to 32767\n");
              scanf("%d",&num);
              n = num;
              while(n>0)
               {
                     rem = n%10;
                     n = n/10;
                     sum = sum + rem;
                }
                printf("the sum of digits is %d", sum);
                printf("\n do you want to continue,press 'y' if yes, else press any other
                         char");
                scanf("%c",&x);
           }
          getch();
    }
    The problem is that the control moves out of the outer while loop even if the user types 'y'.

    Thanks a lot friends

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    When you enter your number, the buffer has the number and the newline from the enter. scanf() takes the number but leaves the newline on the buffer. The next scanf() then take the newline, so the value of x becomes that of the newline. A newline is not 'y', so the loop ends. A possible solution is to place a getchar() after the first scanf(). You might want to read the FAQ on How do I get my program to wait for a keypress?
    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

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    or you can always red the whole line with the fgets (that will not leave a new char in the input stream
    and when extract the user input from the buffer using sscanf
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Problem with char and char*
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 11:44 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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