Having a hard time with functions

Jul 12, 2009 at 2:43am
OK I'm a beginner and I have no idea what the parameters are for and I have no idea how to call the functions in order to print them for the user.

The purpose of the program is to display inches and centimeters into meters and centimeters, I'm required to use at least 3 functions, more if needed.

I used int variable for calc_Meters function because I need to display meters and centimeters seperately, so i planned using int to take only the first number of the function. But anyways this code is going awry, please help!

---------------------------------------------------

#include<iostream>
using namespace std;

int calc_Meters(float);
float calc_Cent(float, int);

float input(int outputMeters)

{ //Declare variables

float inputFeet;
float inches;
float totalFeet;
float feet;

cout<<"Please enter feet: ";
cin>>inputFeet;
cout<<"Please enter inches: ";
cin>>inches;

feet=inputFeet + inches/12;
totalFeet=feet;

//Call functions?
calc_Meters(totalFeet);
calc_Cent(totalFeet,outputMeters);

int r=calc_Meters;
float x=calc_Cent;

//output converted units to user
cout<<endl
<<inputFeet<<" Feet and "<<inches<< "inches is equivalent to: "<<r<< " meter(s) and "<<x<<" centimeters"<<endl
<<endl;

}

int calc_Meters(float f)
{

int outputMeters;

outputMeters=f*.3048;

return outputMeters;
}

float calc_Cent(float totalFeet, int outputMeters)
{

float meters;
float outputCent;

meters=totalFeet*.3048;
outputCent=(meters-outputMeters)*100;
return outputCent;
}
Jul 12, 2009 at 2:47am
Jul 12, 2009 at 3:01am
Thanks for the link, but what am I doing wrong with my code?

It keeps saying can't convert from float to int to float...
and I doubt some of the rest is right either.
Jul 12, 2009 at 4:11am
I personally like to think of functions like little factories for information, parameters go in, the function does work, and the final product (return values) comes out. I assume that your second sentence should have said that the purpose of the program is to convert feet and inches into meters and centimeters? Here are the problems that I have found:

1) these two lines:
1
2
int r=calc_Meters;
float x=calc_Cent;


are function calls and thus need to have their parameters in there just like the two lines above it
1
2
int r = calc_Meters(totalFeet);
float x = calc_Cent(totalFeet,outputMeters);


i assume that those two errors were what was really making you frustrated.

2) the function "input" needs to have a return value of the type "float". that is the whole point of defining the function as float input(int). if you have a function that doesnt have the return type of "void" then you need to have the function return a value.

3) this isnt really a major thing but i would recomend getting rid of your variable called "feet".
you only use it once (that i noticed).

instead of :
1
2
feet=inputFeet + inches/12;
totalFeet=feet;

change to:
totalFeet = inputFeet + inches / 12

4) why is the parameter in "input" called outputMeter? where you trying to make the "outputMeter" parameter list your output? you cannot output information from a fuction through the parameter list (except pass by reference, but i'll leave that for another day becuase that would take a lot of time and i dont like getting scooped when i answer questions on the internet (EDIT: Damit)). the correct way would be something like this:

return outputMeter;

I think you did the same thing in calc_Cent.

I would change the program to do something like this:

From main() call a function (maybe name it "input") that gets user input of feet and inches. next it converts the variable that stores feet into inches and adds this value to the variable that stores inches. then it converts the inches to cm (1in = 2.54cm) and returns the number of cm that the user inputed to the main function.

the main function stores the value returned from the input function in a variable (named "cm" maybe).
the cm variable is passed into a function (maybe named toMeters) that calculates, and returns to main, the number of meters in the cm variable.

lastly (now back in main), cm is passed into another function (maybe named toCent) that calculates, and returns to main, the remaining centimeters (maybe use the % operator?)


Also your calculations are a little off becuase it converts an input of 5ft 0in to 1m -347.6cm.

those were my thoughts so far.

PS where is your main() function?
Last edited on Jul 12, 2009 at 4:27am
Jul 12, 2009 at 5:52am
Thanks for the reply.

My main function i didn't know if i needed it or what it's purpose was. But i took your advice and simplified my code.

but now when i click run, the print screen appears, and then quickly closes on me completely whenever i try to run it.

do you have any idea what's wrong, any help would be appreciated.

----------------------------------------------------------

#include<iostream>
using namespace std;

void input();
int calcMeters(float,float);
void output(float, float, int);

void main(float inputFeet, float inches)
{
int z;

z=calcMeters(inputFeet,inches);
output(inputFeet,z);

}

void input()

{ //Declare variables

float inputFeet;
float inches;

cout<<"Please enter feet: ";
cin>>inputFeet;
cout<<"Please enter inches: ";
cin>>inches;

}




int calcMeters(float inputFeet, float inches)
{
float totalFeet;

totalFeet=inputFeet + inches/12;

int outputMeters;
outputMeters=totalFeet*.3048;

return outputMeters;

}


void output(float inputFeet, int z)
{

cout<<endl
<<inputFeet<<" Feet is equal to "<<z<< " meter(s) <<endl
<<endl;
}
Jul 12, 2009 at 11:30am
Do you mean the console flashes up then disappears so you can't see the results?
try: http://www.cplusplus.com/forum/beginner/1988/

p.s.
Two questions from me:
 
void main(float inputFeet, float inches)

1. Is void main() a standard, or int main()?
2. Should we reserve the place for arguments only for command-line options?
Jul 12, 2009 at 2:52pm
To wmheric,

I'm a beginner so I'm unfamiliar with the terminology of some of the words in C++. So bear with me lol.

My understanding is that the difference between having void main and int main, wih int main, i know i must return a value, so since i didn't need a value, I used void main, but again I'm not sure if that's correct to do so.

As for question two, I'm a little shady about what arguments are for command line options, so I'm completely lost on that as well.
Last edited on Jul 12, 2009 at 4:41pm
Jul 12, 2009 at 3:00pm
OK I'm a beginner


guess u should post at the beginner´s section...:P....
Jul 12, 2009 at 6:08pm
My understanding is that the difference between having void main and int main, wih int main, i know i must return a value, so since i didn't need a value, I used void main, but again I'm not sure if that's correct to do so.


You are actually correct except main is a special case and must always return an int. For all other functions, though, if you don't want to return anything you make them void.

As for question two, I'm a little shady about what arguments are for command line options


I wouldn't worry about it. You shouldn't deal with the command line args until you're more familiar with C++.

Just know that main must look like this: int main() or this: int main(int argc,char* argv[]). Since you won't be using the command line, stick with the simpler, first version for now.


As for why you need main (don't know if anyone answered this or not): it's the entry point for your program. The computer will start running your program by starting with main. All other functions get called from there. Without main, the computer wouldn't know where your program is supposed to start.
Jul 12, 2009 at 6:43pm
As for why you need main (don't know if anyone answered this or not): it's the entry point for your program. The computer will start running your program by starting with main. All other functions get called from there. Without main, the computer wouldn't know where your program is supposed to start.


Also for the return value you need to return 0 (zero) if the program runs without errors and return 1 if there are errors.

to get the screen to stay up put:
cin.get();
where you want it to pause.
Jul 12, 2009 at 7:50pm
Keep the console open long enough to see your program's output
http://www.cplusplus.com/forum/articles/7312/
Jul 12, 2009 at 10:15pm
I think you also need to review the scope of your variables. i.e. if you declare a variable where can you use it? ex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void examp(int);

int main()
{
       int testInt = 5;
       float testFloat = 20;

       examp(testInt);// call to void examp()
       
       // will still output 5 despite a variable of the same name being changed in examp()
       cout<<testInt<<endl;

       cout<<testDouble<<endl;// compiler will complain that testDouble hasnt been initialized
       cin.get();
       return 0;
}
void examp(int testInt)
{
       testInt = 10;
       double testDouble = 50;

       cout<<testFloat<<endl;// compiler will complain that testFloat hasnt been initialized
       cout<<testInt<<endl;
}


this code should give you a couple of errors a) not initalizing testFloat when in void examp() and b) not initalizing testDouble when in int main(). This is because these two variables only have scope in functions that they are initialized. when you pass a variable into a function you are really creating a COPY of that variable (that gets initalized when you call the function) unless you pass-by-reference(again I dont want to go into that right now). c) other errors because I didn't check the code in a complier

when the compiler gets to line 25 it will throw up and error because it has no idea what data type the examp(int) copy of testFloat is. a variable of the same name was initalized in main(), but that variable doesnt have the scope to be referenced in a different function. Same thing with the two testDouble variables. The point of functions is to put code in compartements so to make it simpler. i.e. when im in one function i only have to worry about what is going on in THAT function.
Last edited on Jul 13, 2009 at 12:07am
Jul 13, 2009 at 1:29am
Thank you Geinbits and Duoas, and the rest of you who replied to my post.

I finally figured it out, Thanks to everyone's help, all of your little bits of information helped me finally understand it. I'm very grateful!

Topic archived. No new replies allowed.