-1

In class were learning about classes and structures, Im working on a program that is supposed to have to member functions in the class, one doing calculations and one to return certain values, it seems Im running into trouble calling the functions maybe? IM not a 100% sure because c++ cant specify the error. I hope you can help me, thank you in advance.

#include <iostream>
#include <cstdlib>
using namespace std; 
void rocket(double massR,double massE, double massP,double avgTh,double bd);
double rocketreturn(double& MV,double& MA);

class Rocket
{
private:
 double massR;
 double massE;
 double massP;
 double avgTh; // average thrust
 double bd;    //burn duration

public:

void rocket(double massR,double massE, double massP,double avgTh,double bd) 
{
 massR=massR/1000;
 massE=massE/1000;
 massP=massP/1000;
 double LM=massR+massE;
 double MCP=massR+massE-massP;
 double AM=(LM+MCP)/2;
 double acc=(avgTh/AM)-9.8;
 double MV= acc * bd;
 double Alt=.5*acc*.5*.5;
 double t=MV/9.8; 
 double MA=Alt+MV*t+.5*-9.8*t*t;
  }

 double rocketreturn(double& MV, double& MA)
 {
 cout<< MV;
 cout<< MA;
 }
};



int main( )
{
double massR; // mass of the rocket
double massE; // mass of the engine
double massP; // mass of the propellant
double avgTh; // average thrust
double bd; // burn duration
double MV; // max velocity
double MA; // max altitude
char ans;
system("CLS");
cout << "Take Home # by - "
     << "Rocket Object\n\n";

do
{
cout <<"Enter the mass of the rocket: "<<endl;
cin >> massR;   
cout <<"Enter the mass of the engine: "<<endl;
cin >> massE;
cout <<"Enter the mass of the propellant: "<<endl;
cin >> massP;
cout <<"Enter the average thrust of the engine: "<<endl;
cin >> avgTh;
cout <<"Enter the burn duration of the engine: "<<endl;
cin >> bd;

rocketreturn(MV,MA);

cout <<"the rockets maximum velocity is " << MV<<endl;
cout <<"the rockets maximum altitude is "<<MA<<endl;

cout <<"Would you like to run the program again (Y or N)?"<<endl;
cin>>ans;
}
while(ans=='y'||ans=='Y');

}

  • 1
    Please [edit] your question to explain what you mean by "C++ can't specify the error". Thanks for improving the question's reference value and making it more answerable! – Nathan Tuggy Apr 12 '15 at 02:39
  • Paste the compiler error output. – Kam Apr 12 '15 at 02:39
  • I don't think you need function declarations at the top, plus I don't see see any object creation of type `Rocket` – shiraz Apr 12 '15 at 02:42
  • The last 8 lines of your rocket function don't really do anything because the values you are creating go away at the end of the function (you are not returning them or effecting globals). You may want to ask yourself what you are expecting from that processing. – Dweeberly Apr 12 '15 at 02:45
  • @Dweeberly In fact, the first three lines don't do anything, either. They manipulate function parameters, not member variables of the same name. None of these changes are observable outside the function. – Igor Tandetnik Apr 12 '15 at 02:57
  • 1
    You have declared two global, non-member functions `::rocket` and `::rocketreturn`. You are calling the latter. But you have not actually implemented either of them. Independently, you have class `Rocket`, with two member functions `Rocket::rocket` and `Rocket::rocketreturn` (these two functions are completely unrelated to and independent from global functions that happen to have the same names). You never create an instance of `Rocket`, and of course never call any of its member functions. – Igor Tandetnik Apr 12 '15 at 03:01
  • @Igor, very true, I wasn't paying attention to the names, I just looked to see if they were passed by ref or value. So the entire function doesn't really do anything ... prob'ly not what they want :-) – Dweeberly Apr 12 '15 at 03:03
  • from what IM understanding here, is that everything is done in the function but nothing is actually being passed through? isnt double& supposed to tell the program to do it? @Kam its not giving me anything when I output, it gives me some jargon, when I try. – Notkrokidal Apr 12 '15 at 03:04
  • You don't have your function prototyped using double & ... of course I don't really see where rocket is being called either. – Dweeberly Apr 12 '15 at 03:14
  • @Dweeberly Im calling the function in main, but Im only calling rocketreturn to main, my teacher wants me to have one function to do the calcuations and one to return the values. so Im a little lost, to be quite honest. – Notkrokidal Apr 12 '15 at 04:10

1 Answers1

0

You can try using the following program structure and fill in the details the way you wish. Note, it won't compile.

#include <iostream>

using namespace std;

class Rocket {
  private:
    double mR, mE;
  public:
    /* Constructor. It is missing in your program. You probably want it */
    Rocket(double massR, double massE) {
      /* Better to use initialization list, but this should work too */
      mR = massR;
      mE = massE;
    }

    double rocketreturn(double & a, double & b) {
      /* Do sth with the parameters. But return a double, 
         which you are not doing in your program */
       double result = a + b;
       return result;
    }

};

int main() {
    double x, y;
    do {
        cin >> x >> y;
        Rocket my_rocket(x, y);
        double z = my_rocket.rocketreturn(x, y);  // Assignment not necessary

    } while ( /* some condition */) //my_rocket object will be destroyed after scope goes out
    return 0;
}
shiraz
  • 1,208
  • 2
  • 12
  • 26
  • thank you for your help, can you explain why I would need a constructor thou? the name of the class and the function are now changed, so Im not 100% sure I would need it. – Notkrokidal Apr 12 '15 at 03:05
  • @Notkrokidal The above program is just a hint. As others have pointed out, you probably need to think what you expect from your program. I would suggest to pick a good book on c++ and read about constructors and classes in general. Here is a great list of books: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – shiraz Apr 12 '15 at 03:14