943,565 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1666
  • C++ RSS
Sep 23rd, 2005
0

Im new to C++

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Elthran is offline Offline
3 posts
since Sep 2005
Sep 23rd, 2005
0

Re: Im new to C++

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.
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 23rd, 2005
0

Re: Im new to C++

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.
Reputation Points: 14
Solved Threads: 0
Junior Poster
Niklas is offline Offline
104 posts
since Sep 2005
Sep 24th, 2005
0

Re: Im new to C++

>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<>.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 24th, 2005
0

Re: Im new to C++

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Elthran is offline Offline
3 posts
since Sep 2005
Sep 24th, 2005
0

Re: Im new to C++

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char ch = 'j';
  8.  
  9. while(ch == 'j' || ch == 'J')
  10. {
  11. cout << "Enter height in centimeters: ";
  12. int centimeters;
  13. cin >> centimeters;
  14. cout << "Your height in feet " << centimeters << endl;
  15.  
  16. cout<<"Wish to enter a new number? Press j or J to continue!\n"
  17. "Press any other key to quit!\n";
  18.  
  19. cin>> ch;
  20. }
  21.  
  22. cout<<endl;
  23.  
  24. return 0;
  25. }

Hope this helps
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Sep 24th, 2005
0

Re: Im new to C++

Don't forget to multiply your centimeters by 0.033 to get the height in feets!
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Sep 29th, 2005
0

Re: Im new to C++

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 thx

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char ch ='j';
  7.  
  8. while(ch == 'j' || ch == 'J')
  9.  
  10. {
  11. //Declare variables
  12. int day;
  13. int year;
  14. int month;
  15. int leapyear;
  16.  
  17. //Get input
  18. cout << "Enter year (yyyy): ";
  19. cin >> year;
  20. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
  21. leapyear = 1;
  22. cout << "Enter month (mm): ";
  23. cin >> month;
  24. cout << "Enter day (dd): ";
  25. cin >> day;
  26. {
  27. if (leapyear = 1) if (month > 1)
  28. day = day + 29;
  29. if (leapyear = 0) if (month > 1)
  30. day = day + 28;
  31. }
  32.  
  33. {
  34. if (month = 1)
  35. month = (31 * 1);
  36.  
  37. if (month = 2)
  38. month = (31 * 1);
  39.  
  40. if (month = 3)
  41. month = (31 * 2);
  42.  
  43. if (month = 4)
  44. month = (31 * 2) + (30 * 1);
  45.  
  46. if (month = 5)
  47. month = (31 * 3) + (30 * 1);
  48.  
  49. if (month = 6)
  50. month = (31 * 3) + (30 * 2);
  51.  
  52. if (month = 7)
  53. month = (31 * 4) + (30 * 2);
  54.  
  55. if (month = 8)
  56. month = (31 * 5) + (30 * 2);
  57.  
  58. if (month = 9)
  59. month = (31 * 5) + (30 * 3);
  60.  
  61. if (month = 10)
  62. month = (31 * 6) + (30 * 3);
  63.  
  64. if (month = 11)
  65. month = (31 * 6) + (30 * 4);
  66.  
  67. if (month = 12)
  68. month = (31 * 7) + (30 * 4);
  69. }
  70.  
  71. //Do calculations
  72. day = (year * 365) + (month) + (day);
  73.  
  74. //Show output
  75. cout << "Days since birth of Christ: " << day;
  76. cout << endl;
  77. cout << endl;
  78. cout << endl;
  79. cout << endl;
  80. cout << endl;
  81.  
  82. }
  83.  
  84. cout << endl;
  85. return 0;
  86. }
Last edited by Dave Sinkula; Sep 29th, 2005 at 10:43 am. Reason: Added [code][/code] tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Elthran is offline Offline
3 posts
since Sep 2005
Sep 29th, 2005
0

Re: Im new to C++

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ITgeneration is offline Offline
6 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Plz, helping with writing a bit of code. Thanks
Next Thread in C++ Forum Timeline: C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC