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++.
Re: Frequently Asked Questions
Slightly inaccurate answer to one point here:
Quote:
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.
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.
Re: Frequently Asked Questions
I've edited the first post to include popular IDEs.
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
Re: Frequently Asked Questions
Quote:
Originally Posted by Bobalandi
Sunburnt does mention in the original post that using code to force the program to stay open isn't recommended.
chem
Re: Frequently Asked Questions
OoOo Sorry, I didn't see that... :blush:
Re: Frequently Asked Questions
Quote:
Originally Posted by sunburnt
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;
}
So basically we can do it in this way, just using two lines.
C++ Code:
#include <iostream>
using namespace std ;
int main()
{
// body of the program
cin.get() ;
return 0 ;
}
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:
#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++.
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):eek:
What is the problem?
Re: Frequently Asked Questions
did you #include <string>
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?
Re: Frequently Asked Questions
do you have the compiler error? can you paste it?
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 :thumb: (click here)
Re: Frequently Asked Questions
Quote:
Originally Posted by
eranga262154
So basically we can do it in this way, just using two lines.
C++ Code:
#include <iostream>
using namespace std ;
int main()
{
// body of the program
cin.get() ;
return 0 ;
}
[/COLOR]
system("PAUSE"); works really well.