436 Posted Topics
Re: Your code has three immediate problems: [LIST=1] [*]The i variable is not initialized to 0. [*]getchar() is only called once, not every time the loop iterates. [*]The array is not terminated with a '\0' character. [/LIST] If you fix those things, it should work. | |
Re: C++ treats the closing >> as the right shift operator, not two closing brackets for a template instantiation. You need to separate those two characters with white space: [code] Stack<Tree<int> > stack; [/code] C++0x is the next revision of the standard, and it should fix that problem. | |
Re: Containers are implemented using data structures, and the implementation of a container is not usually specified, so containers are a more abstract concept than data structures. | |
Re: [QUOTE]1) Using the getchar() function, you need to flush the input buffer to get rid of the newline character[/QUOTE] As a corollary, [ICODE]fflush(stdin)[/ICODE] is not the way to flush the input buffer because fflush() is only defined to work on output buffers. A portable way to flush is getchar() in … | |
| |
Re: [QUOTE]When I stepped through on the debugger it did not come up, can anyone shed some light on what I am doing wrong?[/QUOTE] Memory corruption can happen and not show symptoms until much later. The code you posted is not broken in any way, so the problem is probably earlier … | |
Re: Your isbal() function only tests the root, it should recursively test every node because there are trees that can be unbalanced but still have a height difference of less than 2 from the root. The definition of height balanced is that when every node is tested as an individual tree, … | |
Re: Try combining the tests like this: [code] int code; while ((code = scanf("%f", &fDollars)) != 1 || fDollars <= 0) { printf("invalid amount. please enter a positive amount: "); fflush(stdout); if (code != 1) FLUSH_STDIN(); } [/code] Further reading for extra credit can be found [URL="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm"]here[/URL]. | |
Re: [QUOTE]the only way i know of is using the initialisation list[/QUOTE] That is the ideal way, and it is required if the Secret class does not have a default constructor. If the Secret class does have a default constructor you can use assignment in the constructor body: [code] class Secret … | |
Re: Then terminate it there. :) If you do not do something to break the line of execution, you will step over each statement in order. Try this to kill the program from the else clause: [code] else { system("pause"); return 0; } [/code] This matches your end of main code, … | |
Re: [QUOTE]having const after a member function means that, that function will not and shall not change any of its variables. [/QUOTE] This is misleading. The method can change any of its local variables and non-const parameters, but it cannot change the state of the object. That means non-mutable data members … | |
Re: [QUOTE]I have been reading about memset but my understanding is it will not work for a struct.[/QUOTE] There is nothing about a struct that causes memset() to fail, the problem is with the member types. [ICODE]memset(pobj, 0, n)[/ICODE] will set n bytes starting at pobj to all bits 0. All … | |
Re: The only built in compiler I remember was for BASIC, a long time ago. I do not think even that is built into the latest Windows systems. If you want to stretch the definition of 'compiler', you can write and run HTML on a fresh Windows install. :D The reason … | |
Re: [QUOTE][CODE]char AorB(char A, char B) { char T = rand%2 ? A : B ; return T; }[/CODE][/QUOTE] [ICODE]rand()%2[/ICODE] might not be very random. Historically rand() has been written with very non-random low order bits, and using modulus to fit the range uses the low order bits. [ICODE]rand()%2[/ICODE] only uses … | |
Re: Not that I can remember. But they are easy to write, so it is not a big loss. :) | |
Re: [QUOTE](which will ensure a cast to an unsigned long, the biggest unsigned integral type)[/QUOTE] Unless size_t is unsigned long or integral promotion makes it an unsigned long, the only thing you ensure is undefined behavior because the type of the value and the type of the format string do not … | |
Re: [QUOTE]I simply didn't know that and it amazed me.[/QUOTE] Why did it amaze you? There are two good options for how to manage function calls in the condition: [LIST=1] [*]Call the function every time the condition is tested. [*]Call the function once and capture the result, then use the captured … | |
Re: IndexOf() returns -1 if it cannot find the value. | |
Re: [URL="http://www.amazon.com/Programming-Environment-Addison-Wesley-Professional-Computing/dp/0321525949/ref=sr_1_7?ie=UTF8&s=books&qid=1253643528&sr=1-7"]Advanced Programming in the UNIX Environment[/URL]. Anything by W. Richard Stevens is good. | |
Re: A 2D managed list of strings with an initial capacity of 1000 would be declared like this: [code] List<List<String^>^>^ vector1 = gcnew List<List<String^>^>(1000); [/code] You can use typedef to make that simpler too. ;) | |
Re: The frequency of a number is how many times it appears in the list. A histogram is a visual representation of the frequency. Start by ignoring the histogram and only display the frequencies as a list of counts. I can think of two easy ways to count the frequency. A … | |
Re: You can pass a comparator to the set as a constructor argument, but it is just as easy to overload operator< for the struct: [code] #include <cstring> typedef struct CONFIG_ { char app[10]; char key[10]; friend bool operator<(CONFIG_ const& a, CONFIG_ const& b) { return std::strcmp(a.key, b.key) < 0; } … | |
Re: [QUOTE]I can get it to save and load but when I try displaying it, its gonna have a General Protection Exception error.[/QUOTE] Your data structure is probably getting corrupted during the load. Maybe you are not terminating one of the lists with NULL, or not handling NULL cells as a … | |
Re: When you say 'strict binary tree', you mean that each node has only two links? You can do it, but if your node struct only has left and right pointers, it is kind of pointless to do a test that is guaranteed at design time. ;) If you mean to … | |
Re: Start by making a subset of the pattern: [code] *0000000* 0*00000*0 00*000*00 000*0*000 [/code] This is easier to visualize with two counters to mark the stars. After you get this one, you can add the central star that does not ever move. | |
Re: [QUOTE]Is this possible that instead of typing - program.week[0].workout[0].exercise[0].exerciseName everytime I want to access an element. That I can access the elements of the most nested struct via a pointer. [/QUOTE] Yes. If you have an object, you can have a pointer to it: [code] #include <stdio.h> struct A { … | |
Re: I cannot say what the problem is without more code to test with, but it might be that your runtime is not flushing stdout. Try printing a '\n' character or calling [ICODE]fflush(stdout)[/ICODE] after the fwrite() to force a flush. | |
Re: C does not support nested functions. You need to move the function definitions outside of main(). | |
Re: [QUOTE=firstPerson;987910]What you could do is, since you are getting inputs, you can make sure that the inputs are greater than the last input. example run : [code] Please enter a number : 3 Please enter a number greater than 3 : 5 Please enter a number greater than 5 : … | |
Re: Assuming the input buffer you are testing is the same input buffer that was allocated, the only thing that stands out in the code you posted is this: [code] inputBuffer->inputPriorityList = createList(inputBufferName) ; [/code] createList() might be returning a null pointer in some cases. | |
Re: Good start! C is different in a few ways though. Declarations need to be the first thing in a block unless you are compiling under C99, so your C code will not compile with most compilers. [QUOTE][CODE]printf("N %2.2f, factorial %2.2f \n" ,i,factorial(i));[/CODE][/QUOTE] You need to be very careful with printf() … | |
Re: [QUOTE]if i have only one node in the list one function displays height as -1 and other displays 0[/QUOTE] Both are right. The function that returns -1 is probably treating an empty tree as an error case and returning a suitable code. The function that returns 0 is treating an … | |
Re: [QUOTE]the program continue showing lines from file even the file don't exist anymore. My problem is that i don't understand why it still prints lines after file deletion.[/QUOTE] I do not know how CentOS manages files in use, but it looks like the file is loaded into either memory or … | |
Re: [QUOTE]I am adding an integer to a type largeInt, but I have not created an overloaded method that accepts largeInt and numerics as input, only one that inputs largeInts and strings. It compiles and runs fine without even a warning.[/QUOTE] The largeInt class has an implicit constructor that takes a … | |
Re: CArray<> is an MFC class, right? The problem is almost surely going to be in your code, not the code of CArray<>. Can you post a small program that has the same error so that everyone can see how you are using the class? | |
Re: An expression tree is a tree, not necessarily a binary tree, that holds the parts of an expression the same way a stack might hold them for prefix evaluation: [code] Expression: 2+(4*3) Stack: [+][2][*][4][3] Expression Tree: + / \ 2 * / \ 4 3 [/code] Look up how to … | |
![]() | Re: Grab the whole line, then parse it. You can parse with string streams using the same syntax as cin: [code] string line; getline(cin, line); istringstream iss(line); int value; while (iss >> value) cout << value << '\n'; [/code] |
Re: [QUOTE]Do we need to make any changes in the structure of tree , i mean any extra pointers in the structure declaration to hold the parent address when going down the tree.[/QUOTE] Having parent pointers can help make the code simpler, and you do not need to worry about managing … | |
Re: [QUOTE]but what if I want the hexadecimal 199 to be displayed in binary?[/QUOTE] printf() does not have a conversion to binary. The C++ library has a bitset<> class that will indirectly do it for you, but you need to use cout to print directly, or manually convert to a string … | |
Re: I think it is better to learn more about C++ than the basics first. You can learn about writing game logic without adding the complexity of graphics to your program. If you start learning how to make graphics with incomplete knowledge of C++, you will end up struggling with the … | |
Re: [QUOTE][CODE]if (i != n){ cout << i << '\n' << n << '\n'; files += line; files += '\n'; } [/CODE][/QUOTE] What is [ICODE]files[/ICODE]? You use that identifier as if it were a string, but it is never declared or defined anywhere. | |
Re: Compilers usually have macros for versioning. You can use those to figure out which compiler is doing the compiling: [code] #if !defined(_MSC_VER) #error Microsoft Visual Studio is required #endif [/code] | |
Re: [QUOTE]I understand both of those facts but I thought that here, I'm only accessing the private members via the friend class, which I thought would be okay.[/QUOTE] Making a friend to a class does not make the private members public. FriendClass can access UnrelatedClass' private members, but those private members … | |
Re: You can call a function provided it has already been declared: [code] void afunction(); class example { public: example(); ~example(); void doSomthing() { afunction(); } }; void afunction() { //do more things } [/code] A definition is also a declaration, so you can move the definition above your class definition … | |
Re: [QUOTE][ICODE]return *nodePtr->data;[/ICODE][/QUOTE] Unless data is a pointer to a pointer, this either returns data or throws an indirection error. To get the address of an object you use the & operator: [code] return &nodePtr->data; [/code] | |
Re: You don't need to store them all at one. Instead, loop it: [code=c++] while (getline(ifs, line)) { // do your thing } [/code] When there aren't any more lines, getline will return a condition that stops the loop. | |
Re: [QUOTE][CODE]for (int j = 1; j <= (2 * i - 1); i++)[/CODE][/QUOTE] Your bug is the reason why I do not use i and j for counters. ;) They look so much alike it is easy to mistype and not be able to see it. In the code above … | |
Re: Dave Sinkula's idea is good, but I think it is easier to see what goes on by printing the intermediate results too, but in binary instead of hex: [code] #include <iostream> #include <limits> #include <bitset> int main() { using namespace std; //int tmp = 0xff000000 | ((int)(val) << 16 | … | |
This is an example of how to turn numbers into words. 1473 would get translated to "one thousand four hundred seventy three", for example. The class has a simple interface and can handle any value representable by unsigned int. The implementation recursively processes groups of 3 digits using table lookups … |
The End.