-
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.
-
Apr 24th, 2007, 03:36 PM
#2
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.
-
Apr 24th, 2007, 05:21 PM
#3
Thread Starter
PowerPoster
Re: Frequently Asked Questions
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.
-
Jun 19th, 2007, 10:22 PM
#4
Thread Starter
PowerPoster
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.
-
Sep 2nd, 2007, 01:00 PM
#5
Hyperactive Member
Re: Frequently Asked Questions
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
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 7th, 2007, 07:25 PM
#6
Re: Frequently Asked Questions
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 7th, 2007, 07:32 PM
#7
Hyperactive Member
Re: Frequently Asked Questions
OoOo Sorry, I didn't see that...
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Sep 12th, 2007, 03:39 AM
#8
PowerPoster
Re: Frequently Asked Questions
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 ;
}
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
-
Oct 24th, 2007, 12:09 PM
#9
Addicted Member
Re: Frequently Asked Questions
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)
What is the problem?
If above question or answer will help to you
Don't forget to rate me
தமிழ்இன்பன்
-
Oct 24th, 2007, 12:54 PM
#10
Hyperactive Member
Re: Frequently Asked Questions
did you #include <string>
Haikus are easy.
But sometimes they don't make sense.
Refrigerator.
-
Oct 25th, 2007, 08:50 AM
#11
Addicted Member
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
தமிழ்இன்பன்
-
Oct 26th, 2007, 02:09 PM
#12
Hyperactive Member
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.
-
Oct 28th, 2007, 11:34 AM
#13
Addicted Member
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
தமிழ்இன்பன்
-
Jul 17th, 2009, 05:19 PM
#14
New Member
Re: Frequently Asked Questions
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.
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
|