Store some things in a float

This is a discussion on Store some things in a float within the C++ Programming forums, part of the General Programming Boards category; Ok, i want that when someone say /teleport 045 120 039, store the 3 values (x y and z) in ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    17

    Store some things in a float

    Ok, i want that when someone say /teleport 045 120 039, store the 3 values (x y and z) in a float[3]. How i do that? thanks ^^

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    just use a loop with cin >> as this skips spaces

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    This is how I've bashed at it, but I need help. I've never used sscanf() before that fact that my output is "0 - 0 - " kind of makes me think I'm doing something wrong (not necessarily with sscanf obviously).

    Code:
    int main(int argc, char *argv[])
    {
            float   fCoords[3] = {0, 0, 0};
            char    *cInBuf = new char[60];
    
            std::cout << "] ";
            fgets (cInBuf, 60, stdin);
    
            sscanf (cInBuf, "%f %f %f", &fCoords[0], &fCoords[1], &fCoords[2]);
            for (int i = 0; i < 2; i++)
                    std::cout << fCoords[i] << " - ";
    
            std::cout << std::endl;
    
            delete [] cInBuf;
            return 0;
    }
    EDIT: I give "/teleport 123 456 789" as the input.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > sscanf (cInBuf, "%f %f %f", &fCoords[0], &fCoords[1], &fCoords[2]);
    Try:
    Code:
            int num_read = sscanf (cInBuf, "%*/teleport %f %f %f", &fCoords[0], &fCoords[1], &fCoords[2]);
            if (num_read != 3)
            {
                std::cout << "Invalid format. Use: /teleport 123 456 789" << std::endl;
                std::cin.get();
                return 1;
             }
    Or you could also use:
    Code:
            int num_read = sscanf (cInBuf, "%*s %f %f %f", &fCoords[0], &fCoords[1], &fCoords[2]);
            if (num_read != 3)
            {
                std::cout << "Invalid format. Use: /teleport 123 456 789" << std::endl;
                std::cin.get();
                return 1;
             }
    > for (int i = 0; i < 2; i++)
    And I'm assuming you want 3 here:
    Code:
            for (int i = 0; i < 3; i++)

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Thanks for clearing that up, works now
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    17
    Yeah thanks too, i discovered before you post but thanks ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 03:23 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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