I thought to check if I know the basic things that are needed to write programs in C++.
Just ask me if I know what something is. I want to find the holes in my knowledge.
I thought to check if I know the basic things that are needed to write programs in C++.
Just ask me if I know what something is. I want to find the holes in my knowledge.
"The Internet treats censorship as damage and routes around it." - John Gilmore
Because it may cause buffer overflow.
Warning! I may not know much about C functions because I'm using C++.
"The Internet treats censorship as damage and routes around it." - John Gilmore
It automatically detects std functions, structures and stuff without adding "std::" in front of them when you're using "using namespace std". You can "use" any namespace with that syntax (using namespace foo). This is totally useless if you don't use any of these functions.
"The Internet treats censorship as damage and routes around it." - John Gilmore
Now I will be inactive for 15 hours, it's almost midnight here.
"The Internet treats censorship as damage and routes around it." - John Gilmore
One thing to add to this: you can import only one member of a namespace like so:It automatically detects std functions, structures and stuff without adding "std::" in front of them when you're using "using namespace std". You can "use" any namespace with that syntax (using namespace foo). This is totally useless if you don't use any of these functions.
Code:using std::cout;
- (C question) What's wrong with fflush(stdin)?
- How do you keep a Windows console open?
- If you have only cin.get() at the end of your program to keep the console open, why might it not work?
- (C question) What header file is tolower() in?
- How do you overload the = operator and what are some pitfalls you must avoid when doing so?
- How do you use template specialization?
- Last one . . . what's a "function signature"?
If you want C++ questions to answer, either hang around on this board for a while or get a book. Most books have excersises in them.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
You're disturbing my sleep . Good night everyone.
"The Internet treats censorship as damage and routes around it." - John Gilmore
What is the correct type to use for a variable that can have two discreet values, neither of which has anything to do with the logical concept of truth?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Apart from verbosity, what is the difference between the two expressions 8192 and 8192 * sizeof(char)?
System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.
Useful resources:
comp.lang.c FAQ | C++ FQA Lite
I'd say the real reason that the "using" keyword exists is for porting pre-1998 code onto standard compilers. There was 15 years worth of C++ code that existed before the standard, and making all of that code require hundreds of changes to compile would be unacceptable.Originally Posted by maxorator
I assert that porting legacy code is the only valid reason for having "using" in your code. It decreases readability; putting std:: in front of everything makes it far more clear what's going on.
Anyhow, list every potential problem with the following code, and rewrite the function to get rid of all the problems you've identified. Your code should follow good C++ coding practices.
The code will compile and it does produce the output you expect, so there's no trick question here.Code:void SomeFunction(char * str){ char * buffer = new char[256]; char * buffer2 = new char[256]; sprintf(buffer, "The contents of str are: %s",str); sprintf(buffer2, "Skipping the first 10 characters of str gives: %s", str + 10); std::cout << buffer << std::endl << buffer2 << std::endl; delete[] buffer; delete[] buffer2; }
Last edited by Cat; 10-08-2006 at 06:18 PM.
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
Sorry, just couldn't resist seeing that Here's a code fix:
And here's an algorithm fix:Code:ibvq FbzrShapgvba(pune * fge) { pune *ohssre, *ohssre2; gel { ohssre = arj pune[256]; } pngpu(fgq::onq_nyybp) { fgq::pbhg << "Reebe!\a"; erghea; } gel { ohssre2 = arj pune[256]; } pngpu(fgq::onq_nyybp) { fgq::pbhg << "Reebe!\a"; erghea; } fcevags(ohssre, "Gur pbagragf bs fge ner: %.230f", fge); vs(fgeyra(fge) >= 10) { fcevags(ohssre2, "Fxvccvat gur svefg 10 punenpgref bs fge tvirf: %f", fge + 10); } ryfr { fcevags(ohssre2, "fge unf yrff guna 10 punenpgref"); } fgq::pbhg << ohssre << fgq::raqy << ohssre2 << fgq::raqy; qryrgr [] ohssre; qryrgr [] ohssre2; }
Solution #1 seems rather hackish, though.Code:ibvq FbzrShapgvba(pune * fge) { fgq::pbhg << "Gur pbagragf bs fge ner: " << fge << fgq::raqy << "Fxvccvat gur svefg 10 punenpgref bs fge tvirf: "; sbe(fvmr_g v = 10, yra = fgeyra(fge); v < yra; ++v) { fgq::pbhg << fge[v]; } fgq::pbhg << fgq::raqy; }
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
I usually do put std:: in front of the functions that need it and I don't use the "using namespace std;".I assert that porting legacy code is the only valid reason for having "using" in your code. It decreases readability; putting std:: in front of everything makes it far more clear what's going on.
dwks,
1. The behaviour of stdin with fflush is not defined in all compilers and it's not by the standard (I didn't know this before, I am quite stupid when it comes to C functions)
2.Cin.ignore() is a good solution, but when I am really in a hurry and the code IS STRICTLY only for my use, I just quickly add system("pause"); into the end, which is not very good solution actually.
3. If there are some previous functions used to read from the input stream (cin.get,cin.getline()), then the data that was not read (cin.get() reads only one,cin.getline() reads the number of characters you specify) is automatically passed to the next cin.get() or cin.getline() so the cin.get() function just got the character it needed and returns.Code:std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
4. In C, it is in the <string.h> library.
5. Checking for self-assignment? I haven't used = operator overloading...
6. Do you mean kind of overload the template with a template function that has parameters types specified, so when those types are used in the function, that function template is used?
7. It is some kind of information about the function (parameters, function name, scope and more).
CornedBee, could you just stop it? I don't need to argue with you.
zx-1, the second one works with 16bit, 32bit and 64bit Unicode versions too.
Thanks to dwks I got some "knowledge holes" filled.
Last edited by maxorator; 10-09-2006 at 06:29 AM.
"The Internet treats censorship as damage and routes around it." - John Gilmore