C++ quickness, please help :)

This is a discussion on C++ quickness, please help :) within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm Hussain Hani from Kuwait i'm 15 years old learning C++ from the book : Beginning C++ game programming. ...

  1. #1
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186

    C++ quickness, please help :)

    Hi, i'm Hussain Hani from Kuwait i'm 15 years old learning C++ from the book :
    Beginning C++ game programming. And i'm having a problem that once i run the program it closes in second although i wrote the code that stops that. This is my full code :
    Code:
    //Working with variables 
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int score = 7;
        cout << "Score: " << score << endl;
        score++;
        cout << "Score: " << score << endl;
        ++score;
        cout << "Score: " << score << endl;
        
        cout << "Press enter to exit this program." << endl;
        cin.ignore(cin.rdbuf()->in_avail()+1);
        
        return 0;
    }
    I'm waiting

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Huh ? Replace your line with this:
    Code:
    cin.ignore();
    cin.get();
    Simple as that.

    I'm surprised that your code doesn't work, though, before endl is supposed to flush the buffer =/

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    still does not work ;(
    Code:
    //Working with constants 
    
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        const int alien_points = 150;
        int aliensKilled = 10;
        int score = aliensKilled * alien_points;
        cout << "Score: " << score << endl;
        
        enum difficulty {novice, easy, normal, hard, unbeatable};
        difficulty myDifficulty = easy;
        
        enum ship {fighter=25, bomber, cruiser=50, destroyer=100};
        ship myShip = bomber;
        cout << "\nTo upgrade my ship to cruise will cost " << (cruiser-myShip) << "resource points.\n";
        
        cin.ignore();
        cin.get();
        
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    What doesn't work? Is it closing too quickly?

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    First of all make sure you are running your code in debug mode. It may exit because of an exception.

    [edit]
    Look at the program exit code, to see if it is 0 or not.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    Quote Originally Posted by Daved
    What doesn't work? Is it closing too quickly?
    yep, by the way i use DEV-C++

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Please send the real code you've written.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    I actually thought that was his code.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Are you sure you are actually executing the program. Since there is no input, that should show the output and then wait for you to hit enter twice before closing the console window.

    Try opening a command prompt and navigating to the directory that has your executable and running it from there.

  10. #10
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    I'm new to this Dave, please can you tell me how ". I really know that I'm causing inconvenience

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    With Dev-C++ I'm assuming you're using Windows. You should know where your executable is because it is where you created your project. First, go to the windows explorer and navigate to the directory where your project is, and look for an exe file. If you don't see it, look in the Debug directory. When you find it, double-click on it. This should show your output and wait for you to hit enter twice before closing. If you cannot find it, look harder, or check Dev-C++ to see if there were any compile errors.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    903
    Usually something like this:

    - Click on start, then "run" and type "cmd"
    - From there, type "cd c:\program files\documents and settings\hussain hani\my documents\my c++ game\" (I just invented that one, just replace it with the actual path of your project)
    - Type "dir" to make sure that you are in the correct directory
    - If you see "your_project_name.exe" then just type it.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    include this line at the top of the program:

    Code:
    #include <conio.h>
    and this line at the end of main, before return 0; :

    Code:
    getch();

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If the posted code didn't stay open, that shouldn't work either. The problem is somewhere else.

  15. #15
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    Quote Originally Posted by limitedmage
    include this line at the top of the program:

    Code:
    #include <conio.h>
    and this line at the end of main, before return 0; :

    Code:
    getch();
    that works


    thaaaaaaaanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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