|
-
Apr 23rd, 2007, 06:14 PM
#1
Thread Starter
PowerPoster
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:
- 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.
- Equally as good: Run your program from the command line. (IE, use "cmd.exe")
- 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:
#include <string>
#include <iostream>
using namespace std;
// ...program is over.
cout << "Press enter to exit." << endl;
string useless;
getline(cin, useless);
return 0;
}
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:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello world";
cout << s << endl; // print "hello world"
cout << "that's " << s.length() << " characters." << endl;
cout << "'world' starts at the index: " << s.find("world") << endl;
cout << s.substr(0, s.find(" ")) << " is what is before the first space" << endl;
}
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|