Thread: The code doesn't stay!

  1. #1
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321

    The code doesn't stay!

    Hi, ive just signed up, ive wrote my first C++ program, but it terminates as soon as i enter an interger, plz help me!

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int x;
      cout << "Please enter an interger to continue...";
      cin >> x;
      if (x < 0)
      cout << "You entered a negative number! Program terminated!";
      else if (x > 0)
      cout << "Thank you for participating in this program!" << endl;
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    thank you for trying to help, but i dnt understand

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Maybe if you read the link provided again?...
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Yes, thank you, but i want to run it from my compiler/editor

    any ideas?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  7. #7
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    it still doesnt work
    this is the code ive got now...

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int x;
      cout << "Please enter an interger to continue...";
      cin >> x;
      if (x < 0)
      cout << "You entered a negative number! Program terminated!";
      else if (x > 0)
      cout << "Thank you for participating in this program!" << endl;
      cout << "Press enter to continue...";
      cin.get();
      return(0);
    }

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Place cin.ignore() before the cin.get().

    (it's about time someone changes that FAQ )
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Yey, thank you it works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does old code stay stuck in my console?
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2008, 07:29 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM