Question about C++

Sharky Forums


Results 1 to 6 of 6

Thread: Question about C++

  1. #1
    Goldfish machinegun_gotti's Avatar
    Join Date
    Sep 2000
    Location
    Mr. Burns' basement
    Posts
    98

    Question about C++

    I'm trying to follow a C++ programming book, and for one of the example programs it has a Fibonacci sequence (1,1,2,3,5,8,13,etc.).

    The object of the program is to identify the ordinal sequence of a number ("8 is the 6th number of the sequence"), and of course you do cin/cout for where the 8 and 6 appear in that sentence.

    My question is an aesthetic one: is there a way to have the program notice the last digit of the number? I'm asking because for example because if a number is 122nd in the sequence, it will appear as "122th", which isn't correct? Are there any suggestions?
    "'It was the best of times, it was the blurst of times?!' You stupid monkey!"

  2. #2
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    use the modulus operator "%" --> gives you remainder

    let a be an int

    a % 10 will device 10 into a giving you the remainder which is the last digit b/c all digits to the left of the last digit will be divisible by 10.

    then you can do some ifs and crap like that and generate the correct response (eg 121st 122nd 123rd etc)


    eg

    void foo(int bar)
    {
    cout << bar;
    int remainder = bar%10;
    if(remainder == 1)
    cout << "st";
    else if(remainder == 2)
    cout << "nd";
    else if(remainder == 3)
    cout << "rd";
    else
    cout << "th";
    }
    Last edited by dighn; 03-29-2002 at 09:29 PM.
    .

  3. #3
    Expensive Sushi
    Join Date
    Feb 2002
    Posts
    7
    You could go...

    apstring <int> variable;

    cin >> variable;

    cout << "Last digit in variable is: "
    << variable[(variable.length()-1)];

  4. #4
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    that works however apstring is non-standard. it's for educational purposes only. any compiler you buy/download isn't gonna have it.

    besides converting it to string and taking a character is rather expensive compared to modulus
    .

  5. #5
    Grouchy Tech Recon's Avatar
    Join Date
    Jun 2001
    Posts
    4,963
    % is your best bet or typcast the int into a char* and do it that way

  6. #6
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    Originally posted by Recon
    % is your best bet or typcast the int into a char* and do it that way
    I think for the latter you mean converting to a string? type casting to char*(c style string) is not the same as converting to a string (also char*)

    type casting an arbitrary int to char* will give you a "dangling pointer" b/c the memory address referred by the char* will be the same as the value of an integer which is unlikely to be a valid address.

    converting to a string is another story. a piece of memory is allocated, the int is converted to string and put into that memory and that memory address is returned and stored in a char*.

    as you can see they are different.
    .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •