I have a c++ program, I would like the first argument of the main (argv[1]) to correspond to a table of float. Is it possible to do that??
I was thinking about putting in a string my floats separated with spaces (e.g. "1.12 3.23 4.32 1.1 ...")
Is there a way to automatically convert such a string into a table of floats? If I understand well the atof function converts a string into a double. So it seems it could be possible to split my string using the spaces and then convert each portion using atof.
This option does not seem to be very efficient to me? In addition it returns double and not float :(
So, is there a better way to pass table of float as argument of a c++ program ?
Thank you
A stringstream can do both the splitting at spaces and the parsing into a float.
std::stringstream ss(the_string);
std::vector<float> v(std::istream_iterator<float>(ss),
(std::istream_iterator<float>()));
// the extra parentheses here are ugly but necessary :(
How to obtain the string with the data depends on how large it is and where it is supposed to come from. Just keep in mind that in many systems the arguments passed to program are already split by spaces, putting each part in a different element of argv.
Save it in a text file, and then read it from the file when your program starts. I isn't worth it to pass it as a command-line argument.
The main() parameter list is as it is. You can pass the strings of your numbers as arguments to your program. The main function will have to parse its argument.
When you want to pass a space separated list of numbers in argv[1] you can use the strtok function to get the individual number strings and have to pass it to a conversion function.
When your conversion function returns a double you should check that the result can be represented by a float and cast the value to a float variable. But I would consider to use double as the internal representation.
In addition to Singer's answer:
The commandline should be used mainly by human, not computer. If you really need a table of values, use configuration file. You can always use human readable format for it.
Related
I want to add two large numbers in C++ using vectors. However, I can't find any way in which I could properly read them from a file, so I wanted to use a char array which will read the characters and, if they are digits convert them into an int variable of the vector.
Is there any way in which I could do this?
You can easily convert digits into the corresponding integer values, because char can be treated as numeric values. Assuming you got some input like
char input = getNextDigit();
You can do
int asInt = input - '0';
which works, if input is any digit from '0' to '9'.
You can use the atoi function. The given link has pretty clear examples on how to use it.
I hope this is what you were looking for!
UPD: whenever you deal with strings it is better to go one step up in terms of abstraction and don't work with arrays of char directly. std::string should be your first choice. Starting from C++11 atoi has an std::string version called stoi.
I am trying to learn c++, and for an assignment I have ran into a problem. I am trying to get an integer value from a string that a user enters all on one line.
Ex.) The user inputs: "Change value to 15."
What is the best way of getting the 15 from that string? I have looked around for a while, but could only find if a string was only integers.
Thanks in advance!
Why not use a mixture of getline(grabs your whole line) and string stream(tokenizes the input) and put them all in a vector(easier to use than an array), grab the one at .size()-1 and do an atoi on that. Might be overkill, but string stream could do what you want. For a small tut this could help http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/
This might not be the best way but to get something done now you can use strtok to tokenize your input string and then examine the tokens for your integer value integer. One of the answers here [link] suggests using strtok to tokenize a string.
If you know the format of you string or know that there will always be a single integer value then you can use string::find_first_of and string::find_last_of [link] and then just get the substring and use string::stoi.
I am getting command line argument when i launch the application.
I am getting four parameter from command line.
after parsing I store them in four std::string/CString(mfc) variable now i need to know whether the value is decimal or not.because these parameter is going to be used in some mathematical calculation.
Can anybody help me on this.
here you can find how to determine if a string is a numeric, in 70 languages!
I'm not sure if all the solutions check the same thing. The C++ implementation checks if the input is a positive/negative integer/floating-point for base 10, or if it a is positive/negative integer for base 8/16. Is that what you want? Do you need to support only positive numbers? Do you need to support floating-points?
Probably you'll need to convert your input strings to numeric value, so there is no reason to do it in two steps (to check, then to convert). Better do it in a single step.
One more thing: If the input string is too long, for example "32525252332912461984612491264912649126129319312931279171295127951275129" - you normally won't want to consider it as a valid input.
Look at every character in the string, and if you find something that is not a digit, or a '.', then it is not a number.
Just use a string->number conversion function that unambiguously reports failure.
e.g. strtod and not atof
I've been assigned a problem I simply do not understand. I know that I need to use a cin function (like cin.get()), but I'm not sure which one I need or how to use it in this circumstance.
I need to create an insertion and extraction operator that reads (and writes) 3 pieces of data. All of the data is of the type int. For context, the data is the whole part of a fraction, the numerator, and the denominator. The data needs to be delimited by spaces, and the operators will be used for file input and output.
What I really want to know is which cin function I should use, and what the particular syntax should be considering I want to store the value in an integer.
Thanks in advance!
With cin, you can just read data using something like this:
int wholepart,numer,denom;
cin>>wholepart>>numer>>denom;
This would read 3 integers into wholepart, numer and denom respectively. It will skip over whitespace separating the integers.
The normal operator>> for ints expects the data to be separated by whitespace, so you should be able to just use it and interpret the results as you see fit.
must you convert from strings to double? if so. how?
Are there functions for trig that will accept textbox string data as is?
Is there a way to pull the data from the textbox as a numeric value, not as a string?
must you convert from strings to double? if so. how?
Yes. There are quite a few ways to do this.
Are there functions for trig that will accept textbox string data as is?
No, but if you really wanted one, you could easily implement such a function using one of the techniques described in the aforementioned question.
Usually you want to validate user input to be sure it is correct before you use it, and validation of the input in numeric form is much easier than validation of the raw string.
Is there a way to pull the data from the textbox as a numeric value, not as a string?
Maybe; it depends entirely on what GUI framework you are using.
must you convert from strings to double?
Yes.
if so. how?
The C++ way is to use the string streams; in particular, you'll probably want to use istringstream. A viable alternative is to follow the C way, i.e. use sscanf with the appropriate format specifier ("%f").
Another C++ option is to use the boost lexical_cast, but starting to use boost just for lexical_cast is a bit overkill in my opinion.
Example with istringstream:
#include <sstream>
// ...
std::istringstream strParser(yourString);
double yourDouble;
strParser>>yourDouble;
if(strParser.fail())
{
// the string couldn't be converted to a double
}
else if(!strParser.eof())
{
// the string hasn't been fully consumed; this may or may not be a problem, depending on your needs
}
Are there functions for trig that will accept textbox string data as is?
AFAIK no, there's no need of them (although you can write them quite easily).
Is there a way to pull the data from the textbox as a numeric value, not as a string?
The WinAPIs provide a handful of such "convenience functions" for use in dialogs, but the only one I can recall that provides such help is GetDlgItemInt, which, as the name may suggest, works only for integers.
Addendum
I see now that you are using C++/CLI (you mentioned System::String): well, you should have said that, this changes the options quite a bit.
In the managed world, your best option is to use the Double::Parse method; to catch bad-formatted strings, you should either catch the exceptions thrown by Double::Parse or call Double::TryParse before calling Double::Parse.
Addendum bis
Uh, I forgot, since you're using the .NET Framework probably it should be better to use the .NET trigonometric functions (class System.Math).
I'd recommend using strtod
char *str = ...;
const char *end;
double d = strtod(str, &end);
if (*end != '\0')
{
// questionable data
}
There's no way to do a straight conversion from textbox to double as far as I know. You'll need to first convert to a double with something like strtod.
You could easily make a generic function which accepts a textbox control as an argument and returns a double (assuming it's the textbox contains a valid number) mind you..