close

Privacy guaranteed - Your email is not shared with anyone.

Calling all C++ folks

Discussion in 'Tech Talk' started by Jon509, Feb 4, 2005.

  1. Jon509

    Jon509

    Messages:
    250
    Likes Received:
    0
    Joined:
    Nov 18, 2002
    Location:
    southeast
    My fiencee is working on a program in her class that asks for a number and then outputs the number to the screen in reverse order. It has been a couple of years since my programming class so I can't remember the source code. Can anyone help?

    The only thing I have found on the net is "int reverse(int n)" and I'm not sure about it.

    Thanks
    Jon
     
  2. Specks

    Specks

    Messages:
    267
    Likes Received:
    0
    Joined:
    Feb 21, 2001
    Location:
    Virginia
    I'm not much of a C++ programmer, but I use C all the time. Use itoa() to get the number from an int value to a string/char*, then a simple loop can reverse the order, then atoi() to convert back into an int. The itoa() and atoi() functions work the same in C or C++.
     

  3. Jon509

    Jon509

    Messages:
    250
    Likes Received:
    0
    Joined:
    Nov 18, 2002
    Location:
    southeast
    Thanks for the help. She just told me that is has to be something that they have gone over in class already. I am going to look over her notes tonight and get it going for her.
    Thanks
    Jon
     
  4. SanduneCC

    SanduneCC Senior Member

    Messages:
    1,149
    Likes Received:
    223
    Joined:
    Sep 10, 2001
    Location:
    Boondocks, USA
    Specks is right:

    itoa() the number to a character buffer
    strlen() to get the string length of that buffer
    read in reverse order of the string length and store in another buffer
    atoi() that buffer for the new number
     
  5. Dandapani

    Dandapani

    Messages:
    8,216
    Likes Received:
    13
    Joined:
    Mar 24, 2004
    Location:
    Gulf side Florida
    How is this problem a C++ problem?
     
  6. 308endurdebate

    308endurdebate

    Messages:
    118
    Likes Received:
    3
    Joined:
    Oct 12, 2003
    Location:
    Arlington, VA
    use a stack :)

    #include <iostream.h>
    #include "stack.h"

    void main(void)
    {
    Stack stack;
    Item_type item;

    // read a character and check for end-of-file (^D) on input
    // cin of a character does not read newline char
    while ((cin >> item) && (!cin.eof())) {
    if (stack.Push(item) < 0) {
    cout << "No more room on stack.\n";
    exit(1);
    }
    }

    while (stack.Pop(item) >= 0)
    cout << item;
    cout << "\n";
    }
     
  7. David_G17

    David_G17 /\/\/\/\/\/\/\/

    Messages:
    2,046
    Likes Received:
    6
    Joined:
    Oct 7, 2002
    yep, stack.

    but i'm sure they haven't gone over useful stuff yet.

    i'd read it in as a string and use string length to get the length.

    use a for loop to go through the chars outputing them one at a time to the screen

    remember a string is an array and you can access chars like stringelement[1], stringelement[2]...etc.
     
  8. Sinister Angel

    Sinister Angel I'd Hit It!

    Messages:
    252
    Likes Received:
    0
    Joined:
    Oct 11, 2004
    Location:
    Traverse City, Michigan
    ;z ;z
     
  9. David_G17

    David_G17 /\/\/\/\/\/\/\/

    Messages:
    2,046
    Likes Received:
    6
    Joined:
    Oct 7, 2002
    ignore that. it's just some function someone wrote and likely isn't what you're looking for. it's passing in an int and returning an int. if you took that route, you'd want to pass in a string, but that seems overkill.

    from what i understand about the assignment, i doubt you even want functions.

    but i could be wrong.
     
  10. fatlander

    fatlander Comrade

    Messages:
    63
    Likes Received:
    0
    Joined:
    Nov 24, 2003
    Location:
    somewhere in the mediterranean
    The programers evolution
    it reminded me of this.
    basically it describes the "hello world" written by different levels of programers
     
  11. David_G17

    David_G17 /\/\/\/\/\/\/\/

    Messages:
    2,046
    Likes Received:
    6
    Joined:
    Oct 7, 2002
    ;z ;z ;z
     
  12. Jon509

    Jon509

    Messages:
    250
    Likes Received:
    0
    Joined:
    Nov 18, 2002
    Location:
    southeast
    She got it working. Someone had changed the names and icons of the programs they use in their programming class. When they figured that out, things worked as they should have. Thanks for the help.

    Jonathan