436 Posted Topics

Member Avatar for Covinus

[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: …

Member Avatar for Tom Gunn
1
1K
Member Avatar for low1988

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, …

Member Avatar for Tom Gunn
0
110
Member Avatar for pdwivedi

[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 …

Member Avatar for Salem
0
88
Member Avatar for kz07

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 …

Member Avatar for Ancient Dragon
0
4K
Member Avatar for Gaiety

[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 …

Member Avatar for kvprajapati
0
292
Member Avatar for roberto usu
Member Avatar for ElieM
Member Avatar for ElieM
0
87
Member Avatar for rhesus303

[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 …

Member Avatar for Tom Gunn
0
304
Member Avatar for furqankhyraj

[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 …

Member Avatar for leegeorg07
0
74
Member Avatar for furqankhyraj

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 …

Member Avatar for Tom Gunn
0
86
Member Avatar for MaestroRage

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, …

Member Avatar for Tom Gunn
0
2K
Member Avatar for invisi

[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 …

Member Avatar for invisi
0
141
Member Avatar for prashanth s j

[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 …

Member Avatar for Tom Gunn
0
108
Member Avatar for joisan
Member Avatar for Tom Gunn
0
97
Member Avatar for pspwxp fan

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++.

Member Avatar for Tom Gunn
0
82
Member Avatar for gretty

[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 …

Member Avatar for Tom Gunn
0
98
Member Avatar for Gaiety

[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 …

Member Avatar for Tom Gunn
0
216
Member Avatar for daveyb91

[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 …

Member Avatar for The Dude
0
179
Member Avatar for kamos.jura

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; …

Member Avatar for kamos.jura
0
1K
Member Avatar for himgar

[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 …

Member Avatar for Acute
0
2K
Member Avatar for group256

[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.

Member Avatar for GDICommander
0
233
Member Avatar for MrNoob

'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 …

Member Avatar for MrNoob
0
107
Member Avatar for Der_sed

[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 …

Member Avatar for mrnutty
0
155
Member Avatar for ihatestarch

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 …

Member Avatar for ihatestarch
0
146
Member Avatar for Niner710

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] …

Member Avatar for Tom Gunn
0
4K
Member Avatar for AutoC
Member Avatar for yun

[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 …

Member Avatar for yun
0
211
Member Avatar for rhoit

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 …

Member Avatar for Tom Gunn
0
185
Member Avatar for hmortensen

[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 …

Member Avatar for hmortensen
0
106
Member Avatar for rhoit

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 …

Member Avatar for rhoit
0
270
Member Avatar for nateuni

[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 …

Member Avatar for Tom Gunn
0
1K
Member Avatar for kaninelupus

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 …

Member Avatar for kaninelupus
-1
470
Member Avatar for codedhands

[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) …

Member Avatar for codedhands
0
147
Member Avatar for gretty

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 …

Member Avatar for Tom Gunn
0
119
Member Avatar for xfreebornx

[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. …

Member Avatar for thug line
0
94
Member Avatar for josephe

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 …

Member Avatar for josephe
0
130
Member Avatar for themask18

[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* …

Member Avatar for themask18
0
152
Member Avatar for feroz121

[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 …

Member Avatar for Tom Gunn
0
728
Member Avatar for larrifari

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; …

Member Avatar for Nick Evan
0
4K
Member Avatar for mrfred

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 …

Member Avatar for mrfred
0
90
Member Avatar for MentallyIll5150

[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 …

Member Avatar for MentallyIll5150
0
88
Member Avatar for osei-akoto

[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 …

Member Avatar for Tom Gunn
0
4K
Member Avatar for macobex

[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 …

Member Avatar for dgr231
0
122
Member Avatar for jasonjinct

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", …

Member Avatar for Tom Gunn
0
484
Member Avatar for osei-akoto

Please stop spamming the forum with questions that can be answered using a web search or any reference book on C.

Member Avatar for Tom Gunn
-1
66
Member Avatar for DaBunBun

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.

Member Avatar for Salem
0
135
Member Avatar for xyzt

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; …

Member Avatar for Tom Gunn
0
149
Member Avatar for mbulaheni

[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 …

Member Avatar for mbulaheni
-1
118
Member Avatar for poliet

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); }; …

Member Avatar for poliet
0
220
Member Avatar for tomtetlaw

[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: …

Member Avatar for mrnutty
0
110

The End.