Results 1 to 14 of 14

Thread: Frequently Asked Questions

  1. #1

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Frequently Asked Questions

    A lot of pretty basic questions seem to come up over and over. Let's cover those answers here.

    Question: What is a good free compiler and/or IDE?
    Answer:
    Windows:

    Linux:
    On linux, you'll generally see a separation between IDE and compiler. By far the most popular compiler for anything on linux is gcc. g++ is the gcc c++ compiler, which can be installed however your distribution installs software.
    IDEs include kdevelop, Code::Blocks, Anjuta, or pretty much any text editor such as emacs or vi(m).

    Question: When I run my console program, the console window doesn't stay open, so I can't see the output. What am I doing wrong?
    Answer: Nothing. When a command line program is finished, it's supposed to return you back to the command line, not hang until you "press any key..." Of course, this behavior is really troublesome when you are debugging your program. There are a couple different things you can do:
    1. Most recommended: Use an IDE that keeps the console window open for you. Visual C++ Express, Visual Studio, and Code::Blocks all fall under this category.
    2. Equally as good: Run your program from the command line. (IE, use "cmd.exe")
    3. Least recommended: Insert a line of code that keeps your program running until you press a certain key.


    If you are hell-bent on the last option, a nice solution (using C++) is as follows:

    C++ Code:
    1. #include <string>
    2. #include <iostream>
    3.  
    4. using namespace std;
    5.  
    6.    // ...program is over.
    7.    cout << "Press enter to exit." << endl;
    8.    string useless;
    9.    getline(cin, useless);
    10.    return 0;
    11. }


    Question: What is the difference between #include <foo.h> and #include "foo.h" ?
    Answer: Strictly speaking, none: in both cases it is completely up to the compiler where it searches for the files (if it does at all).
    However, most compilers behave the same way: #include <name> will search the compiler's "include path" for the file 'name'. #include "name" will first search the directory the including file is in, and then the include path.
    Based on this, the general convention in usage is that #include "name" is used for headers that are part of the current project, and #include <name> for all others, such as standard library headers or 3rd party library headers.

    Question: What is the difference between #include <iostream.h> and #include <iostream> ?
    Answer: The first is wrong. It may work with some older compilers, but most modern compilers will either issue a warning or completely fail to find the file. Suffice to say that all STL headers should not have a ".h" appended to them.

    Question: How do I work with strings in C/C++?
    Answer: The answer depends greatly on whether or not you are using C or C++.
    With C++, all you need to do is include the <string> header. You can then create and use "std::string"s which provide a ton of useful functions and simplify working with strings.

    Example:
    C++ Code:
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8.     string s = "hello world";
    9.     cout << s << endl; // print "hello world"
    10.     cout << "that's " << s.length() << " characters." << endl;
    11.     cout << "'world' starts at the index: " << s.find("world") << endl;
    12.     cout << s.substr(0, s.find(" ")) << " is what is before the first space" << endl;
    13. }
    If you are using plain C, you will need to learn about character arrays and the associated functions such as strcpy(), strcat(), and so on. String handling is C is much more complicated than in C++.
    Last edited by sunburnt; Jun 19th, 2007 at 10:22 PM.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Frequently Asked Questions

    Slightly inaccurate answer to one point here:

    Question: What is the difference between #include <foo.h> and #include "foo.h" ?
    Strictly speaking, none: in both cases it is completely up to the compiler where it searches for the files (if it does at all).
    However, most compilers behave the same way: #include <name> will search the compiler's "include path" for the file 'name'. #include "name" will first search the directory the including file is in, and then the include path.
    Based on this, the general convention in usage is that #include "name" is used for headers that are part of the current project, and #include <name> for all others, such as standard library headers or 3rd party library headers.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Frequently Asked Questions

    Quote Originally Posted by CornedBee
    Slightly inaccurate answer to one point here:


    Strictly speaking, none: in both cases it is completely up to the compiler where it searches for the files (if it does at all).
    However, most compilers behave the same way: #include <name> will search the compiler's "include path" for the file 'name'. #include "name" will first search the directory the including file is in, and then the include path.
    Based on this, the general convention in usage is that #include "name" is used for headers that are part of the current project, and #include <name> for all others, such as standard library headers or 3rd party library headers.
    Thanks for the reply! I've edited the post to contain your answer.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  4. #4

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Frequently Asked Questions

    I've edited the first post to include popular IDEs.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    &nbsp;
    Posts
    453

    Re: Frequently Asked Questions

    Quote Originally Posted by sunburnt
    Question: When I run my console program, the console window doesn't stay open, so I can't see the output. What am I doing wrong?
    Answer: Nothing. When a command line program is finished, it's supposed to return you back to the command line, not hang until you "press any key..." Of course, this behavior is really troublesome when you are debugging your program. There are a couple different things you can do:
    Couldnt you add
    c Code:
    1. cin.get();
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Frequently Asked Questions

    Quote Originally Posted by Bobalandi
    Couldnt you add
    c Code:
    1. cin.get();
    Sunburnt does mention in the original post that using code to force the program to stay open isn't recommended.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    &nbsp;
    Posts
    453

    Re: Frequently Asked Questions

    OoOo Sorry, I didn't see that...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  8. #8
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Frequently Asked Questions

    Quote Originally Posted by sunburnt
    C++ Code:
    1. #include <string>
    2. #include <iostream>
    3.  
    4. using namespace std;
    5.  
    6.    // ...program is over.
    7.    cout << "Press enter to exit." << endl;
    8.    string useless;
    9.    getline(cin, useless);
    10.    return 0;
    11. }

    So basically we can do it in this way, just using two lines.

    C++ Code:
    1. #include <iostream>
    2.  
    3. using namespace std ;
    4.  
    5. int main()
    6. {
    7.    // body of the program
    8.   cin.get() ;
    9.   return 0 ;
    10. }
    Last edited by eranga262154; Sep 12th, 2007 at 03:44 AM. Reason: Incorrect
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  9. #9
    Addicted Member thamizhinpan's Avatar
    Join Date
    Dec 2005
    Location
    TE
    Posts
    243

    Re: Frequently Asked Questions

    Quote Originally Posted by sunburnt
    Question: How do I work with strings in C/C++?
    Answer: The answer depends greatly on whether or not you are using C or C++.
    With C++, all you need to do is include the <string> header. You can then create and use "std::string"s which provide a ton of useful functions and simplify working with strings.

    Example:
    C++ Code:
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8.     string s = "hello world";
    9.     cout << s << endl; // print "hello world"
    10.     cout << "that's " << s.length() << " characters." << endl;
    11.     cout << "'world' starts at the index: " << s.find("world") << endl;
    12.     cout << s.substr(0, s.find(" ")) << " is what is before the first space" << endl;
    13. }
    If you are using plain C, you will need to learn about character arrays and the associated functions such as strcpy(), strcat(), and so on. String handling is C is much more complicated than in C++.

    But when I try string, I can see this error message.

    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable
    conversion)


    What is the problem?
    If above question or answer will help to you
    Don't forget to rate me
    தமிழ்இன்பன்

  10. #10
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    &nbsp;
    Posts
    453

    Re: Frequently Asked Questions

    did you #include <string>
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  11. #11
    Addicted Member thamizhinpan's Avatar
    Join Date
    Dec 2005
    Location
    TE
    Posts
    243

    Re: Frequently Asked Questions

    No! I have declared it already.
    But after I copied the same code (fully) to another project, it was working correctly.
    I think this is a Visual C++ compiler error.
    Is it correct?
    If above question or answer will help to you
    Don't forget to rate me
    தமிழ்இன்பன்

  12. #12
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    &nbsp;
    Posts
    453

    Re: Frequently Asked Questions

    do you have the compiler error? can you paste it?
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  13. #13
    Addicted Member thamizhinpan's Avatar
    Join Date
    Dec 2005
    Location
    TE
    Posts
    243

    Red face Re: Frequently Asked Questions

    Sorry!
    I didn't tell that must be a compiler error.
    But I think it; (I'm a beginner for Visual C++. So some times I have misunderstanding)
    I have already paste the error code.
    Any way I'll place it again

    Code:
    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable
    conversion)
    But I faced many problems like this when I using my Visual C++ 6.0 (Enterprise Edition).
    You can see my next problem on another thread (click here)
    If above question or answer will help to you
    Don't forget to rate me
    தமிழ்இன்பன்

  14. #14
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: Frequently Asked Questions

    Quote Originally Posted by eranga262154 View Post
    So basically we can do it in this way, just using two lines.

    C++ Code:
    1. #include <iostream>
    2.  
    3. using namespace std ;
    4.  
    5. int main()
    6. {
    7.    // body of the program
    8.   cin.get() ;
    9.   return 0 ;
    10. }
    [/COLOR]

    system("PAUSE"); works really well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width