d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Assignment
12Next
Add Reply New Topic New Poll
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 19 2013 01:52pm
I have bits and pieces of code that I think are correct, but I don't know how to fill in the remaining pieces. Any help would be appreciated.

Here's the assignment:

Code
HW4. Input Conditioning for an Aircraft Simulator

Devise a program to takes user inputs for a flight simulator and checks their validity until prompted to exit.

Given Variables:
1) char menuChoice -- Input by user. Allowable options defined in the following table:
'A' -> increase velocity
'B' -> decrease velocity
'C' -> increase altitude
'D' -> decrease altitude
'E' -> increase velocity and altitude
'F' -> decrease velocity and altitude
'0' -> exit
2) double velocityChange -- Input by user. Allowable range is 0.1 to 100.0
3) double altitudeChange -- Input by user. Allowable range is 200.0 to 10000.0
4) double currentVelocity -- Internal value. Allowable range is 0.0 to 650.0 and initial condition is 0.0
5) double currentAltitude -- Internal value. Allowable range is -200.0 to 40000.0 and initial condition is 1000.0

Note: ensure your program provides protection against mishandling of user and program variables (remember scope and safety)

Write main and the functions for the following actions to take user input and continuously adjust velocity and altitude until the user exits or a fault condition occurs:
1) Handle user inputs for the following variables: char menuChoice, double velocity, double altitude. If no valid input choice, keep asking until one is provided.
2) Check validity of user velocity input and return new currentVelocity based on increase/decrease as appropriate.  Exit with a message if user input violates the currentVelocity range.
3) Check validity of user altitude input and return new currentAltitude based on increase/decrease as appropriate.  Exit with a message if user input violates the currentAltitude range.

If the user chooses to exit provide a message to that effect.  For all exit cases print the currentVelocity and current Altitude (ensuring the fault condition was not applied).





Here's what I have so far

Code
int main()
{
   cout << "To increase velocity, press A. \n";
   cout << "To decrease velocity, press B. \n";
   cout << "To increase altitude, press C. \n";
   cout << "To decrease altitude, press D. \n";
   cout << "To increase velocity and altitude, press E. \n";
   cout << "To decrease velocity and altitude, press F. \n";
   cout << "To exit, press 0. \n";


Code
while( velocityChange < 0.1 || velocityChange > 100){
      cin >> velocityChange;
}

while ( altitudeChange < 200.0 || altitudeChange > 10000.0){
     cin >> altitudeChange;
}
while ( currentVelocity < 0.0 || currentVelocity > 650.0){
     cin >> currentVelocity;
}
while ( currentAltitude < -200.0 || currentAltitude > 40000.0){
     cin >> currentAltitude;
}




I don't know how to define the variables and how to execute what the program is requiring
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Sep 19 2013 02:18pm
to define the variables just type

Code
double velocityChange;
double altitudeChange;
double currentVelocity;
double currentAltitude;

or
Code
double velocityChange, altitudeChange, currentVelocity, currentAltitude;

put that in the start of the program, right after your main line.

for the user choices:
Code
char menuChoice

cin >> menuChoice; // tell the user to input a letter

switch(menuChoice){
case 'A' :
//enter code here to increase velocity
// do this for the rest of the letters, continue with case 'B'
// for '0', do case '0': exit(0); (i think you need to put an exit message.)

default:
cout << "Error, please enter the correct key" << endl;
}

// do the math from the other variables here


sorry the tabs don't work on firefox for some reason in the code brackets :/
if you need more help just let me know.

e: for 1), you'll need to put a 'while' for the user input until the user puts the correct key, I posted something similar (or the same) for that in your other topic.

This post was edited by ShadowFiend on Sep 19 2013 02:22pm
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 19 2013 02:29pm
Thanks, im gonna work on it and ill post here again with a more developed code
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 19 2013 03:10pm
for the switch statement, would the cases look like this?

Code
case A:
                      velocityChange = velocity + currentVelocity;
                      break;
                 Case B:
                      velocityChange = velcoity - currentVelocity;
                      break;
                 Case C:
                      altitudeChange = altitude + currentAltitude;
                      break;
                 Case D:
                      altitudeChange = altitude - currentAltitude;
                      break;


theres no 'velocity' declared in the main, so I don't understand what it means to change the velocity or altitude
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Sep 19 2013 04:59pm
read 1)
you have to declare those variables also
I'm assuming the user has to input the value for velocity or altitude in the cases and do some verifications so currentVelocity/currentAltitude is between the values mentioned, if not, ask the user for another value.
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 19 2013 07:28pm
this is what I have so far, does it look right or is there a blaring mistake? Still working on it but if im doing something completely wrong any tips would be good

Code
#include <iostream>
using namespace std;

float currentVelocity;
float velocityChange;
float velocity = currentVelocity + velocityChange;

int main()
{
   double velocityChange;
   double altitudeChange;
   double currentVelocity = 0.0;
   double currentAltitude = 1000.0;
   
   
char menuChoice;
  cout << "To increase velocity, press A. \n";
  cout << "To decrease velocity, press B. \n";
  cout << "To increase altitude, press C. \n";
  cout << "To decrease altitude, press D. \n";
  cout << "To increase velocity and altitude, press E. \n";
  cout << "To decrease velocity and altitude, press F. \n";
  cout << "To exit, press 0. \n";
  cin >> menuChoice;
  cout << '\n';
         
switch(menuChoice){

                 case 'A':
                      cout << "Enter a velocity: ";
                      cin >> velocity;
                      velocityChange = currentVelocity + 1;
                      cout << endl;
                      break;
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Sep 19 2013 09:24pm
The assignment is really badly written. It looks like the variables velocity and altitude is completely useless and I'm not even sure if you have to display the changes made to the variables after you add/subtract values :/
And are you allowed to do functions? did he say that you can write the program in any form you want?

Anyways, i added some comments, make sure to change/add the things i said.
And sorry for the shitty format once again, hopefully the code function on this site will be fixed soon.
Code
#include <iostream>
using namespace std;

int main()
{
  double velocityChange = 0;
  double altitudeChange = 0;
  double currentVelocity = 0.0;
  double currentAltitude = 1000.0;
 
char menuChoice;

while(1){
 cout << "To increase velocity, press A. \n";
 cout << "To decrease velocity, press B. \n";
 cout << "To increase altitude, press C. \n";
 cout << "To decrease altitude, press D. \n";
 cout << "To increase velocity and altitude, press E. \n";
 cout << "To decrease velocity and altitude, press F. \n";
 cout << "To exit, press 0. \n";
 
  cin >> menuChoice;
 cout << '\n';
     
 
switch(menuChoice){

                case 'A':
                     cout << "Increase velocity by: ";
      while(velocityChange <= 0.1 || velocityChange >= 100.0){
      cin >> velocityChange;
      }
      if( currentVelocity + velocityChange > 650.0) {// check before doing the affectation so it doesn't go over
      cout << "Error, velocity too high" << endl; exit(0);}

                     currentVelocity += velocityChange; // this is the same thing as currentVelocity = currentVelocity + velocityChange
                     cout << endl;
      velocityChange = 0; // place it back to 0 so the user can input another value
      cout <<  currentVelocity << " - current velocity" << endl; // debugging, remove this after
                     break;

   case 'B':
                     cout << "Decreases velocity by: ";
      while(velocityChange <= 0.1 || velocityChange >= 100.0){
      cin >> velocityChange;
      }
      if( currentVelocity - velocityChange < 0.0){
      cout << "Error, velocity too low" << endl; exit(0);}

                     currentVelocity -= velocityChange; // this is the same thing as currentVelocity = currentVelocity - velocityChange
                     cout << endl;
      velocityChange = 0;
      cout <<  currentVelocity << " - current velocity" << endl; // debugging, remove this after
                     break;

      // do this for the others, make sure to put the correct ranges

      // for all places where you put exit(0); do a cout for currentVelocity and currentAltitude
 }
}
}
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 19 2013 09:42pm
This is supposed to be an intro class with no previous experience required, and the teacher assigns these homeworks and they are impossible for a majority of the class to do. He rarely shows up to class and gives these assignments. Without jsp I would be really screwed. Basically he gives us the prompt and we're on our own
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 19 2013 10:22pm
Code
case 'A':
                      cout << "Increase velocity by: ";
                      while(velocityChange <= 0.1 || velocityChange >= 100.0){
                      cin >> velocityChange;
                      }
                      if(currentVelocity + velocityChange > 650.0)
                      {
                      cout << "Error, velocity too high" << endl; exit(0);
                      }
                           currentVelocity += velocityChange;
                           cout << endl;
                      velocityChange = 0;
                      cout << currentVelocity << " - current velocity" << endl;
                      break;


For this part of the code, when I input a number > 650 (ie 700) it doesnt read the error or exit the program

This post was edited by King3r on Sep 19 2013 10:36pm
Member
Posts: 17,302
Joined: Nov 25 2006
Gold: 23,806.60
Trader: Trusted
Sep 19 2013 11:59pm
Ah I figured it out. Everything is running properly now :)
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll