Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Basic C++ Help

  1. Jun 3, 2010 #1
    I have been creating simple programs from the hello world one to simple math and loops - all of a sudden when I went to practice more today my programs would be completed but the window showing the result would pop up and almost instantaneously close itself - in the end I cant see the result, here is a simple example where code seems fine no errors are shown but the result window is not staying up long enough:

    #include <iostream>
    using namespace std;

    int main ()
    {
    int a, b;
    int result;

    a=5;
    b=3;
    result=a+b;

    cout << result;

    return 0;
    }

    I am new to c++, so I have no idea where to even look to fix this problem - it seems like the result window is not staying up long enough - any ideas? and please inform me if I am not being clear enough
     
  2. jcsd
  3. Jun 3, 2010 #2
    Add #include <conio.h> and use 'getch();' right before 'return 0;'. getch() waits for a character to be entered (and returns the value of it).
     
  4. Jun 3, 2010 #3
    hm conio.h ? but I did apply the getch and the same issue occured
     
  5. Jun 3, 2010 #4
    I added the two lines. Now it should work.

    Code (Text):
    #include <iostream>
    #include <conio.h> // I added this

    using namespace std;

    int main ()
    {
    int a, b;
    int result;

    a=5;
    b=3;
    result=a+b;

    cout << result;


    getch(); // I also added this

    return 0;
    }
     
  6. Jun 3, 2010 #5
    awesome, it worked, Thank You! my question is why is neither the website I am using nor the book I have include that bit?
     
  7. Jun 3, 2010 #6
    I don't know, but I think it is because it is not "proper" C++ to do it this way. getch(); is a command which returns the value of the next character to be typed, which is why it exits when a new character is typed in. It just happens to give us what we want, i.e. time to see the result of our code.

    Good luck.
     
  8. Jun 3, 2010 #7

    Borek

    User Avatar

    Staff: Mentor

    OS? IDE?

    Under Visual Studio what happens depends on whether you run your program using F5 or Ctrl-F5. I never remember which one is which - but one stops displaying the window after the execution ended and waits for anykey. And it has nothing to do with C or C++.
     
  9. Jun 3, 2010 #8

    Mark44

    Staff: Mentor

    If you run your original program in a command prompt, it should work as expected. OTOH, if you run your program in an IDE such as Visual Studio, the IDE will open a separate command window, display the result, and then close.
     
  10. Jun 3, 2010 #9
    It is Ctrl-F5, which pauses.

    And I did not say it has anything to do with C or C++, but that the reason why my suggestion is not mentioned in a book is because it is bad practice.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook