On C++ coding.

MaverickSawyer

Acolyte of the Probe
Joined
Apr 11, 2011
Messages
3,919
Reaction score
5
Points
61
Location
Wichita
Lately, I have been learning how to code in C++. It's actually quite similar to the forma used by my TI-89 graphing calculator, so I have a fairly good understanding, which I was quite suprised by. Anyway, I have a program on the TI that can determine the elements of a circular orbit around a variety of bodies in our system from just one input, which can be altitude, period, or velocity. I am working on converting it to C++, but it's slow.
Anyways, this is what I have so far, without any test runs for stability:
Code:
#include <iostream>

using namespace std;

int main()
{
  int type;
  cout << "Select Known Value: ";
  cout << "1: Altitude";
  cout << "2: Period";
  cout << "3: Velocity";
  cout << "Choice: ";
  cin >> type;
  cout << "You chose: " << type << "." << endl;

  if (type==1);
  {
    int refob;
    int alt;
    int rad;
    int orad;
    int mass;
    int time;
    cout << "Select Reference Object: ";
    cin >> refob;
    cout << "Chosen Reference: " << refob << "." << endl;
    cout << "Enter Altitude in meters: ";
    cin >> alt;
    cout << "Your altitude is " << alt << " meters." << endl;
  }
  else if (type==2);
  {
    int refob;
    int time;
    int orad;
    int rad;
    int alt;
    int vel;
    cout << "Select Reference Object: ";
    cin >> refob;
    cout << "Chosen Reference: " << refob << "." << endl;
    cout << "Enter Orbital period in seconds: ";
    cin >> time;
    cout << "Your orbital period is " << time << " seconds." << endl;
  }
  else if (type==3);
  {
    int refob;
    int vel;
    int orad;
    int rad;
    int alt;
    int time;
    cout << "Select Reference Object: ";
    cin >> refob;
    cout << "Chosen Reference: " << refob << "." << endl;
    cout << "Enter Orbital velocity in meters per second :";
    cin >> vel;
    cout << "Your orbital velocity is " << vel << " meters per second." << endl;
  }
  else
  {
    cout << "ERROR: Invalid Entry!" << endl;
  }
  return 0;
}

Am I on a good track, or am I going the wrong way?
Comments and criticisms are invited.
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,617
Reaction score
2,337
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
REMOVE THE SEMICOLON AFTER THE "if (...)".

your code won't compile like that, it will fail because the else hangs in the empty air.

Code:
if (x) ;

is the same as if you write

Code:
if(x) {}

The statements that should be executed if x is true is empty.
Try it with a switch...case:...default: statement. This makes the if...else if...else stuff much better to read and will tell the compiler to make more effective code.
 

Blake

Addon Developer
Addon Developer
Joined
Mar 9, 2009
Messages
233
Reaction score
121
Points
58
Location
Lehi, Utah
Presumably you are assigning values to refob, alt, rad, etc that will be used later. Those variables should be moved to the top of the method. They will fall out of scope when the if{} block is exited and not be available.

Replace the else if construct with a switch (as has been said).

Code:
switch (type)
{
   case 1:
   ... do stuff
   break;
...

Better yet, use consts so your code reads like this:

Code:
const int ALTITUDE = 1;
...
switch (type)
{
   case ALTITUDE:
   ... enter altitude etc.
   break;
...

I would also move the data entry to another method that is called from main so that the intent of your 'main' method is easier to read. That is a judgement call and depends on what else 'main' is going to do of course.
 

Hurricane

Grinfeld Aerospace guy
Joined
Sep 16, 2011
Messages
211
Reaction score
0
Points
0
Code:
int refob;
int vel;
int orad;
int rad;
int alt;
int time;

I think (and that's just ME, I'm also a beginner) you could put all those into a forward declaration, and get rid of repeating them every time. :tiphat:
Again, I'm only a beginner, so I'm not sure about it.
 

RisingFury

OBSP developer
Addon Developer
Joined
Aug 15, 2008
Messages
6,427
Reaction score
492
Points
173
Location
Among bits and Bytes...
I'm not gonna offer any technical advice, but I do have a suggestion for upgrading this - and giving you a new C++ task.

You have an if statement with 1, 2 and 3 for which value to pick, but if you remember, pretty much every quantity has a unit. For time it's seconds (s), for altitude it's meters (m) and for velocity it's meters per second (m/s). You could have the data entry done in a way that you just enter a number with the unit, example:
6000 s

The program would look for the unit. If it finds the unit s, it would work on the formula for time.
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,617
Reaction score
2,337
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
You could also just enter values (variable = value unit)and then start to calculate when enough data is given to calculate one other quantity.

Also as tiny help for describing units in a simple way: OpenFOAM for example uses a vector of seven integer values to describe the units. Each component of the vector is one basic unit (eg, kg, m, s) and the number gives the exponent of it... for example you could use (kg m s) as units and then describe internally meters per second squared as (0 1 -2) or Newtons as (1 1 -2)
 

MaverickSawyer

Acolyte of the Probe
Joined
Apr 11, 2011
Messages
3,919
Reaction score
5
Points
61
Location
Wichita
Thanks for the tips, guys. I wasn't planning on incorporating units into the equation, since that's beyond my understanding of C++ at this time. this is just the start of the project, and I haven't had time to convert the entire TI program to something that C++ can read. I still am trying to make heads or tails of the header files, but considering I was half asleep when I looked at that section last, that's hardly suprising. :lol: I'll take the tips and incorporate them into the project.
 
Top