Im new to C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 3
Reputation: Elthran is an unknown quantity at this point 
Solved Threads: 0
Elthran Elthran is offline Offline
Newbie Poster

Im new to C++

 
0
  #1
Sep 23rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: Im new to C++

 
0
  #2
Sep 23rd, 2005
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.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Im new to C++

 
0
  #3
Sep 23rd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Im new to C++

 
0
  #4
Sep 24th, 2005
>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<>.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 3
Reputation: Elthran is an unknown quantity at this point 
Solved Threads: 0
Elthran Elthran is offline Offline
Newbie Poster

Re: Im new to C++

 
0
  #5
Sep 24th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Im new to C++

 
0
  #6
Sep 24th, 2005
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Im new to C++

 
0
  #7
Sep 24th, 2005
Don't forget to multiply your centimeters by 0.033 to get the height in feets!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 3
Reputation: Elthran is an unknown quantity at this point 
Solved Threads: 0
Elthran Elthran is offline Offline
Newbie Poster

Re: Im new to C++

 
0
  #8
Sep 29th, 2005
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 6
Reputation: ITgeneration is an unknown quantity at this point 
Solved Threads: 0
ITgeneration ITgeneration is offline Offline
Newbie Poster

Re: Im new to C++

 
0
  #9
Sep 29th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC