Login Register






C++ Noobie Challenge - Hex Input filter_list
Author
Message
C++ Noobie Challenge - Hex Input #1
Write a C++ program that takes in a numeric value in hexadecimal (base 16), and store that value into a primitive 'int' variable. Smile
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Noobie Challenge - Hex Input #2
Code:
#include <cstdlib> int main(int argc, char* argv[]) { int input; scanf("%x", &input); }

Reply

RE: C++ Noobie Challenge - Hex Input #3
That isn't a *real* C++ solution however. That's the way C programmers would do it...
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Noobie Challenge - Hex Input #4
Code:
#include <cstdlib> int main(int argc, char* argv[]) { int input; std::cin >> hex >> input; }

Using scanf is cleaner imo and C++ supports that function...
(This post was last modified: 08-01-2014, 11:33 PM by miiike980.)

Reply

RE: C++ Noobie Challenge - Hex Input #5
(08-01-2014, 11:32 PM)dropzon3 Wrote:
Code:
#include <cstdlib> int main(int argc, char* argv[]) { int input; std::cin >> hex >> input; }

Using scanf is cleaner imo and C++ supports that function...

Explain how it would be cleaner? At the least, it is much less typesafe than what you would and should normally do if you're writing in C++. Additionally, that function has to do the following:

1. Parse the format string to determine the types and variable arguments to parse from an args list
- Look for occurrences of %
2. Determine all the attributes/specifiers in the format designator (%7.4lf for example)
3. Read the characters from the standard input buffer until a character that isn't right for the specified format from the input string is found
4. Convert these characters into the proper binary format for the specified type
5. Load the binary data into the variable referenced by the pointer parameter(s) passed in, following the format string

This process repeats until the variable arguments have all been assigned to, which is determined by the format string (the only way to determine how many variable arguments were actually passed in).

It's a huge and very ugly function.

However, in either variation scanf() or std::cin, are you correctly handling invalid input for the standard input.

Here's what I was looking for somewhat:
Code:
#include <iostream> #include <string> #include <limits> int main() { int x; { while (!(std::cin >> std::hex >> x)) { std::cout << "Invalid input, try again..." << std::endl; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } std::string tmp; getline(std::cin, tmp); } std::cout << x << std::endl; }
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: C++ Noobie Challenge - Hex Input #6
Another generic approach I came up with:
Code:
#include <iostream> #include <string> #include <sstream> template <class T> T get_input(bool &err, std::istream &in = std::cin) { return [&in, &err](T t) noexcept -> T { std::string tmp; getline(in, tmp); return (err = !(std::istringstream(tmp) >> std::hex >> t)), std::move(t); }({}); } int main() { bool err; auto &&x = std::move(get_input<long>(err)); std::cout << "Error flag set: " << err << std::endl; std::cout << x << std::endl; std::cin.get(); }

edit: Alternative:
Code:
auto &&x = std::move([&](int t) noexcept { std::string tmp; getline(std::cin, tmp); return (err = !(std::istringstream(tmp) >> std::hex >> t)), std::move(t); }({}));

FFS - I tried editing my post 3 or 4 times now, and there's no way I'm wasting more of my time to have any filters f*ck me over once more. If the questions arise for what I was intending on posting, then I'll answer them accordingly...
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply