C++ Question

Discussion in 'Off Topic' started by xburrow, 28 January 2013.

  1. xburrow

    xburrow ayyyyy luhmaoh

    Joined:
    23 January 2013
    Messages:
    137
    Likes Received:
    26
    So I recently started to go on C++ classes and part of my homework is to make a program with "A" and "B" and to switch their number.If a=5 and b=2 I have to output b=2 a=5.That is not hard, I already made it but if I run the program outside "CodeBlocks" It immediately closes after I am done with it. I hope there are some C++ programmers on here. [​IMG]

    Here is my code:
    Code:
    #include<iostream>
    using namespace std;
    int main(){
    long long a, b, c;
    cout<<"enter A:"<<a;
    cout<<"enter B:"<<b;
    c=a, a=b, b=c;
    cout<<"A="<<a<<endl;
    cout<<" B="<<b<<endl;
    return 0;
    }
    
     
  2. Teublyster

    Teublyster Active Member

    Joined:
    20 May 2012
    Messages:
    101
    Likes Received:
    40
    It immediately closes after it is done because you haven't told it to do something else. You need to insert a line of code saying: stay open until I do [undefined action]
     
  3. Chaeris

    Chaeris Active Member

    Joined:
    8 March 2012
    Messages:
    766
    Likes Received:
    89
    I don't program in C++, but in most of the languages, there is one command-line:


    pause


    It stops the program until you press a key (or enter most of the time).
     
  4. theunknownxy

    theunknownxy Member

    Joined:
    20 May 2012
    Messages:
    14
    Likes Received:
    6
    As far as I know only the Windows Batch language has this command.
    For C++ he is probably searching this:
    Code:
    // Declared in iostream in the namespace std
    cin.get() // Read a single character
     
  5. sk89q

    sk89q Administrator
    Staff Member

    Joined:
    1 December 2011
    Messages:
    2.490
    Likes Received:
    1.504
    If you are using Visual Studio (Express), there's a setting to make the window stay open as well after execution I think, but what theunknownxy suggested works as well.