Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
I have floating-point numbers stored as strings. During serialization to JSON, I want to convert those strings to numbers. For example, the strings a="1.001" and b="0.1" must be converted to numbers:
{
"a" : 1.001,
"b" : 0.1
}
The folloiwng code:
val["a"] = "1.001";
val["b"] = "0.1";
Json::StreamWriterBuilder builder;
auto json=Json::writeString(builder,val);
std::cout << json << std::endl;
produces:
{
"a" : "1.001",
"b" : "0.1"
}
which is not what I want because numbers as strings are rejected by the server.
The following code:
val["a"] = std::stod("1.001");
val["b"] = std::stod("0.1");
Json::StreamWriterBuilder builder;
auto json=Json::writeString(builder,val);
std::cout << json << std::endl;
produces:
{
"a" : 1.0009999999999999,
"b" : 0.10000000000000001
}
however, that is also not what I want because I lose precision.
UPDATE:
I've solved the problem by implementing a custom Json::Writer.
I used the existing implementation of Json::FastWriter::writeValue as a starting point and made changes to the way it processes string values:
case stringValue: {
auto str = value.asString();
if (isNumeric(str))
document_ += str;
else
document_ += valueToQuotedString(str.c_str());
break;
}
While my solution works, it is not ideal because all string fields that contain valid floating-point numbers are now serialized as numeric type, and the server rejects JSON string fields that are represented solely as floating-point numbers without quotation marks.
Can you suggest any other solutions? Perhaps I should consider using a different JSON library?
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
This post was edited and submitted for review yesterday.
Improve this question
I want to replace a String data with a new one where I do not know what is inside the string as it may be empty, spaces, alphabets, numerics, special characters, mix, etc. It is simple as below
String randomString = "Some Old String"; // It will be any string or may be an empty string too
print("Old String ${randomString}"); // Output = Some Old String
randomString = "Some New String";
print("New String ${randomString}"); // Output = Some New String
But due to some limitations (no need to define here), I can't use the upper process. I would like to do this with only sub-functions like .replaceAll, .replaceRange, .replaceMap, .replaceFirst, .replaceFirstMap etc. For that, I tried following.
String randomString = ""; // It will be any string or maybe an empty string too
print("Old String ${randomString}"); // Output = ""
randomString.replaceAll(RegExp(r"/\W/g"),"Some New String");
print("New String ${randomString}"); // Output = "" (Showing Empty)
What should be the regex to select all and replace with alphabets, digits, special characters, spaces, empty, etc.?
I am currently using RegExp(r"/\W/g") so tell me the correct one for my need.
Live DEMO:
jdoodle.com/ia/DVm
You can achieve this by creating TextInputFormatter class and pass it to inputFormatters parameter in TextField, here's an example:
class ReplaceTextInputFormatter extends TextInputFormatter {
#override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
// TODO replace the [[oldValue] with [newValue] for your needs
return newValue;
}
}
TextField(
inputFormatters: [ReplaceTextInputFormatter()],
)
Edit after #Muhammad Hassan's comment about no need for widgets.
We can achieve this using replaceRange from 0 to the string length, for example:
randomString.replaceRange(0, s.length, 'replacement');
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've asked to write code that gets a char array(sentence), if the there is an 'i' in the sentence I need to add the letter 'b' the letter 'i' again like this example:
pig -> pibig
I tried to use string.h functions but I didn't succeed to make it right.
Use std::string in string header file, and std::string::insert whenever you need to insert a char in string:
std::string my_string = "my satringa";
for (size_t i = 0; i < my_string.length(); ++i)
{
if (my_string.at(i) == 'a')
{
my_string.insert(i + 1, "b");
}
}
std::clog << my_string << std::endl;
Output:
> my sabtringab
If you are forced to use C-style strings, don't worry do all of your operations on std::string and then take the underlying stored string with std::string::c_str() as a C-style string (and don't forget to take a copy).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So I'm a bit stumped on this, I'm reading a file with two types of lines that contain data, and they are started by a number followed by a comma. I need a way in order to match the lines with the same starting digit into a single line and output that. How would I even get started?
I'd do this by reading each line in two parts: the stuff before the comma, and the stuff after it.
Then I'd have a map (or unordered_map) with the value before the comma as the key, and the rest as the value associated with it.
std::map<std::string, std::string> data;
std::string key, value;
while (std::getline(infile, key, ',')) {
std::getline(infile, value);
data[key] += value;
}
Then (presumably) you'd want to write out the values:
for (auto const &v : data)
std::cout << v.first << ":" << v.second << "\n";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am making a shopping list program. For this program, I need to be able to type in a user input that accepts both number (1564, 121,1, etc) and word (hello, goodbye, etc) combinations. The program reads numbers just fine, but it cannot process words. Thank you in advance. The part of the code I am stuck with is below:
int code, option, count = 0;
double quantity, price, cost;
string description;
cin >> code;
while ((code != 123456789) && (count < 2))
{
cout << "Incorrect code, try again \n";
cin >> code;
count++;
if (count == 2)
{
cout << "max # of tries reached. Goodbye. \n";
system("pause");
}
}
Your code variable is now an int. If you wanted that to be a string, declare it so: std::string code;. Note that you might need to #include <string> in the very beginning. Also, if you want to compare it with numbers, either you call something like atoi() (string has .cstr()), or better yet, you might just compare it with "123456789". HTH.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I receive text with special characters (such as á) so I have to manually search and replace each one with code (in this case "á")
I would like to have code to search and replace such instances automatically after user input. Since I'm a noob, I'll show you the code I have so far - however meager it may be.
// Text fixer
#include <iostream>
#include <fstream>
#include <string>
int main(){
string input;
cout << "Input text";
cin >> input;
// this is where I'm at a loss. How should I manipulate the variable?
cout << input;
return 0;
}
Thank you!
An easy method is to use an array of substitution strings:
std::string replacement_text[???];
The idea is that you use the incoming character as the index into the array and extract the replacement text.
For example:
replacement_text[' '] = " ";
// ...
std::string new_string = replacement_text[input_character];
Another method is to use switch and case to convert the character.
Alternative techniques are a lookup table and std::map.
The lookup table could be an array of mapping structures:
struct Entry
{
char key;
std::string replacement_text;
}
Search the table using the key field to match the incoming character. Use the replacement_text to get the replacement text.