Thread: cin/cout under Linux...

  1. #1
    Unregistered
    Guest

    Question cin/cout under Linux...

    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!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    >As a side issue, how do I get rid of the GUI in redhat 7.1


    http://www.cprogramming.com/cboard/s...threadid=18995
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  4. #4
    Unregistered
    Guest
    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...

    Code:
    #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....

  5. #5
    Unregistered
    Guest
    thanx alot, i will try that tonight.

  6. #6
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    >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.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  7. #7
    Unregistered
    Guest

    Smile

    Just include "stdio.h"
    Then you can use all the "C" io functions
    eg. printf etc..etc..

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    106
    #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
    C++ Makes you Feel Better

    "Gravity connot be held reponsible for people falling in love"--Albert Einstein

  9. #9
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    onurak wrote
    Code:
     #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.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    29

    Huh, this is advanced stuff

    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.
    I like traffic lights. I like traffic lights. I like traffic lights, but only when they're green.

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    106
    exit function works well with iostream liblary no problem i always use exit() function with iostream liblary
    C++ Makes you Feel Better

    "Gravity connot be held reponsible for people falling in love"--Albert Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 11:40 AM
  2. Wireless Network Linux & C Testbed
    By james457 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-11-2009, 11:03 AM
  3. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  4. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM