I just downloaded Dev-C++ and tried typing code. My first test was

#include <iostream>
using namespace std;

int main()
{
cout << "Enter height in centimeters: ";
int centimeters;
cin >> centimeters;
cout << "Your height in feet " << centimeters << endl;

return 0;
}

But I have discovered that no matter what code I use, if I have the line "cout << "Your height in feet " << centimeters << endl;" then if I press the enter(return) key my program window closes. Can someone please tell me why? Thanks

Recommended Answers

All 8 Replies

It is working as expected. You created a simple console that operates on a command line. When a console application is loaded by double-clicking or otherwise running it from the GUI, Windows creates a temporary console, executes the program, and then closes the console when you press a key after the code has finished executing.

If you want the console to stay open after the program has executed so you can execute other programs, click "Start", click "Run...", type "cmd", click "OK", change the path to the folder where your program is located, and then execute the program.

cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();

put that code right before return 0; and ur program will not exit untill you press enter.

>cin.ignore(numeric_limits<streamsize>::max(), '\n');
If you're going to use this solution, you need to include <limits> to get access to std::numeric_limits<>.

thx for the help. my program will atleast let me press enter and then show me answer to my question, but closes if i press enter again. is there an easy way to loop it so that after it shows answer it starts question again so you can try a different number without closing the window? thx :)

#include <iostream>

using namespace std;

int main()
{
char ch = 'j';
  
	while(ch == 'j' || ch == 'J')
	{
		cout << "Enter height in centimeters: ";
		int centimeters;
		cin >> centimeters;
		cout << "Your height in feet " << centimeters << endl;

		cout<<"Wish to enter a new number? Press j or J to continue!\n"
			  "Press any other key to quit!\n";

		cin>> ch;
	}

	cout<<endl;

	return 0;
}

Hope this helps ;)

Don't forget to multiply your centimeters by 0.033 to get the height in feets!

ok, your advice was much appreciated ;) I've now tried to create a calculator that tells you how many days have passed since year 0000, but am having a bit of trouble. can someone please help :D thx

also, i'm probably writing more code than i should be, any shortcuts would be very nice too

#include <iostream>
using namespace std;

int main()
{
    char ch ='j';
    
         while(ch == 'j' || ch == 'J')
         
           {
           //Declare variables
           int day;
           int year;
           int month;
           int leapyear;
            
           //Get input
            cout << "Enter year (yyyy): ";
            cin >> year;
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                leapyear = 1;
            cout << "Enter month (mm): ";
            cin >> month;
            cout << "Enter day (dd): ";
            cin >> day;
              {
                if (leapyear = 1) if (month > 1)
                day = day + 29;
                if (leapyear = 0) if (month > 1)
                day = day + 28;   
              }     
            
              {       
                if (month = 1) 
                month = (31 * 1);
                
                if (month = 2)
                month = (31 * 1); 
                
                if (month = 3) 
                month = (31 * 2);
                
                if (month = 4)
                month = (31 * 2) + (30 * 1);
                
                if (month = 5)
                month = (31 * 3) + (30 * 1);
                  
                if (month = 6) 
                month = (31 * 3) + (30 * 2); 
                       
                if (month = 7)
                month = (31 * 4) + (30 * 2);
                 
                if (month = 8) 
                month = (31 * 5) + (30 * 2);
                
                if (month = 9) 
                month = (31 * 5) + (30 * 3);
                
                if (month = 10)
                month = (31 * 6) + (30 * 3);
                 
                if (month = 11)
                month = (31 * 6) + (30 * 4);
                
                if (month = 12)
                month = (31 * 7) + (30 * 4);      
              }

           //Do calculations
            day = (year * 365) + (month) + (day);            
            
           //Show output
           cout << "Days since birth of Christ: " << day;
           cout << endl;
           cout << endl;
           cout << endl;
           cout << endl;
           cout << endl;
            
           }
           
         cout << endl;  
         return 0;
}

AOA :mrgreen:
/*
*please write it in main program ..
*/

# include <iostream.h>

main()
{


system("PAUSE");
}

this program display as under

Press Any key To Continue.....

when u press any key in last the will end...
OK ALLAH HAFIZ

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.