Run from command line

This is a discussion on Run from command line within the C++ Programming forums, part of the General Programming Boards category; Hello, my John Strobel. I started teaching myself C++ with books and tutorials earlier this week. I had written a ...

  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,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Well you could read this
    << !! Posting Code? Read this First !! >>

    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 09:47 PM.

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

    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, 03:56 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21