Menu

Very new to c++...need help with looping

kevinh1022
2007-08-29
2012-09-26
  • kevinh1022

    kevinh1022 - 2007-08-29

    Hi, im new to c++, and right now im trying to get a program to loop....actually its more of a count down...Im trying to make it so the Program says "Please enter starting number>_" and when you enter the number, it will show a count down and then eventually display a message...Here is the code i am using-

    include <iostream>

    using namespace std;

    int main ()
    {
    int n;
    cout << "Enter the starting number > ";
    cin >> n;

    while (n>0) {
    cout << n << ", ";
    --n;
    }

    cout << "FIRE!\n";
    return 0;
    }

    Now, when i enter the number, the dos window just closes...

    I think i need to insert a pause command somewhere...but im not sure where...if not pause, what is it? help would be greatly appreciated.

     
    • Gairnok

      Gairnok - 2007-09-06

      You can also run command.com and switch to that directory. That is an alright method if you want to see your programs output without using a pause command.

      Oh and welcome to C!

      Now stop using those nasty Cout commands, they make me eyes hurt! ;)

      Gair

       
    • David

      David - 2007-08-29

      Replace your line "return 0" with:

      system(&quot;PAUSE&quot;);
      return EXIT_SUCCESS;
      
       
    • kevinh1022

      kevinh1022 - 2007-08-29

      Hey, thanks alot!

       
    • Anonymous

      Anonymous - 2007-08-29

      When a program completes its windows closes - normal behaviour; how have you never noticed that before!?

       
    • Wayne Keen

      Wayne Keen - 2007-08-29

      One small point as well - it is not a "dos window", it hasn't been a "DOS" window
      for quite a few years now.

      As Clifford pointed out, when the program ends, the window closes - SO - solutions
      to keep the window open involve keeping the program from finishing. You can even
      do it as simply, and crudey like this

      int waynesadummy;
      cin >> waynesadummy;

      I mention this to avoid the "magic word" way of thinking. The key to getting things
      to work is not knowing the magic word, its understanding what is happening and why.
      If you understand the what and why, the how is a lot easier to work with.

      So, as you are learning things, please, try to avoid the trap of "OK, there is
      a magic word to do that, that's all I need to know, I don't need to know why
      it works"

      Wayne

       
    • Nobody/Anonymous

      How had i never noticed it before? I told you...im very new to c++...cut me some slack.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.