353 Posted Topics
Re: @tennis > 1. an int and a class with only an int member, are they of the same size? Maybe. The size of a class will be the total bytes of the data members and any padding. It is the padding that gets you on this question. > 2. what … | |
Re: remove_if only knows the name of the method, but the class defines two methods by that name. So remove_if cannot decide which one to call. You can disambiguate with a cast to the more suitable predicate. [code] particleContainer.remove_if((bool(*)(const Particle&))Particle::isDead); [/code] | |
Re: There is still overflow. When building an intermediate value, it is safest to stop before the least significant digit of the largest possible value, then you can test the intermediate value and next digit separately for overflow. [code] #include <cctype> #include <climits> #include <iostream> int main() { const unsigned safe_value … | |
Re: > Why does a C program treat a function call to a function and a function call to a function pointer the same? Because they *are* the same. :) A function name is converted to a pointer to a function before the call operator is applied. [quote=C99 Draft] The expression … | |
Re: Why does it need to be open source? Just askin'. ;) | |
Re: Break it down into a while loop and the problem stands out more. [code] com = cmd.commands; while (com->name) { free(com); com = com->next; } [/code] The code frees com and then immediately uses the freed pointer to reset itself. Freed pointers are off limits, all you are allowed to … | |
Re: fgets() stores newlines because the absence of a newline is how fgets() tells the caller that only a partial line was read. The comparison is failing because the line contains "RESET\n" instead of "RESET". The usual way to get around a problem like this is trimming the newline. [code] while … | |
Re: Network status information can be gathered from the network adapters on the machine. Here is a sample that does it on Windows to give you ideas. [code] // Compiled and run with Visual C++ 2010 #include <cstdlib> #include <iostream> #include <vector> #include <windows.h> #include <iphlpapi.h> #pragma comment(lib, "IPHLPAPI.lib") int main() … | |
Re: XML is the way to go, baby! Store the form state in a separate object, then serialize that object using XmlSerializer. Separating form data from the form class makes this easy, and here is a class that makes the serialization even easier than it usually is. ;) [code] using System.IO; … | |
Re: If the number of students is not too big, you can load them all into memory. Then it is easy to work with non-sequential data. [code] #include <iostream> #include <map> #include <sstream> #include <string> #include <vector> using namespace std; struct Student { string Id; string Name; vector<int> Grades; Student() {} … | |
Re: The string class and all of the STL container classes grow dynamically. What are you trying to do that those classes cannot accomplish. | |
Re: Fun problem. :) Solving it brute force is easy though. Just go through every element in the big matrix and look for a sub matrix starting at that point. [code] foreach row in bigMat foreach col in bigMat currRow = row for i = 0 to smallMat.Rows currCol = col … | |
Re: The assignment operator works right to left. [code] <target> = <source>; [/code] All of the assignments in the posted code are flip flopped. | |
Re: Have you profiled the running program and confirmed that there is a bottleneck? Why optimize if the program is fast enough already? :) | |
Re: For testing heap memory you can manually throw std::bad_alloc in standard C++ or System::OutOfMemoryException in C++.NET. For stack memory, calling an unconditionally recursive function will eventually overflow the stack. | |
Re: [QUOTE=firstPerson;1240812]Also subtle but still there, this part : [code] char* argv[1] [/code] creates a array of char pointer, with size 1, thus only index 0 is valid. That means that this code :[code] string arg = argv[1]; [/code] is a index out of bounds.[/QUOTE] The array size in a function … | |
Re: > why they are using *fp Edward assumes you mean why is it [CODE]FILE *fp = fopen(...)[/CODE] instead of [CODE]FILE fp = fopen(...)[/CODE] Under the hood, it looks kind of like the following. [code] struct FILE { HANDLE h; /* System file handle */ BUFFER b; /* I/O buffer */ … | |
Re: If array input works and file input does not, the strings you are getting from file are not the same as expected, or you are not searching for a string that was in the file. Verify your data before troubleshooting the hash table. | |
Re: There is probably a .NET method for this, but Edward's first thought was to do it manually with a dictionary. [code] public string MostFrequent(string[] arr) { var freq = new Dictionary<string, int>(); Array.ForEach(arr, s => freq[s] = freq.ContainsKey(s) ? freq[s] + 1 : 1); return (from x in freq orderby … | |
Re: As far as the node definition goes, sure. But even though there are two pointers, the use of those pointers is conceptually very different. | |
Re: > 2 - I request a line of input, as per the first function. Crash! Bang! Why you say crash, do you mean the program crashes? Can you walk Ed through each step with the input and output? Or better yet, post a short program that Edward can run to … | |
Re: > I would use an std::map. Unless the problem is likely to become more general, there is no need for the overhead of a map. [code] int count = 0; char c; while (fin.get(c)) { if (c == 'A') ++count; } cout << "There are " << count << " … | |
Re: scanf's %s specifier stops reading on whitespace. If you want to read a string with embedded whitespace, fgets is the better option. That way you can read a whole line and then continue to parse it if needed. [code] #include <stdio.h> #include "practice.h" #define TWENTY_FIVE 25 int main () { … | |
Re: > scanf("%s", &exp); This line is the problem. First, exp is an uninitialized pointer. As soon as scanf begins writing characters to whatever address exp points to, a segmentation fault is likely. Next, scanf stops reading on whitespace for the %s specifier. With your sample data of "35x + 17y … | |
Re: There are two versions of PDF: the old proprietary format and the newer XML based format. If you do not support the old format, and have the balls to do it manually, any XML library will work. Otherwise, LibHaru might be a start. Ed has not worked with PDF in … | |
Re: You can open the form as a dialog using formObj.ShowDialog(). But that makes the form modal and you cannot navigate away until it is closed. You can also set the ShowInTaskbar property to false for a non-modal form that does not show up in the task bar. If you really … | |
Re: This [URL="http://msdn.microsoft.com/en-us/library/bb776891%28VS.85%29.aspx"]MSDN article[/URL] might be helpful. | |
Re: The problem has to do with modifying a copy of the pointer. Pointers work the same way when it comes to passing by reference as everything else. If you want to change what the pointer points to, another level of indirection is needed. Why? Because *everything* is passed by value … | |
Re: What you call variations are normally called combinations with repetition. Here is a sample program for you that does a subset of what you want. It is not be hard to extend the code to use an arbitrary list of values from a file, but Ed does not want to … | |
Re: std::vector has a back() method that returns a reference to the last element. You are using push_back() to add elements, so the last element will be the most recent. [code] bObjArray.back().header.push_back(currentLineInFile); [/code] at() should work too, as long as the index is bObjArray.size()-1. Edward would need more code to say … | |
![]() | Re: [code] #include <iostream> #include <windows.h> int main() { WIN32_FIND_DATA info; HANDLE file = FindFirstFile("*.txt", &info); if (file != INVALID_HANDLE_VALUE) { struct FileInfo { HANDLE h; WIN32_FIND_DATA info; } newest; newest.h = file; newest.info = info; while (FindNextFile(file, &info)) { if (CompareFileTime(&info.ftLastWriteTime, &newest.info.ftLastWriteTime) > 0) { newest.h = file; newest.info = … |
Re: > I dont like to use recursion, unless it is very much necessary Don't fear recursion, it's cool! ;) Actually, Ed doesn't use recursion either unless it makes the code much shorter or simpler. It's a great tool if you know when to use it and more importantly, when not … | |
Re: strtok ignores empty fields. Edward would recommend using another solution, like strcspn and some method of reading or copying a substring: [code=c] #include <stdio.h> #include <string.h> int main() { const char *str = "source,desti,string1,,string3"; const char *delims = ","; size_t start = 0; while (str[start] != '\0') { size_t end … | |
Re: fgets reads a single line of characters, but fread reads a block of unidentified objects. fgets uses '\n' as a delimiter, but fread doesn't inspect any of the objects so it relies on a limit of the number of objects. If you're using fread to read string data, the only … | |
One problem with C# is that if you want to modify a collection, you have to manually iterate over it. The generic List class supports ForEach, but there's no method that allows global list changes. C# is kind of restrictive in what changes you can make while iterating, so Edward … | |
Edward designed this class as a poor man's named property initializer using syntax similar to C99's designated initializers if they worked in a C++ constructor call: [code] Person me( .FirstName="Radical", .LastName="Edward", .Age=23 ); [/code] | |
Re: On the main IDE window's menu bar choose Project -> {project name} Properties. That opens the project's property pages. You can specify command line arguments under Configuration Properties -> Debugging -> Command Arguments. Just type in exactly what you would type at the command line after the program name. If … | |
Re: >I get a error if I don't have ; after else statement, before sum = 0 [code] else [COLOR="Red"](number < 0)[/COLOR] [/code] The else statement doesn't have a condition; the condition is implied by the accompanying if and else if clauses: [code=cplusplus] if (number >= 0) cout << "Invalid Entry! … | |
Re: Edward's first guess without seeing the code is that you corrupt your memory somewhere and Vista is better at catching it with a hard failure, or you're just getting lucky on XP. But without seeing the code, all Ed can do is guess. | |
Re: [QUOTE=RayvenHawk]Any reason why my stack is displaying backwards??[/QUOTE] That's how a stack works, it's a "last-in, first-out" structure. The item at the top will always be the last item that was pushed: [code=cplusplus] #include <iostream> #include <stack> int main() { using namespace std; stack<int> st; for (int i = 0; … | |
Re: The only thing Edward can think of is using a union like that isn't required to work even if the conversion is safe but a reinterpret_cast<> is. The rules say that assigning to one member of a union and then accessing a different member right away is undefined behavior. | |
Re: [code] [COLOR="Red"]// Star is in the wrong place[/COLOR] void Field::*data(){ return NULL; } [COLOR="Red"]// Star is in the wrong place // void* is the wrong return type[/COLOR] void Field::*clone() const{ return NULL; } [/code] The class name and scope operator go right next to the method name, after the return … | |
Re: According to the rules, you can't post anything related to pirated programs. Edward would assume that includes linking to sites that offer cracked software. | |
Re: [QUOTE=MattEvans]- Is there any way to tell if an i/ostream passed to a function has been opened in binary or text mode? [/QUOTE] Not directly through the object by default. The easiest solution if you need that information is to pass it along with the object as a parameter. To … | |
Re: [QUOTE=daviddoria]I have been using vector in c++ lately, but I needed to speed up my code a bit so someone recommended I use regular arrays.[/QUOTE] Using arrays instead of vectors only saves you the overhead of C++'s object model, especially if you need a dynamically sized array. Instead of playing … | |
Re: The starting form is the first form that's opened. If you're using code generated by the new project wizard there will be a *.cpp file named the same as your project. That's where main() is, and in main() you'll see something like this: [code=cplusplus] // Create the main window and … | |
Re: [QUOTE=Alex Edwards]What I want to understand is how to have better control of data and how it is represented in a class, structure, union or namespace. Basically a recommendation for a book that really goes in depth on how data is represented in all possible data containers.[/QUOTE] One of Ed's … | |
Re: [QUOTE=Rawfel]It feels like it's very inefficient coding considering the program has to repeat the same arithemtic argument for every "cout" and that's why I ended up posting this, I want to know whether it can improved without getting to advanced.[/QUOTE] If you use the same expression more than once, you … | |
Re: Try using the fully qualified name to make sure you've made the namespace visible for the <string> header: [code=cplusplus] std::string q; [/code] | |
Re: That looks like a fun little program. :) To make it do both addition and subtraction, you need to determine what changes between the two paths. Edward sees that you need to display a different character for the equation--either '+' or '-' so the user knows how to figure the … |
The End.