cin/cout under Linux... [Archive] - C Board

PDA

View Full Version : cin/cout under Linux...


Unregistered
06-04-2002, 01:57 PM
I have been a windows user for a long time (far too long), and learned c++ using borland C++Builder 5. Although i am quite proficient at programming in that environment, i recently decided to try out linux (first Slackware 7, now Redhat 7.1).

Obviously, one of the things i tried was to write a program ("hello world..." you know the variety), just to get to know the way of doing things under linux better. To my surprise, i couldn't get cout and cin to work! (i /did/ #include <iostream>;) So I used gets() and puts() instead.

Now, i have read about cin and cout, but never had to use them, because of all the other nice features that builder has, like editboxes and so forth. Can someone please, oh very please show me sample "hello world" code, using cin and/or cout that works in linux?



As a side issue, how do I get rid of the GUI in redhat 7.1 - i want to work in a pure console (not an emulator)? The GUI is nice and dandy, but when i am working in linux, i really don't want to be reminded of windows!

Salem
06-04-2002, 02:09 PM
Did you do these two things as well

1. Put the source code in a .cpp file - eg. prog.cpp
2. use the c++ compiler, not the c compiler - eg. g++ prog.cpp

The result (if all is well) should be a file called a.out, which you run by typing
./a.out

stautze
06-04-2002, 05:14 PM
>As a side issue, how do I get rid of the GUI in redhat 7.1


http://www.cprogramming.com/cboard/showthread.php?s=&threadid=18995

Unregistered
06-04-2002, 05:22 PM
also,

you must bring elements cin and cout into scope from the std namespace with the using keyword (when you are using <iostream> instead of the depreciated <iostream.h>) that's just standard, it's not linux specific, although it's compiler specific...

#include <iostream>

using std::cout;

int main ()
{
cout << "Hello, world!" << endl;
return 0;
}

you can always access a console login with ctrl+alt+F1 through ctrl+alt+F6...alt+ F7 to get back to X....

Unregistered
06-05-2002, 02:23 PM
thanx alot, i will try that tonight.

stautze
06-05-2002, 08:19 PM
>you can always access a console login with ctrl+alt+F1 through ctrl+alt+F6...alt+ F7 to get back to X....

Just remember your xserver it still running.

Unregistered
06-20-2002, 06:06 PM
Just include "stdio.h"
Then you can use all the "C" io functions
eg. printf etc..etc..

onurak
07-09-2002, 03:10 PM
#include <iostream>
using namespace std;

int main()
{

cout <<"hello world"<<endl;
exit(0);
}

save this as example.cpp

in the console

type

g++ -o executable_name example.cpp
and compıle it
the run

kwigibo
07-09-2002, 06:51 PM
onurak wrote

#include <iostream>
using namespace std;

int main()
{

cout <<"hello world"<<endl;
exit(0);
}




Did you even test this, the exit function requires <cstdlib> to work.

Skarr
07-25-2002, 09:53 AM
Erh, guys... This is a "Hello world" program... it shouldn't really be any trouble. Well, just so you don't flame me:

#include <iostream.h>

int main(void) {

cout << "Hello world" << endl;

return 0;

}

Hehe, wouldn't wonder if that doesn't work either, I never use cout... printf kicks. Ass.

onurak
07-26-2002, 05:06 AM
exit function works well with iostream liblary no problem i always use exit() function with iostream liblary