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

    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

       int num, n, rem, sum = 0;
       char x = 'y';
        while(x == 'y')
              printf("enter a number less than equal to 32767\n");
              n = num;
                     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
    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
    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
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    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
    To be or not to be == true

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, 03:49 PM
  3. Problem with char and char*
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:44 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

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