436 Posted Topics
Re: [QUOTE]Seems a good text, but I wonder about the choice of using main() as opposed to int main() ? [/QUOTE] C++ evolved from C89, and C89 supports a feature called implicit int. If the type is not explicitly stated, int is assumed. These two definitions were the same in C89: … | |
Re: One problem is that you never actually insert the node in that method. :) Another thing I noticed is that you do not handle edge cases. What happens if you call InsertAfter() on an empty list or the search fails? Inserting to an empty list can be a special case, … | |
Re: [QUOTE][CODE]printf("name=%s",b1.name); printf("\nname=%s",&b1.name);[/CODE][/QUOTE] Technically the second statement is undefined behavior because the %s modifier expects a char pointer, but you pass a pointer to a char pointer. The types do not match. In reality it will probably work because [ICODE]b1.name[/ICODE] and [ICODE]&b1.name[/ICODE] should both evaluate to the same address even if … | |
Re: Unless the lines are formatted to have exactly the same length, getline() is really the best way to do what you want, even if you throw away most of the lines: [code] for (int x = 0; getline(fs, line) && x < selectedLine; ++x) { // all work in condition … | |
Re: [QUOTE]can you explain why is that required?[/QUOTE] I always figured that it is because type names can consist of more than one token, like [ICODE]sizeof(long double)[/ICODE], but variable names are always one token. The parentheses make parsing multiple tokens easier. I cannot think of a good place where the behavior … | |
Re: What does your copy constructor look like for the XboxFriends class? | |
Re: I think you need to create a project and attach those files to the project before you can build and then run. | |
Re: [QUOTE]Is there a way for me to compile them into some shared library object so that I can link them with my visual c++/clr source files using cl.exe.[/QUOTE] You need to rebuild them on a Windows system or use a cross compiler targeting Windows. Otherwise the library file format from … | |
Re: [QUOTE][CODE]printf("\nThe value is: %d", i**pow);[/CODE][/QUOTE] ** is not an operator in C, it will be parsed as [ICODE]i * (*pow)[/ICODE], which should either not compile or throw a runtime exception because pow is not a pointer. The pow() function in math.h is the library way to calculate a power. It … ![]() | |
Re: Can you do it all in main? If all you need to do is refactor, that is relatively easy. Take a simple adder for example: [code=c] #include <stdio.h> #include <stdlib.h> int main() { int a, b; printf("2 numbers to add: "); if (scanf("%d%d", &a, &b) != 2) { fputs("Error reading … | |
Re: If you defer initialization of the struct object, you can do it all in one go: [code=c] #include <stdio.h> typedef struct a { int a; } A; typedef struct b { char ba[2][12]; int bb[12]; } B; int main() { B test = { {"a", "b"}, {1, 2, 3, 4, … | |
Re: [QUOTE]vector sizes are always in unsigned integers, so change the for-iterator i into an unsigned int.[/QUOTE] The type might not be [ICODE]unsigned int[/ICODE]. It is safer to use the actual type that is typedef'd in the vector class: [code=cplusplus] void RandomArrayFill(vector<int>& vec) { cout << "Creating and filling the array … | |
Re: [QUOTE]1)What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?[/QUOTE] The NULL macro is a symbolic name for a null pointer constant. It is defined something like this: [code=c] #define NULL ((void*)0) [/code] or this: [code=c] #define NULL 0 [/code] [QUOTE]2)What is the … | |
Re: Can you post something that I can compile? I cannot reproduce your problem with the code given. | |
Re: I think before you get into Windows and games, you should be very comfortable with just C++. That way you only struggle with one complex thing at a time. ;) [URL="http://www.amazon.com/C-Programming-Language-Special/dp/0201700735/ref=sr_1_1?ie=UTF8&s=books&qid=1251902149&sr=8-1"]This[/URL] is a good second book on C++. | |
Re: [QUOTE]Can you take a look at my program and tell me if it will work for all numbers? Or can you see an exception when this algorithm wont work?[/QUOTE] If you want to be confident in your algorithm, you need to give it good tests before turning it in. The … | |
Re: [URL="http://www.itl.nist.gov/div897/sqg/dads/HTML/completeBinaryTree.html"]Complete Binary Tree[/URL], [URL="http://www.itl.nist.gov/div897/sqg/dads/HTML/fullBinaryTree.html"]Full Binary Tree[/URL], [URL="http://www.itl.nist.gov/div897/sqg/dads/HTML/perfectBinaryTree.html"]Perfect Binary Tree[/URL]. According to those definitions, I would draw the difference like this: [code] // Trees rotated 90 degrees counter clockwise Perfect: Full: Complete: g e c c c f d a a a e e b b b d d [/code] The … | |
Re: [QUOTE]I get a warning just because I made a double topic, and had no idea about how to move the other one.[/QUOTE] You can probably go to the post you want to move and click the Flag Bad Post link to report it. That will get the moderators' attention, and … | |
Re: Normally you just pass the object and the compiler does the rest. No special syntax is needed: [code=cplusplus] #include <iostream> #include <vector> void PrintAll(std::vector<int> const& v) { std::vector<int>::const_iterator x = v.begin(); while (x != v.end()) std::cout << *x++ << '\t'; std::cout << '\n'; } int main() { std::vector<std::vector<int> > v; … | |
Re: [QUOTE]A little searching will always help. There is already a thread for this. Just use the code it shows there for generating a random number between a range, and use a range of 10000000000 to 99999999999.[/QUOTE] [QUOTE]1) Seed random number 2) use rand()/RAND_MAX * (max-min) + min;[/QUOTE] On 32 bit … | |
Re: [QUOTE]You can use std::sort with a STL container, like a vector or a list.[/QUOTE] sort() uses random access iterators, so it will not work on a list. STL lists use bidirectional iterators, but they also support their own sort method. | |
Re: 'Discard' in this case means that the character is extracted from the input stream so that the next read will return the character after it or EOF. getchar() always extracts a character if it can, so if '\n' is next in line, getchar() will remove it from the stream and … | |
Re: [QUOTE][CODE] int c_hrs1 , c_hrs2 , c_hrs3 , c_hrs4; float gpa_1 , gpa_2 , gpa_3 , gpa_4; int total_c_hrs= c_hrs1+c_hrs2+c_hrs3+c_hrs4; float cgpa= ((c_hrs1*gpa_1)+(c_hrs2*gpa_2)+(c_hrs3*gpa_3)+(c_hrs4*gpa_4))/total_c_hrs;[/CODE] [/QUOTE] This will not work because all of those variables used in initializing total_c_hrs and cgpa have not been initialized. You should move the second two lines … | |
Re: Characters are really small integers. '0' is a convenient symbol for the value 48. If you add 1 to 48, you get 49. And 49 is the actual value of the character '1'. Because '0' is a symbol for 48, you can say '0'+1 and still get 49. You can … | |
Re: You have the right idea, but there are a few problems in the details: [QUOTE][CODE]vector <string > string; vector <int> int; [/CODE][/QUOTE] Naming your variables the same as types is not a good idea. Most of the time it will not work the way you want. [QUOTE][CODE]tranform(string.begin(), string.end(), int.begin(), atoi);[/CODE][/QUOTE] … | |
Re: [QUOTE]Recursive functions should be used as sparingly as possible as they are extremely slow...[/QUOTE] Potentially slow. Compilers do not have to do a function call mechanism with the usual stack frame setup that has the lion's share of overhead. For some tail recursive functions, compilers can easily optimize them into … | |
Re: Most of the time when a programmer prints a char*, he wants a string to be printed, not the address. cout assumes that is what you want and to get around it, you need to cast to a different pointer type. void* is a good choice because an object pointer … | |
Re: [QUOTE]Not pretty, so if anyone out there have a more clean way of doing it, please reply[/QUOTE] With a recursive algorithm that is about the best way to do it. A cleaner and more powerful way is by writing an iterator for your BST. Traversal with iterators can be stopped … | |
Re: Member access through pointers is different. If you try to do something like [ICODE]T1.reveal()[/ICODE], you would get that error because the dot operator does not work with pointers. You need to dereference the pointer first, then do the access: [code=cplusplus] (*T1).reveal(); [/code] Because it is such a common operation, there … | |
Re: [QUOTE]Now I am aware that C++ has the try/catch option like Java, but what do I have available in C?[/QUOTE] C can simulate exception handling with the setjmp.h library, but that is kind of an advanced C thing. ;) Usually error handling in C is done by receiving error codes … | |
Re: Daniweb probably has to comply with certain information archival laws. If any threads are physically deleted from the database, that could be grounds for heavy fines or even complete shutdown of the site. Or the information could be used to prosecute spammers, but physical deletion makes coming up with a … | |
Re: [QUOTE]Am looking at up to a billion,i haven't such data yet,but i would like to know if someone has an idea on how it will perform[/QUOTE] The STL map class has performance guarantees that match a balanced search tree. The height will be small for a billion items, like 2*log(n) … | |
Re: I think a more important question than how to make a function recursive is why should it be recursive? Recursion is great, but it has pitfalls. If the recursion goes too deep, you risk a stack overflow with no way to intercept the exception. Hard crashes like that are very … | |
Re: [QUOTE][CODE]int a,b,c; cout <<"average is:"<<average(a,b,c); cin >>a>>b>>c;[/CODE][/QUOTE] I always wonder how it makes sense to do the work before getting the values when the work depends on the values. Try this: [code=cplusplus] int a,b,c; cin >>a>>b>>c; cout <<"average is:"<<average(a,b,c); [/code] Also, the function is not defined to take 3 arguments. … | |
Re: Did you link with Gdi32.lib, or just include windows.h? All headers do is declare names so that the code will compile. Compilation and linking are two different steps in the build process. If you declare a name and do not define it, the code will compile but fail to link … | |
Re: [QUOTE=Hiroshe;957006][URL="http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/"]qsort()[/URL] what your looking for.[/QUOTE] But be careful with the example from that page. Subtracting ints like that without any kind of underflow test has some risk. The safe way is easier to understand too: [code=c] #include <stdio.h> #include <stdlib.h> int Compare(void const* a, void const* b) { int const* … | |
Re: [QUOTE]Now you can get the username by using scanf like this: [CODE]scanf("%s", username);[/CODE] More info about character arrays and the scanf-function can be found at the links which are at the bottom* of this post.[/QUOTE] I have not seen any references that talk about reading strings the right way with … | |
Re: Static fields in a class are really external declarations. You need to define the field outside of the class for it to exist: [code=cplusplus] #include <iostream> class A { public: // external declaration static double ratio; }; // actual definition double A::ratio; int main() { A a; a.ratio = 1.76; … | |
Re: You can still use system() if you want. All it does is pass the string to the command shell, so the string has to look like it would in the command prompt. For names with spaces, wrap the path in double quotations: [code=cplusplus] #include <iostream> #include <string> #include <cstdlib> int … | |
Re: [QUOTE][CODE]main(int ac, char *argv[]) [/CODE][/QUOTE] Even when you do not specify a return type, main returns int, but your main does not have a return value. If you do not return a value from main, a junk value is returned to the OS. Also, C99 does not support implicit int … | |
Re: [QUOTE]In case of main function, if i forgot to write return 0; at the last, GCC compiler generates no error[/QUOTE] If you rely on the compiler to point out every problem in your code, you will spend more time with the debugger than if you know the problem areas that … | |
Re: [QUOTE]Why does the calling function does not reflect the change done in the called function? despite the fact that I pass the variable via pointer.[/QUOTE] Passing by pointer only means that the pointed to object can be changed. If you want to repoint the pointer to another address, it has … | |
Re: If your goal is neat and tidy code, you should be abstracting the records into a struct and using functions to do the dirty work: [code=c] typedef struct StudentScore { char name[SIZE]; int biology, chemistry; } StudentScore; [/code] [code=c] int GetScoreRecord(FILE* ifs, StudentScore* score) { return fscanf(ifs, "%s %d %d", … | |
Re: Please stop spamming the forum with questions that can be answered using a web search or any reference book on C. | |
Re: Old C meaning the latest standard will not allow it. If you compile as C89, the implicit int works, but if you compile as C99, it will throw an error. gcc supports both standards, so it depends on your compiler settings. | |
Re: C does not have pass by reference, but you can fake it by passing pointers and using indirection to get to the original object: [code=c] #include <stdio.h> void nochangex(int x) { x = 123; } void changex(int* x) { *x = 123; } int main() { int x = 0; … | |
Re: [QUOTE]I also have to implement [...][/QUOTE] Right, *you* have to. If you do not know where to start, say so. If you already have something that does not work, post it and somebody can tell you where you went wrong. Otherwise it looks like you are asking for someone to … | |
Re: Cast the void pointer when you want to use it as an int pointer, not when you want to store it as a void pointer: [code=cplusplus] #include <iostream> class A; class B; class A { public: A(): pVar(0){}; void* pVar; }; class B { public: B(){}; void Do(void *passedVar); }; … | |
Re: [code=cplusplus] #include <iostream> void a_func(void (*func)()) { func(); // this should work (*func)(); // more explicit } void test() { std::cout << "test\n"; } int main() { a_func(test); // this should work a_func(&test); // more explicit } [/code] When you have a function, you can do two things with it: … |
The End.