By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

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

P: 12
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
Share this Question
Share on Google+
4 Replies


DeMan
100+
P: 1,806
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

P: 12
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
100+
P: 1,806
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

P: 12
thanks i did what you said and it seems to work now.
Dec 7 '06 #5

Post your reply

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