Thread: Run from command line

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Run from command line

    Hello, my John Strobel. I started teaching myself C++ with books and tutorials earlier this week. I had written a sample code from a book and ran it, it takes the imput but never produces output after i hit enter. I apologize for such a trivial question, but how do I run the program from a command line prompt, or window? Instead of compile and run.

    Thank you for any help, I may be a bit short sighted with something so simple

    #include <iostream>
    using namespace std;


    int n, remainder;

    //Get a number from the keyboard

    cout << "Enter a number and press ENTER: ";
    cin >> n;

    //Get remainder after dividing by 2

    remainder = n % 2;

    //If remainder is 0 then number imput is 7

    if (remainder == 0)
    cout <<"the number is even. ";

    else
    cout << "The number is odd. ";

    return 0;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You type the name of the program in the command prompt and press "enter". You will probably have to navigate to the directory where you put the executable file first.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you could read this
    http://cboard.cprogramming.com/showthread.php?t=13473

    And include all the braces in your code, so that it actually makes sense.

    Simply deleting all the braces, so you can post the code, whilst a novel approach, doesn't actually do anything for us.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    Thank you, I apologize about the braces. Im eager to learn and got a bit anxious to post. its only been a few days and im enjoying it.

    JOHN

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Does the window close after you enter your input? This is very likely since it will flow immediately to the return. You can slow it down by putting a getchar() before return which will wait for you to press a button. That way you don't have to worry about navigating the command prompt.
    Last edited by Drac; 01-25-2009 at 10:47 PM.

  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    Thumbs up

    Quote Originally Posted by Drac View Post
    Does the window close after you enter your input? This is very likely since it will flow immediately to the return. You can slow it down by putting a getchar() before return which will wait for you to press a button. That way you don't have to worry about navigating the command prompt.
    or include <cstdlib> and at the end or you codes , before "return 0;" write system("pause");
    this should do it . no need to test your code out put using a cmd!
    Code:
    #include <cstdlib>
    
    int main ()
    {
    ~
    ~
    ~
    system("pause");
    return 0;
    }
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Is it my morning eyes, or is his code missing main() o.O?
    Currently research OpenGL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  2. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 06:00 PM
  3. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  4. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  5. Replies: 2
    Last Post: 10-29-2002, 04:56 PM

Tags for this Thread