Adblock breaks this site

My first program....

Discussion in 'Programming General' started by evil-oreo, May 12, 2008.

  1. evil-oreo

    evil-oreo Member

    Joined:
    Dec 3, 2005
    Posts:
    40
    Referrals:
    0
    Sythe Gold:
    0
    My first program....

    //Hello Sythe.org
    //My first C++ program

    #include <iostream>

    using namespace std;

    int main ()
    {

    cout << "Hello Sythe.org!" << endl;
    return 0; /* I dont know how to make the window stay open though.
    Also, this is all from memory. I think I posted a couple programs before, but copyed someone else.*/

    }
     
  2. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    My first program....

    This shows you how to keep it open by waiting for user input.

    Code:
    //Hello Sythe.org
    //My first C++ program
    
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    cout << "Hello Sythe.org!" << endl;
    cout << "Press any key to exit...";
    getchar();
    return 0; 
    /* I dont know how to make the window stay open though.
    Also, this is all from memory. I think I posted a couple programs before, but copyed someone else.*/
    }
     
  3. fareed

    fareed Newcomer

    Joined:
    Jul 2, 2005
    Posts:
    2
    Referrals:
    0
    Sythe Gold:
    0
    My first program....

    hi evil-oreo,

    If you used dev c++ compiler, add

    system("pause");

    example:


    #include <iostream.h>

    using namespace std;

    int main ()
    {

    cout << "Hello Sythe.org!" << endl;

    system("PAUSE");

    return 0;

    }

    you can also clear the screen by adding:

    system("cls");

    example:

    #include <iostream.h>

    using namespace std;

    int main ()
    {

    cout << "Hello Sythe.org!" << endl;

    system("cls");

    cout << "Hello world!" << endl;

    system("PAUSE");

    return 0;

    }
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    My first program....

    That's terrible, terrible code. What if I use a UNIX or *NIX environment? DOS commands are worthless there.

    Use cross-platform libraries, if you are to use any at all. It'll save you a lot of trouble.

    Edit: In response to the first post, to keep the window open, use the following:

    Add the following header:
    Code:
    #include <conio.h>
    Then before you return 0 from your main method, add this:
    Code:
    getchar();
    simple, cross-platform (I think =/) and works generally effectively.
     
  5. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    My first program....

    1. The getchar() function is defined in the <iostream> include and is therefore cross-platform.

    2. The conio.h header which does not include getchar() is not cross-platform. It does however include the similar getch() function which you might have been thinking of.;)
     
  6. fareed

    fareed Newcomer

    Joined:
    Jul 2, 2005
    Posts:
    2
    Referrals:
    0
    Sythe Gold:
    0
    My first program....

    First of all i am sorry for providing uncross-platform libraries since I've never been thought about it in my college. And Swan can you pls tell me what is the function to clear the screen? A cross-platform one of course.

    Thanks 4 the helps.
     
  7. MavFan07

    MavFan07 Member

    Joined:
    Dec 18, 2007
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0
    My first program....

    Nice program ;)

    Here is my recommendation:

    Code:
    //Hello Sythe.org
    //My first C++ program
    
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    char quit;
    cout << "Hello Sythe.org!" << endl;
    cout << "Press any key to quit: ";
    cin >> quit;
    return 0;
    }
    simple. Some of the other post are good as well, but i'm not sure if you understand the libraries yet, and I don't think you should be using libraries in your code if you don't understand them.
     
  8. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    My first program....

    Thankyou for clearing that up ;)

    Now that I try it, Conio does not work on my Linux setup as I thought it would. And yes, I was thinking of getch() ;)

    To clear the console output? I've never really had to, and more or less, don't want to. However, you could always Google it. The DOS command for clearing is "cls", where as Linux is just "clear", makes more sense so far as I'm concerned.
     
  9. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    My first program....

    That is correct but unfortunately on Linux you cannot physically "clear" the terminal window. The clear() function simply pushes all of the current characters up until the window is filled with blank lines. :)
     
  10. opensourced

    opensourced Guest

    Referrals:
    0
    My first program....

    Windows is a large cooperate cross platform API because of Wine.
     
  11. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    My first program....

    On my setup it clears the console. Must be the way the developers make it happen.

    opensourcecd, Wine is a 3rd party compatibility layer for people wishing to use Windows programs without actually installing Windows. It isn't perfect, and fails with most corporate software.
     
< Freee *hit@@@@@ | [SOURCE] Command Prompt Pacman in C >


 
 
Adblock breaks this site