Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (kikuo):

Please help hint what to do. I have no idea how to fix these mistakes. http://prntscr.com/bfp0d0 http://prntscr.com/bfp0fm

OpenStudy (kikuo):

@Sachintha

OpenStudy (sachintha):

Remove void keyword and try.

OpenStudy (kikuo):

http://prntscr.com/bfp93j Why did his work?

OpenStudy (kikuo):

http://prntscr.com/bfp98f This is what he said about passing reference parameters to modules

OpenStudy (sachintha):

You need to add the ampersand to the parameter name and not before the data type.

OpenStudy (kikuo):

Hm, I don't quite understand.

OpenStudy (kikuo):

Can you show me in my code?

OpenStudy (sachintha):

\(\sf \color{blue}{void}~calculatesBmi(double~\color{red}{\&}weight, double~\color{red}{\&}height);\)

OpenStudy (kikuo):

Got it! Alright, what about this error? http://prntscr.com/bfpbf4

OpenStudy (sachintha):

Remember to close it!

OpenStudy (kikuo):

Let me just give you the list of all the errors I have right now and we can work through them haha http://prntscr.com/bfpc7y http://prntscr.com/bfpcdt http://prntscr.com/bfpcfi

OpenStudy (sachintha):

First check every line whether you have closed it properly. Most of the errors are due to missing semi-colons.

OpenStudy (kikuo):

Fixed em. Down to five. : )

OpenStudy (sachintha):

Const should be const. Make it all simple.

OpenStudy (kikuo):

Down to four : )

OpenStudy (sachintha):

Add a screenshot of the errors and edited code.

OpenStudy (sachintha):

Yet you haven't closed everything properly. You have missed the variables and those inside the BMI function.

OpenStudy (kikuo):

I haven't? But I put semicolons in the places that weren't working, and they worked? Am I misunderstanding what you mean by close? @Sachintha

OpenStudy (sachintha):

But you haven't put semicolons after the variables height, weight inside main() and those inside the BMI function.

OpenStudy (kikuo):

Alright, down to one error. http://prntscr.com/bfpj11 http://prntscr.com/bfpj2f

OpenStudy (sachintha):

Try adding the BMI variable inside main()

OpenStudy (kikuo):

Hm, that didn't work. @Sachintha

OpenStudy (sachintha):

Show me how you added it.

OpenStudy (kikuo):

http://prntscr.com/bfpl0f

OpenStudy (sachintha):

Why are you adding the void keyword?

OpenStudy (kikuo):

double is a data type which is why I added it

OpenStudy (sachintha):

No I meant why did you add the void keyword in-front of the calculatesBmi function? Did you sir say so?

OpenStudy (kikuo):

Yes.

OpenStudy (sachintha):

Add this code in line 31 and try. Not sure whether it would return anything. BIM = calculatesBmi( weight, height);

OpenStudy (kikuo):

It worked! Can you explain why it did?

OpenStudy (kikuo):

Oh wait it says its an error now wth lol

OpenStudy (sachintha):

What is the error?

OpenStudy (kikuo):

http://prntscr.com/bfpodr

OpenStudy (sachintha):

Try removing void keyword from the function. Also remove line 35.

OpenStudy (kikuo):

http://prntscr.com/bfpu9x I'm not sure I can remove void because of my teacher

OpenStudy (sachintha):

Wait I will try running it on a online compiler.

OpenStudy (kikuo):

//Ashton Dreiling //6/9:00 PM/2016 //BMI exercise #include <iostream> #include <stdlib.h> using namespace std; //module prototype void calculatesBmi(double &weight, double &height); //Global constant const double KILOGRAM = 0.453592; int main() { //some variables double BMI; double weight; double height; cout << "Hello, today we are going to be calculating your BMI" << endl; cout << "Please type in your weight" << endl; cin >> weight; cout << "You typed in " << weight << endl; cout << "Please type in your height" << endl; cin >> height; cout << "You typed in " << height << endl; cout << "Please give us a second to calculate your BMI." << endl; BMI = calculatesBmi( weight, height); //pass variables to calculatesBmi module cout << "Your BMI is " << BMI << endl; system("Pause"); return 0; }//end main void calculatesBmi(double height, double weight) { //calculating BMI double BMI; BMI = weight / height * height; }//end calculatesBmi

OpenStudy (sachintha):

Do you have a note about the use of void given by your sir?

OpenStudy (kikuo):

-for now the headers for all the C++ functions that you will write begin with the key word void -following this you write the name of the function and a set of parentheses -function header never ends with a semicolon

OpenStudy (kikuo):

/* This is a test for loading the MaxDensity curves Ray Warren 22 May 2015 */ #include <iostream> #include <cstring> using namespace std; //module prototypes void noReferenceParameters(int anInt, double aDouble, string aString); void withReferenceParameters(int &anInt, double &aDouble, string &aString); int main() { //some variables int myInt = 1; double myDouble = 1.0; string myString = "Hello"; // output the variables cout << "The variable values in main(): " << endl; cout << " myInt: " << myInt << " myDouble: " << myDouble << " myString: " << myString << endl; // call the no reference parameter module noReferenceParameters(myInt, myDouble, myString); // output the variables cout << "The variable values in main(): " << endl; cout << " myInt: " << myInt << " myDouble: " << myDouble << " myString: " << myString << endl; cout << "They remain unchanged in main()" << endl; // call the no reference parameter module withReferenceParameters(myInt, myDouble, myString); // output the variables cout << "The variable values in main(): " << endl; cout << " myInt: " << myInt << " myDouble: " << myDouble << " myString: " << myString << endl; cout << "They are now changed in main()" << endl; } //end main() void noReferenceParameters(int anInt, double aDouble, string aString) { anInt = anInt * 2; // double it aDouble = aDouble * 2; // double it aString = aString + " World!"; // output the variables cout << endl; // output a blank line // "\t" is the tab character cout << "\tThe variable values in noReferenceParameters(): " << endl; cout << "\t myInt: " << anInt << " myDouble: " << aDouble << " myString: " << aString << endl; cout << endl; // output a blank line } //end noReferenceParameters() void withReferenceParameters(int &anInt, double &aDouble, string &aString) { anInt = anInt * 2; // double it aDouble = aDouble * 2; // double it aString = aString + " World!"; // output the variables cout << endl; // output a blank line // "\t" is the tab character cout << "\tThe variable values in withReferenceParameters(): " << endl; cout << "\t myInt: " << anInt << " myDouble: " << aDouble << " myString: " << aString << endl; cout << endl; // output a blank line } //end withReferenceParameters()

OpenStudy (sachintha):

Whew!! It worked. ^_^ #include <iostream> #include <stdlib.h> using namespace std; //module prototype void calculatesBmi(double &weight, double &height); //Global constant const double KILOGRAM = 0.453592; int main() { //some variables double weight; double height; double BMI; cout << "Hello, today we are going to be calculating your BMI" << endl; cout << "Please type in your weight" << endl; cin >> weight; cout << "You typed in " << weight << endl; cout << "Please type in your height" << endl; cin >> height; cout << "You typed in " << height << endl; cout << "Please give us a second to calculate your BMI." << endl; calculatesBmi(weight, height); //pass variables to calculatesBmi module cout << "Your BMI is " << BMI << endl; system("Pause"); return 0; }//end main void calculatesBmi(double &weight, double &height) { //calculating BMI double BMI; BMI = weight / (height * height); cout << BMI; }//end calculatesBmi

OpenStudy (kikuo):

What did you change?

OpenStudy (sachintha):

I added a line to output BMI inside the function and fixed some typos.

OpenStudy (sachintha):

This one works. But I had to pass another reference parameter. #include <iostream> #include <stdlib.h> using namespace std; //module prototype void calculatesBmi(double &BMI,double &weight, double &height); //Global constant const double KILOGRAM = 0.453592; int main() { //some variables double weight; double height; double BMI; cout << "Hello, today we are going to be calculating your BMI" << endl; cout << "Please type in your weight" << endl; cin >> weight; cout << "You typed in " << weight << endl; cout << "Please type in your height" << endl; cin >> height; cout << "You typed in " << height << endl; cout << "Please give us a second to calculate your BMI." << endl; calculatesBmi(BMI,weight,height); //pass variables to calculatesBmi module cout << "Your BMI is " << BMI << endl; system("Pause"); return 0; }//end main void calculatesBmi(double &BMI,double &weight, double &height) { //calculating BMI BMI = weight / (height * height); }//end calculatesBmi

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!