Thread: problem in char variable

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 05: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, 01:18 AM