Adjusting to a new level

Pages: 123
You're going to want to reconsider majors, I'm afraid.

"do your magic here" simply means "write the code that belongs in the function".
The function is named area, and it takes a Carpet as parameter. Sounds like it
should return the area of the carpet.
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
28
29
30
#include <iostream>

using namespace std;

struct Carpet		//struct
{
    float lengthInFeet, widthInFeet;
};

int main()			//
{
	Carpet myCarpet; 
	myCarpet.lengthInFeet=7.8;
	myCarpet.widthInFeet=4.2;

	cout<<"Length:";
	cin>>myCarpet.lengthInFeet;
	cout<<"Width:";
	cin>>myCarpet.widthInFeet;
	
	return 0;

float displayArea()
{
	float area;
	area = lengthInFeet * widthInFeet; 
	return area;
}
	cout << "The square feet of carpet is: " <<displayArea <<endl;
}


i can't run it due to this error:
:\Documents and Settings\Administrator\Desktop\proj2\carp2.cpp(13) : warning C4305: '=' : truncation from 'const double' to 'float'
C:\Documents and Settings\Administrator\Desktop\proj2\carp2.cpp(14) : warning C4305: '=' : truncation from 'const double' to 'float'
C:\Documents and Settings\Administrator\Desktop\proj2\carp2.cpp(24) : error C2601: 'displayArea' : local function definitions are illegal
Error executing cl.exe.

closed account (jwC5fSEw)
wish someone gave the exact answer coz i'd tried everything


This is something you can be sure will never happen here. This forums is for help, not giving out the exact answer.

Okay, the main problem with that is that you are declaring displayArea() in main(), which is not allowed. Put it after your Carpet struct.

Second, displayArea needs to take a Carpet and use that Carpet's elements. Right now, it takes nothing.
Last edited on
like this?

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
28
29
30
31
32
#include <iostream>

using namespace std;

struct Carpet		//struct
{
    float lengthInFeet, widthInFeet;
};

float displayArea(float lengthInFeet, float widthInFeet)
{
	float area;
	area = lengthInFeet * widthInFeet; 
	return area;
}


int main()			//
{
	Carpet myCarpet; 
	myCarpet.lengthInFeet=7.8;
	myCarpet.widthInFeet=4.2;

	cout<<"Length:";
	cin>>myCarpet.lengthInFeet;
	cout<<"Width:";
	cin>>myCarpet.widthInFeet;
	cout<<"The square feet of carpet is:" <<displayArea <<endl;
	
	return 0;

}


the program runs and gives an output like this:

Length: 10
width: 2
The square feet of carpet is: 004010F0

then I different number still the answer is the same
We posted links to the function tutorial, read it
Try this please:

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;
struct Carpet		//struct
{
   float lengthInFeet, widthInFeet;
    float displayArea()
    {
	
	 return (lengthInFeet * widthInFeet); 
	 
    }
};
int main()			
{
	Carpet myCarpet; 
	myCarpet.lengthInFeet=0;
	myCarpet.widthInFeet=0;

	cout<<"Enter the Length:";
	cin>>myCarpet.lengthInFeet;
	cout<<"Enter the Width:";
	cin>>myCarpet.widthInFeet;
	cout<<"The square feet of carpet is:" << myCarpet.displayArea() << endl;

	return 0;

}


by the way,i'm a beginner too.
Last edited on
closed account (jwC5fSEw)
It's not supposed to be a member function.

Also, please don't give out complete answers.
qudongfang

You save the day man! tnx!

the program works.

but i don't understand this:

write a main() function that instantiates a Carpet object and assigns values to its data fields


what does that means? is it?

1. before running a program i'll put values in lengthInFeet and widthInFeet so when i run the program the result will shown automatically or

2. after running the program the user must input the length and width of the carpet?
To everyone on the forum: If you absolutely must give out complete answers, at least put in some obscure syntax error and state that you did so. I do that from time to time.

-Albatross


To the kid: Just write a main function that creates a Carpet and defines how long and how wide it is.

-Albatross


To qdongfang: That was a direct answer to his problem. Nice, though if you used a base class pointer... nah. Never mind.

-Albatross
Last edited on
closed account (jwC5fSEw)
OP:

That solution posted isn't the answer to your question. Your function has to take a SINGLE Carpet object and modify its values.
Here is another hint : Lets look at the Carpet structure:

1
2
3
4
5
6
struct Carpet {

     float lengthInFeet;
     float widthInFeet;

};


Pretty basic structure. Since you know you can cin values to the Carpet's data members, what else can you do with the data members? I'm going to hard code some values in the sample code below, but you can eaisly cin values to the Carpet

1
2
3
4
5
6
7

   Carpet rug; // declares a structure named "rug" of type Carpet.

   rug.legnthInFeet = 14.5;    // hard coded value for the rug's lengthInFeet data member.

   rug.widthInFeet = 12.2;   // hard coded value for the rug's widthInFeet data member.



Now that you know you have good values in two data members, you could do just about anything to them...

1
2
3
4
5
6
7
8

   float temp;
   temp = rug.lengthInFeet  - rug.widthInFeet;   // subtract widthInFeet from lengthInFeet and assign that value to temp. 

   rug.lengtheInFeet = rug.widthInFeet * 50;  // multiply the widthInFeet by 50 and assign that value to "rug's" lengthInFeet data member

   // etc.


Now how would you determine the area if you have both the length and width stored in data members of a structure?


Remember when you pass a structure into a function, you are sending the whole structure, and the function will have access to a copy of all those data member values...

1
2
// sample function header
float area( Carpet ); // this function will receive a Carpet and return a float value.... 


When you figure it out it will be an AH HA moment!! :D Good Luck!
I am sorry ,I'm new here and a beginner too.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

struct Carpet
{
    float lengthInFeet;
    float widthInFeet;
};

void setL(Carpet &myCarpet,float &length)
{
        myCarpet.lengthInFeet = length;
}
 void setW(Carpet &myCarpet,float &width)
{
       myCarpet.widthInFeet = width;
 }
 float getArea(const Carpet &myCarpet)
 {
	
	return (myCarpet.lengthInFeet * myCarpet.widthInFeet); 
	 
}

int main(int argc,char ** argv)			
{
	Carpet myCarpet; 
                myCarpet.lengthInFeet = 0;
                myCarpet.widthInFeet = 0;
                float length,width;
	
	cout<<"Enter the Length:";
	cin>>length;
                setL(myCarpet,length);

	cout<<"Enter the Width:";
	cin>>width;
                setW(myCarpet,width);

	cout<<"The square feet of carpet is:" << getArea(myCarpet) << endl;

	return 0;

}


There is a few bugs in this program.maybe you can try to fix and compile it!
Last edited on
qudongfang

u dont have to say sorry but u did a great job, u just give me an idea.

@all
i know the answer stated above is not the real answer but an idea.
aside from this forum, i have different reference forum.
so i compile the answers from the users and study the structure of
of their statement, at last i got what i've been looking for..

thank you very much for the time that everyone spent in my post,
this forum is a MUST for a beginner like me a very NOOBISHKID.

i guess its a bout time to close this thread to prevent flaming..

:D
closed account (jwC5fSEw)
No problem, everyone was a beginner once!

Though I don't see why getArea() should take a reference if it returns a value.
@qudongfang
no offense but you're doing it wrong. no need for pointers and reference to solve this problem.

Shadow Addict+1
@Shadow Addict
@blackcoder41
I think getArea() (Carpet &myCarpet) will be faster than getArea(Carpet myCarpet).
Use pointer or reference not a copy of the object myCarpet as parameter will be faster.

Last edited on
Yes, it will avoid copying. But it should be passed by const reference if you are not willing to modify the object passed
@Bazzy
TNK u for your advice.
ok i know that this is gonna sound funny but what is the struct, float, and all the other fun stuff mainly used for and also could the person of just made a simple little program that goes allong the lines of something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()
{
int l;
int w;
cout << "enter length: ";
cin >> l;
cout << "enter width: ";
cin >> w;

cout << l*w;
cout << " is the area."
return 0;
}


because i think that would of been alot faster and easyer then having to remake one code ten times

this only is a little area finding program that i built in my free time that finds the area of rectangle or a square

so what are the differences between my code and what the other code is required to be because personally i think my code is in nicer shape but thats only cause what i made i can fully understand whatever my code has and the other code have in common like cin and cout thats about the only things that are similar please help solve this minor issue.

Thanks Skillet
because i think that would of been alot faster and easyer then having to remake one code ten times
Yes, but that's not what OP was asked to do.

Create a structure named Carpet that has two public data members: lengthInFeet and widthInFeet. Write a main() function that instantiates a Carpet object and assigns values to its fileds. Pass the object to a function named Area() that calculates the carpet area in square feet and displays the results. Save the file as Carpet.cpp
Pages: 123