ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey, so I'm a beginner programmer, and I tried running some code with this off another programming website so I could better understand how to use character arrays:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char ask[]= "Enter your first name: ";
char answer[]= "So your name is ";
char name[100];
I found out that in order for this to work, you need the iostream header, which is either just <iostream> or <iostream.h>, which didn't work because this file is not on my computer. I even downloaded the newest gcc gnu compiler by itself to see if it would be in there, but it isn't!
Is this considered an obsolete way to program? What the hell is going on?
I found out that in order for this to work, you need the iostream header, which is either just <iostream> or <iostream.h>, which didn't work because this file is not on my computer. I even downloaded the newest gcc gnu compiler by itself to see if it would be in there, but it isn't!
I'm surprised about that. Modern gcc compilers usually have the ability to compile C or C++ code and include the <stdio.h> or <iostream> files as a matter of course.
Are you saying that the files are not installed on the computer but they will be included in a program anyway if the source code contains the statements #include <stdio.h> or #include <iostream>?
If you include <iostream> it is you who must ensure its availability, the same with <stdio.h>. If you include iostream in your C-code (normally an error), *AND* the header is available, the C-compiler acts schizophrenic and compiles it anyway.
I cannot say that I appreciate this laxity. In my opinion, C should stay C and C++ should stay C++. The compiler should explode or make the display rotate or fart or whatever, when the code is mixing headers or other elements of both languages. But someone has certainly had a reason to make it act differently.
Guess we are over-complicating this. OP couldn't compile his C-program, for different reasons: 1) it wasn't a C-program. 2) he couldn't include 'iostream' header, because he had never installed it on his computer.
It's hardly a complicated matter. OP downloaded and installed the newest gcc compiler but says that the file <iostream> wasn't there. That's what surprised me.
#include <iostream>
using namespace std;
int main ()
{
string ask = "Enter your first name: ";
string answer = "So your name is ";
string name;
cout << ask;
ask >> name;
cout << answer << name << ".\n";
return 0;
}
Save it as "test.cpp" and build it with:
Code:
g++ test.cpp
That will produce a file called "a.out". Run it with:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.