Thread: coding practices

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    coding practices

    Hi, I'm trying to migrate to C++ from C (which I had a basic knowledge of). I'm just wondering, what should I use:

    Code:
    using namespace std;
    char x[50];
    cout << "Hello\n";
    cin >> x;
    as an example, or:

    Code:
    std::cout << "hello\n";
    std::string x;
    std::cin >> x;
    or something like that?

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, you should definitely use the std::string type to hold sequences of characters, there's no doubt about it.
    I hate real numbers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Rather the second. The first example is vulnerable to buffer overflows if user types more than 49 characters. The second is not. (Use smart containers over raw arrays, smart pointer over raw pointers).

    You should probably not use using namespace in new code, but it's not that bad as long as you don't do that in a header.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM