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 ^^
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 ^^
just use a loop with cin >> as this skips spaces
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).
EDIT: I give "/teleport 123 456 789" as the input.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; }
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
> sscanf (cInBuf, "%f %f %f", &fCoords[0], &fCoords[1], &fCoords[2]);
Try:Or you could also use: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; }> for (int i = 0; i < 2; i++)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; }
And I'm assuming you want 3 here:
Code:for (int i = 0; i < 3; i++)
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
Yeah thanks too, i discovered before you post but thanks ^^