Let me preface this by saying I am really new to C++.
I want to grab the numerator and denominator from a fraction that gets entered in the form of A/B (in the command line); what is the easiest way to get A and B into their respective variables?
Let me preface this by saying I am really new to C++.
I want to grab the numerator and denominator from a fraction that gets entered in the form of A/B (in the command line); what is the easiest way to get A and B into their respective variables?
The easiest approach using C++ is
The above reads the rational value from a stream. To get a stream from an argument on the command line, you'd use an
std::istringstream
:The main drawback is that the separating character can be anything with the extraction code mentioned above. To fix this up I'd use a
slash
manipulator:Since this solution seems to be appreciated, I want to point out that depending on needs you might want to slightly tweak the implementation of
slash()
: The posted version expects the slash right after the numerator. It may be reasonable to skip leading whitespace:Also, this is a special implementation for just one character and you might want to have similar manipulators for other characters. To avoid replicating code the manipulator can become a template:
The need to use
std::char_traits<char>::to_int_type()
is there to avoid problems when usingchar
s with a negative value.Command line arguments are passed as C strings (i.e, character pointers). If you have a basic framework of a program, you already have...
... or something like it. The first member of the argv array (i.e., argv[0]) is a character pointer to the name of the running program. The remaining arguments (argv[1] thru argv[argc-1]) are the command line arguments.
If you are just learning c/c++, then you should run a basic simple program under the debugger passing different arguments, quoting any arguments with spaces, using environment variables as arguments, and just experimenting to see what really gets passed in.
When you have this down, use your A/B as an argument. It will be a string and not a number, so you will need to divide at the "/".
will work as long as your numbers and slash are all run together to make one argument. The shell will usually take spaces to delimit different arguments.
You can use other methods to find the slash and the value. The answer to your question does not have to be specific to c++. Scanf works for both c/c++. Another simple way is to...
Now you have the string form of the values that would need to be converted to integer (or float or double, depends on your requirement). Of course, there is no error checking, so if you don't call the program exactly right, programmatic chaos will ensue.
Others have posted other answers for other ways to do it, but I hope this helps and welcome to c/c++ programming. And please get a good book, trial and error only goes so far and C/C++ are very unforgiving.
scanf
. Even in C++, it still works. Note that variable names should start with lowercase.-Removed-By Poster for not completing the answer with command line.
Split the string look up strtok once you read it in from std in like others have demonstrated.