Setting value back?

This is a discussion on Setting value back? within the C Programming forums, part of the General Programming Boards category; Say I have the following code below, and I want to print out the original value of a, how would ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Setting value back?

    Say I have the following code below, and I want to print out the original value of a, how would I do that?

    Code:
      printf("Enter a positive number: ");
      scanf("%d", &a);
    
      if(a > 0){
        while(a > 0){
          digits = a % 10;
          a = a / 10;
          if(digits == 0) printf(" ");
          p++;
        }
      }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As in, the value before the loop? Do something like
    Code:
    original_value_of_a = a;
    before the loop, and then print original_value_of_a.

    Of course, that's cheating, since your assignment is probably to use the loop to print a correctly. In that case, you should re-examine what's going on in your loop and decide what you want to print (hint: it's not " ").

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Oh, my assignment is to find the persistence of a number. I figured out the algorithm for it, but when it came to resetting the value to print it back out stumped me.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Also, how would I be able to loop the scanf until EOF has been entered without pasting that repeatedly?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by blurx View Post
    Also, how would I be able to loop the scanf until EOF has been entered without pasting that repeatedly?
    Put it in a loop?

    Search the FAQ for looping until end of file.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Interesting.
    http://en.wikipedia.org/wiki/Persistence_of_a_number

    If your algorithm is the code above, then it's far from complete.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UInt to CString and back
    By xlnk in forum Windows Programming
    Replies: 6
    Last Post: 08-27-2003, 03:08 PM
  2. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 12:42 AM
  3. Yey Im Back!
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2003, 03:07 PM
  4. Returning a Tree node back to void main()
    By gazza in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2002, 02:48 AM
  5. I am back
    By NANO in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2002, 11:00 AM

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