Reading in a number backwards!

This is a discussion on Reading in a number backwards! within the C++ Programming forums, part of the General Programming Boards category; Hi, I have to write a program that will take a binary number (10010111) and then tell it decimal equivalency. ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Reading in a number backwards!

    Hi,

    I have to write a program that will take a binary number (10010111) and then tell it decimal equivalency. But i do not know how to read in a number backwards. I know how to read them in one at a time from left to right but not right to left like i need to.


    Any tips or suggestions would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is to NOT each bit, thus you will end up with the reverse integer.

    The NOT syntex for C++ is this: ~

    Kuphryn

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    To get the last number in an int, just do a modulus 10. For example N%10 will give you the last digit for the number N. If you want to keep going, divide N by 10 to chop off the last number, then do modulus again.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    you could put the number in a char array, use a for loop or two to put the number backwards in another array, and then use atoi() to make it an integer again..

  5. #5
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35
    Or you could read them into a stack (instead of a string/char*), just push each one in as you get to it. Then pop them out again.
    Something like this, where Str is an AnsiString that contains the string just inputed, and S is a stack:
    Code:
      for (int i = 0; i < Str.Length(); i++)
        S.Push(Str[i]);
    
      AnsiString Str2;
      for (int i = 0; i < Str.Length(); i++)
        Str2 = S.Pop(Str[i])

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I think the char array idea is simplest, then just read the number as you would a word and process the array elements indivually in a loop
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  2. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  3. Reading files number by number, also getw()?
    By rmullen3 in forum C Programming
    Replies: 4
    Last Post: 01-03-2003, 12:22 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. HELP - Reading CPU Serial Number
    By Rigoberto Croes in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2002, 05:15 AM

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