-
calling a function??
Hi all,
I'm a rookie when it comes to C but I'm learning. I was wondering if someone can give me advice on my code? I'm trying to write a function that receives from the calling program the length and width of a room in decimal feet. The function should then caculate and return the number of square yards in the room.
I would like my main program to prompt for the dimensions of the room in decimal feet and the cost of a yard of carpet. Then, I would like to display both the number of yards needed and the total cost of the carpet.
I think I have my code in my function mixed up with the code in my main program. I didn't totally finish because I'm at this roadblock. Please, this is probably basic to some, but I'm just starting out. Thanks in advance for any advice. Here is my code:
Code:
#include <iostream.h>
#include <stdlib.h>
double room(double length, width, yds)
{
yds=9/(length*width);
}
return yds;
int main()
{
double length, width, cost, total;
cout<<"Enter the width of the room:";
cin>>width;
cout<<"Enter the length of the room:";
cin>>length;
cout<<"Enter the cost of a yard of carpet:";
cin>>cost;
total=room*cost;
-
Code:
double room(double length, double width, double yds)
{
return yds=9/(length*width);
}
Code:
total = room(length, width, yds) * cost;
-
I think you need to read up on how to use functions and how to properly syntax them.
I have given you a code for a simple function which calculates the square of a given number.
You should be able to read through and adapt it to meet the needs of your specific program.
Code:
#include <iostream>
//function declaration
//always before int main()
//semi colon needed.
//If more than one variable is used
//separate them with a comma
int square(float);
using namespace std;
int main()
{
float number;
cout<<"Enter a number:";
cin>>number;
// call the function 'square' passing in the variable 'number'
// into it. Here you don't need to define its type
//semicolon needed
square(number);
cout<<"Press any key to exit"<<endl;
char pause;
cin>>pause;
}
//function definition
//here you have to declare what type the variable is
//Here the semi colon is ommitted.
int square(float number)
{
cout<<"The number squared was"<<number*number<<endl;
}