Basic things I need to know

This is a discussion on Basic things I need to know within the C++ Programming forums, part of the General Programming Boards category; I thought to check if I know the basic things that are needed to write programs in C++. Just ask ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Basic things I need to know

    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

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,635
    Why is gets() not recommended?

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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

  4. #4
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    why is using namespace std; appropriate?

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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

  6. #6
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    nice

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    One thing to add to this: you can import only one member of a namespace like so:
    Code:
    using std::cout;
    1. (C question) What's wrong with fflush(stdin)?
    2. How do you keep a Windows console open?
    3. If you have only cin.get() at the end of your program to keep the console open, why might it not work?
    4. (C question) What header file is tolower() in?
    5. How do you overload the = operator and what are some pitfalls you must avoid when doing so?
    6. How do you use template specialization?
    7. 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.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You're disturbing my sleep . Good night everyone.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    So much for inactive.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    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

  13. #13
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by maxorator
    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.
    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.

    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.

    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;
    }
    The code will compile and it does produce the output you expect, so there's no trick question here.
    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.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Sorry, just couldn't resist seeing that Here's a code 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;
    }
    And here's an algorithm fix:

    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;
    }
    Solution #1 seems rather hackish, though.
    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;}

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.
    I usually do put std:: in front of the functions that need it and I don't use the "using namespace std;".

    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.
    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    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.

    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

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob with basic q's
    By SimplyComplex in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2006, 12:17 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 07:41 PM
  5. Basic Window Creation, Dev C++ 4.9.9.0 Linking Error
    By Tronic in forum Windows Programming
    Replies: 2
    Last Post: 11-27-2004, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21