Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to create a string array to hold the strings generated by a void function. I am very confused about if or how I can do this. The below code is giving me an error about "no suitable constructor."
getWords("theFile.dat"); // Function to extract list of strings from file
string wordsList[] = {getWords("theFile.dat")}; // Add strings from function to array
If (as your comment states) the code uses cout to print the strings, then you have no way to capture them and put them in your array. Well, ok, you might be able to, by doing some major/ugly hack that grabs hold of the stdout file descriptor and reads what is written to it, but that would just be ugly beyond words (and most likely would need different platform specific implementations). Just don't go there.
Write your own good function to read the file and return what you need or fix the crap function you've been given.
There's got to be a limit to how much crud (with the risk of introducing more crap/bugs) one has to add to work around broken crap code before fixing or re-implementing/re-factoring it.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
I am using XMPie and need a regex to character count for me. My Regex is ^[\S\s]{1,800}$ but I am getting a different result with the refresh preview button than the next screen button. I had a support case on it and it went all the way to R&D and they basically said, there is nothing we can do, try a different expression. I think one button must be implemented with C# and the other with JS or some such thing. What seems to be happening is that one is counting a return once (refresh), while the other is counting 2 characters for it. I think it must see it as \r or \n and count that as 2. Any ideas on how I could modify my expression to prevent that? I mostly just want consistency, so making it explicitly count it twice, or not count it at all would be more livable than differing results, especially as refresh is lower.
Thanks so much for any help!
I have tried a slew of different expressions, but I am not great with regex and nothing has given me a better result.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
Let's say I have an object MyObject and it has an attribute double MyObject::a. When I initialize a I set it to 0.05, but at some point when I run my code I notice that a is 1.something e-316, even though my code is never supposed to change its value. If I knew an exact place in memory that a occupies, is there a tool that will tell me when exactly this place is overwritten?
As others have already noted, you could use a debugger for this. Most reasonably recent debuggers have some capability to break when a value is written to a particular variable (e.g., Visual Studio calls this a "watchpoint" or "data breakpoint" (depending on what age of IDE you're looking at), if memory serves.
Depending on the situation, you might be able to get some useful information by changing your double to a const double:
const double value { 0.05 };
Then any code that tries to assign a new value to this variable simply won't compile.
If, however, the problem arises from some code doing an out of bounds write, rather than assigning to the value that's getting overwritten, this won't help find that.
gdb watchpoints
VS data breakpoints
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have implemented a multi-map, but I was wondering how do I access the first 5 elements of a multimap?
I tried using a for-loop, but that didn't work out. Any suggestions?
Although it would be easier to help you if you were to post a minimal reproducible example, as text, formatted as a code sample, I think I still understand what you’re asking.
This looks like a learning exercise that you want to solve on your own. But I can give some advice.
What you want to do is check two conditions: that you’ve read five elements, or that you’ve run out of them. Declare both a loop counter initialized to 0 and an iterator.initialized to .begin(). Loop until the counter is equal to 5 or the iterator is equal to .end(). On each iteration, increment both the counter and the iterator. You might express this as a while loop, but you could also do it with comma operators in your for loop.
Also, please indent your code properly and use braces under your for and if statements. It’ll save you from writing a lot of bugs and make your code much easier to read.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
The title pretty much says it all, but still to elaborate:
I would have understood if the language would have restricted me from adding new contents to a file (irrespective of position) because it would lead to fragmentation.
But what I do not understand is why it is not possible to:
Erase contents from the last line, similar to backspacing from EOF. [ASCII/BINARY]
Erase contents from middle portion of file [ASCII/BINARY]
Replace text in a file with some other text of same size [ASCII]
Replace data in a file with some other data of same size [Binary]
Does any other language support this?
EDIT: To do this in C++, you need to read the file, perform the modifications on variables, then create a new file. The question was why it is not possible to edit the "original" file instead of creating another file.
Does any other language support this?
C++ supports it. You can seek and you can write. But the fact that this is a very uncommon scenario to replace the same exact byte length and the fact that replacing a bunch of bytes with another bunch of bytes that has a different length is horribly inefficient to a point were writing a new file is faster, lead to the language and functions that exist.
Other languages may handle this differently, but I have yet to learn one that actually does. Because file handling does not change across languages and all language and framework writers came to the same conclusions: files are not the best medium for insertion and deletion of data. Arrays are not the best medium for insertion and deletion, files just being one them.
If you need to insert and delete, use another container (lists come to mind). If you need to persist this container to disk, do so after all manipulation is done.
Looks like you were expecting lowish-level access to files to magically work like some word processor / text editor application. But they don't do this, either! They simply abstract away from the user all the complicated mechanisms involved in editing and re-committing a file to disk.
In writing C++ source code, you are now taking responsibility for implementing those mechanisms. It's not quite as simple as dumping a bunch of backspace characters at EOF. :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am relatively new to programming, and I can only really understand C++. I have recently started working on a project that requires the user to input something that will allow them to make a selection. I can't figure out how to make it possible for the user to input a string or a char but get the same result. I know that this would require that I assign the variable that the user inputs (for example 'a') two data types, but how do I do that? I've tried using "string/char a;" but that doesn't work.
Could someone please help me with multi-data-type variables?
Thanks
The string type will work for all user input. Since it "doesn't work" for you, we can't help you further if you don't show us what you tried.
If the User is the one making the Input from the I/O, then you get to decide if you will treat the input as a string or char. After receiving the Input you should know what you want to do with it. And you can also store the input data in array, vectors or list. Primitive data types can do so many things just understand the purpose and function of your program.