473,414 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

I need help with getting my lunar class to work in C++

So I'm working on this class and I have a major problem.

This class happens to be for a rocket, it's a game that you play and you try to see it you can land safely from 1000 meters up in the air.

The problem is when I input the throttle the program ignores it.

It doesn't matter what i input into the throttle i get the same result.

There are also other problems as well.

Some of my accessors don't change correctly.

What is wrong with my program?

Here is my code first i have the cpp.

I don't know if this helps but im running this off of a Win XP PC using Visual studio 2005.

Expand|Select|Wrap|Line Numbers
  1.  -------------------------------------------------------------------------------------------------------------- 
  2.  
  3. [
  4.  
  5. // Program6.cpp
  6. // Elliot Weimann
  7. // November 21, 2006
  8. // This program is a rocket game
  9. // Computer Science 210
  10.  
  11. #include <iostream>
  12. #include <string>
  13. #include "program6.h"
  14.  
  15. using namespace std;
  16.  
  17. void changetime();
  18.  
  19. void main()
  20. {
  21.     int throttle = 0;
  22.     int time = 0;
  23.  
  24.  
  25.     lunar c1;
  26.     c1.setFuelflow(0.0);
  27.  
  28. cout << "Welcome to my rocket game try to land the rocket safetly from 1000 meters" << endl;
  29. cout << "To start input the time interval" << endl;
  30. cin >> time;
  31.  
  32. while (c1.getAltitude() > 0 && c1.getAltitude() != 0)
  33.  
  34. {
  35.  
  36.  
  37.  
  38.     cout << "Enter a number 1 through 10 for the fuel flow rate " << endl;
  39.     cin >> throttle;
  40.     cout << "Your altitude is " << c1.getAltitude() << endl;
  41.     cout << "Your mass is " << c1.getMass() << endl;
  42.     cout << "Your fuel is " << c1.getFuel() << endl;
  43.     cout << "Your speed is " << c1.getVerticalspeed() << endl;
  44.  
  45.     if ( throttle > 10 || throttle < 0)
  46.     {
  47.         cerr << "You can't input that amount" << endl;
  48.         system("pause");
  49.         return;
  50.     }
  51.  
  52.  
  53.  
  54. c1.setFuelflow(throttle/10.0);
  55.  
  56.  
  57.  
  58.          for (int i = 0; i < time; ++i)
  59.          {
  60.             c1.setPasstime(.01);
  61.  
  62.          }
  63.  
  64.  
  65. }
  66.  
  67. {
  68. if (0 > c1.getVerticalspeed() && c1.getVerticalspeed() > -10 || c1.getVerticalspeed() == 0)
  69.  
  70.     cout << "safe landing" << endl;
  71.  
  72.  
  73. else
  74.  
  75.     cout << "unsafe landing" << endl;
  76.  
  77. system("pause");
  78. }
  79.  
  80. --------------------------------------------------------------------------------------------------
  81. Here is my implentation file
  82. --------------------------------------------------------------------------------------------------
  83. // Program6implentation.cpp
  84. // Elliot Weimann
  85. // November 21, 2006
  86. // This program is a rocket game
  87. // Computer Science 210
  88.  
  89. #include <iostream>
  90. #include "program6.h"
  91.  
  92. using namespace std;
  93.  
  94. lunar::lunar()
  95. {
  96.     thrust = 5000;
  97.     altitude = 1000;
  98.     mass = 900;
  99.     fuel = 1700;
  100.     fuelrate = 10;
  101.     fuelflow = 0;
  102.     verticalspeed = 0;
  103.     passtime = .01;
  104. }
  105.  
  106. float lunar::getFuel()
  107. {
  108.     fuel += passtime * fuelrate * fuelflow;
  109.     return fuel;
  110. }
  111.  
  112. float lunar::getFuelrate()
  113. {
  114.     return fuelrate;
  115. }
  116.  
  117. float lunar::getThrust()
  118. {
  119.     return thrust;
  120. }
  121.  
  122. void lunar::setFuelflow(float)
  123. {    if (fuel == 0 && fuelflow > 0)
  124. {
  125. fuelflow = 0;
  126. }
  127. }
  128.  
  129. float lunar::getFuelflow()
  130.     return fuelflow;
  131. }
  132.  
  133. float lunar::getMass()
  134. {
  135.     mass = mass + fuel;
  136.     return mass;
  137. }
  138.  
  139.  
  140.  
  141. void lunar::setPasstime(float)
  142. {passtime += .01;
  143.  
  144. }
  145.  
  146. float lunar::getPasstime()
  147. {    
  148.      return passtime;
  149. }
  150.  
  151. float lunar::getAltitude()
  152. {
  153.     altitude += passtime * verticalspeed;
  154.  
  155. if(altitude < 0)
  156. {
  157.     altitude = 0;
  158. }
  159.  
  160. if(altitude > 1000)
  161. {
  162.     altitude -= passtime * verticalspeed;
  163. }
  164. return altitude;
  165. }
  166.  
  167. void lunar::setVerticalspeed(float)
  168. {
  169.  
  170. }
  171.  
  172. float lunar::getVerticalspeed()
  173. {
  174.  
  175. verticalspeed += passtime * ((thrust * fuelflow)/mass) - 1.62;
  176.      return verticalspeed;
  177.  
  178. }
  179. ---------------------------------------------------------------------------------------------------
  180. here is my header file
  181. --------------------------------------------------------------------------------------------
  182. // Program6.h
  183. // Elliot Weimann
  184. // November 21, 2006 
  185. // This program is a rocket game 
  186. // Computer Science 210
  187.  
  188. #ifndef LUNAR_H
  189. #define LUNAR_H
  190.  
  191. class lunar 
  192. {
  193. public:
  194.  
  195.     lunar();
  196.     lunar(float, float);
  197.  
  198.     // Set time of lunar
  199.     void setPasstime(float);
  200.  
  201.  
  202.     // Set fuel of lunar
  203.     void setFuelflow(float);
  204.  
  205.     // Set speed of lunar
  206. void setVerticalspeed(float);
  207.  
  208.     // get thrust of lunar
  209.     float getThrust();
  210.  
  211.     // get time of lunar
  212.     float getPasstime();
  213.  
  214. // get altitude of lunar
  215.     float getAltitude();
  216.  
  217.     // get mass of lunar
  218.     float getMass();
  219.  
  220.     // get fuel of lunar
  221.     float getFuel();
  222.  
  223.     // get fuel of lunar
  224.     float getFuelrate();
  225.  
  226.     // get fuel of lunar
  227.     float getFuelflow();
  228.  
  229.     // get fuel of lunar
  230.     float getVerticalspeed();
  231.  
  232. float fuelflow;
  233.  
  234. private:
  235.     // Data members (attributes)
  236.     float fuel;
  237. float altitude;
  238.     float mass;
  239.     float thrust;
  240.     float fuelrate;
  241.     float verticalspeed;
  242.     float passtime;
  243. };
  244. #endif // LUNAR_H
  245.  
  246.  
  247. ]
  248.  
  249.  
  250. -------------------------------------------------------------------------------------------
  251.  
Dec 1 '06 #1
4 1855
DeMan
1,806 1GB
Have you tried outputting the throttle value that you pass in to make sure that the value is what you intend?
Dec 1 '06 #2
yes i just did that and it couts what i input, the thing is though when i output the fuelflow it says its always zero no matter what i input, and zero is what it starts out at.
Dec 5 '06 #3
DeMan
1,806 1GB
fuelflow is defined as a float, yet is used in various places as though it were an int. This in itself is not a problem in so far as the program is usually happy to cast it, but when things get unintentionally cast, interesting things can happen.. When casting a number from a float to an int, for example, you get a rounding effect identical to calling a 'floor' function. It always pays to be pedantic and use 0.0 rather than 0 for floats (although I just spotted something else)

Also your set fuelfuel flow method is a little odd
Expand|Select|Wrap|Line Numbers
  1. void lunar::setFuelflow(float)
  2. {    
  3. if (fuel == 0 && fuelflow > 0)
  4.  {
  5.   fuelflow = 0;
  6.  }
  7. }
  8.  
As far as I can tell, this method only set's the fuelflow if fuel==0 and fuelflow>0 (in which case it set's it to 0). Firstly I think the second test is redundant (in that you wouldn't really care whether fuel is flowing or not, if the tank is 0 then stop fuelflow). More importantly you should do something with your input. I think you nmay have meant something like:
Expand|Select|Wrap|Line Numbers
  1. void lunar::setFlow(float new_flow)
  2. {
  3.   if(fuel==0 && fuelflow>0) //I have left it your way assuming there is a reason
  4.   {
  5.     fuelflow=0;
  6.   }
  7.   else
  8.   {
  9.     fuelflow=new_flow;
  10.   }
  11. }
  12.  
Dec 5 '06 #4
thanks i did what you said and it seems to work now.
Dec 7 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Shlomi Schwartz | last post by:
Hi all, Why do I get the folowing date when creating it like so: var d = new Date(2003,9,2); Wed Oct 1 23:00:00 UTC+0200 2003 Isent it soposed to be:
9
by: Fabian | last post by:
Is there any way of determining when the first day of a *lunar* month (far eastern lunar months if it makes a difference) falls, using javascript? -- -- Fabian Visit my website often and...
3
by: haydn_llewellyn | last post by:
Hi, My company runs on a fiscal calendar that starts on the first monday in July, and is based on a 13 week quarter (4 weeks, 4 weeks, 5 weeks). What I need, is a way of relating Date() to the...
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
28
by: Tim Daneliuk | last post by:
I have a program wherein I want one behavior when a file is set as executable and a different behavior if it is not. Is there a simple way to determine whether a given named file is executable...
9
by: Tool69 | last post by:
Hi, Until now, I was running my own static site with Python, but I'm in need of dynamism. After reading some cgi tutorials, I saw Joe Gregorio's old article "Why so many Python web...
6
by: barun.saha04 | last post by:
Hi, I am facing a very basic problem with PSP. I have installed mod_python (in fedora Core 1), added the lines required for loading Python modules and handling PSP pages. I have created a...
1
by: =?Utf-8?B?bGF3ODc4Nw==?= | last post by:
i am looking for some MS Excel formula or MS Visal Basic Marco for converting Date to Lunar Date. tks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.