Adjusting to a new level

Pages: 123
Hi everyone,

I'm new here, I'm Arkie by the way, a 1st year college student taking up Information Technology shifting from Hotel and Restaurant Management.

I'm new at this course because I've done my BSHRM course for a year and now I have difficulties adjusting my knowledge about computer subjects.

My professor gave me a take home quiz which is all about C++ programming, I'm new at this subject which why I need somebody's help.

The problem should be like this:

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

I asked my professor some help but he gave me a hint only, he says I will use STRUCT and FUNCTION.
The problem statement is so clear that it almost gives you the answer. What is it you need help with?

A structure is basically a kind of data. So declaration looks like this:

1
2
3
4
5
6
7
8
9
struct structureName
{typeOfData nameOfVariable;};

/*for example*/

struct Carpet
{
float lengthInFeet, widthInFeet;
};


You make a global declaration of this structure (for example somwhere efore main function).
You have access to component variables of this structure by using dot.

Somewhere in main function:

1
2
3
Carpet myCarpet;
myCarpet.lengthInFeet=5.62;
myCarpet.widthInFeet=2.21;


It's just the structure. Making a function which calculate area of this carpet shouldn't be a problem.
Sigh.
taking up Information Technology shifting from Hotel and Restaurant Management
Wow. Just wow. I think that's the most bizarre career change I've ever heard, except maybe for that man who was an engineer for like twenty years and then decided to become a doctor when he was sixty.
Do you like Do you like Air Jordan Shoes? it includes Air Jordan Shoes,Nike Air Max Shoes;
you can choose:Nike Shox Shoes,Nike Shoes,Nike Air Force 1,Nike Air Max 90,Nike Air Max LTD,Nike Air Max TN,Nike Air Rift Shoes,Nike Dunk SB High,Nike SB Shoes,Nike Shox R3 Shoes,Nike Shox R4 Shoes,Nike Air Max 87.
site:http://www.shoespurchasing.com.
I know its easy for everyone the problem given above, but its my first time to face this kind of question and I believe it needs a logic, aw.. that most probably hurt my brain a lot.

so can anyone site an example according to my given problem so that I can study it?
The problem is that the "question" is its own answer. There's no problem to solve, the exercise is just a measure of how much you know about C++ syntax. In your case, nothing.
I recommend you read this site's tutorial (http://www.cplusplus.com/doc/tutorial/ ) up to "Functions (I)" and then read the "Data Structures" section of the same tutorial.
I'm sorry, but there's really no logic that can be applied here; it's just a matter of knowing the language. To put it in more familiar terms, the exercise says "write 'one plus two times three equals seven' in mathematical notation". There's no problem to solve, it's just telling you to translate information in one language to another. If you don't know the target language, you plain and simple can't do it.
is my statement correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
using namespace std;

struct Carpet
{
float lengthInFeet, widthInFeet;
};

int multiplication (int l, int w)
{
	int c;
	c=l*w;
	return (c);

int main()
{
	int l, w;
	cout<<"Enter length:";
	cin<<"l";
	cout<<"Enter width:";
	cin>>"w";
	cout<<"The square feet of carpet is: <<c";
}
closed account (jwC5fSEw)
You came sorta close, though that program shouldn't compile.

1. You're including stdio.h (actually should be cstdio), but using cout and cin which are in iostream. stdio.h is a C header containing the C equivalents of cout and cin (printf and scanf).

2. The function should be named Area, as the problem specifies.

3. Your function needs to return float and take floats (not ints) as arguments if you want to pass floats to it.

4. Your function needs a closing brace.

4.1. This isn't critical, but it's a bit more efficient to say return (l*w) instead of creating a local variable and using that (also more readable).

5. Changing the function to take floats, l and w should be declared as floats in main() instead of ints.

6. Nowhere in main do you actually use your function. You either need to declare another float and make that equivalent to the result of your function applied to l and w, or you need to use the function in your cout statement (i.e. cout << "The square feet of carpet is: " << Area(l, w); )

7. Your first cin is using << instead of >>.

8. Don't put quotes around the variable you're reading with cin. Just say cin << w.

9. Your final cout statement won't print out the value of c, it'll print "The square feet of carpet is: <<c". To print the value of c, you need to say cout << "The square feet of carpet is: " << c; (or better yet, cout << "The square feet of carpet is: " << Area(l, w); . That removes c, which isn't really needed.)
Last edited on
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
#include <iostream>
using namespace std;

struct Carpet
{
float lengthInFeet, widthInFeet;
};

float Area (int l, int w)
{
	int c;
	c=l*w;
	return (c);

int main()
--> {
	float l, w;
	cout<<"Enter length:";
	cin>>l;
	cout<<"Enter width:";
	cin>>w;
	cout<<"The square feet of carpet is:" <<Area(l,w);
	return 0:
}

}


C:\Documents and Settings\Administrator\Desktop\function\function.cpp(16) : error C2601: 'main' : local function definitions are illegal

one more error
Last edited on
closed account (jwC5fSEw)
You're still missing the closing brace on Area. And again: Area() shouldn't take two ints, it should take two floats.
Last edited on
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
#include <iostream>
using namespace std;

struct Carpet
{
float lengthInFeet, widthInFeet;
};

float Area (float l, float w)
{
	int c;
	c=l*w;
	return (c);
}

int main()
{
	float l, w;
	cout<<"Enter length:";
	cin>>l;
	cout<<"Enter width:";
	cin>>w;
	cout<<"The square feet of carpet is:" <<Area(l,w);
	return 0;
}


my program is now working..

My final questions:

1. did I make it right, correct?

2. is my program uses struct and function?

3. did i create a structure base on the problem statement?

4. what's the use of lengthInFeet and widthInFeet when everything is right?

5. did my main() function instantiates a carpet object?

6. did it assigns values to its data fields?

7. did the object pass to a function named Area()?

I wanna make sure if I'm done the right thing so that when I pass this through my professor
I have the rights to defend my answer.

Last edited on
Remove function.cpp from the project and try again.
closed account (jwC5fSEw)
Okay I just reread the first post and I feel stupid.

Your function needs to take one object of type Carpet and use both member variables to calculate the area in the function. In main(), you need to create a Carpet, assign values to both floats, and pass the Carpet object to Area(). Sorry for misleading you; I just looked at your code without reading the first post carefully.
my program already running tnx man.

is there wrong on my code?

what values should I assign to each float?

is the user needs to manually input the width and length so that
the square feet of carpet is gain?
Last edited on
Pass the object to a function named Area() that calculates the carpet area in square feet and displays the results.


Here are some hints. Once you have your struct you can access the data members with the dot (.) modifier.

Example:
1
2
3
4
5
6
7
   Carpet rug;  // declare a structure named rug of type Carpet.  

   cout << "\nWhat is the carpet's length ? ";
   cin >> rug.lengthInFeet;   // assigns rug's "lengthInFeet"'s data member. 

   cout << "\nWhat is the carpet's width ? ";
   cin >> rug.widthInFeet;  // assigns rug's  "widthInFeet"'s data member.  


Then you'd want to pass rug into a function that accepts a Carpet structure.

1
2
3
4
5
6
float area( Carpet arg ) {

// do your magic here
// make sure you return a float. 

}

wow.. This forum is an interactive compiler with human readable error messages! Cool.


:-D

Ciao, Imi.
PS: Sorry, no offense. Just got a good mood today.
what magic do u mean??

i dont have any knowledge bout that.

all I know is to follow instruction given by the forumer.

wish someone gave the exact answer coz i'd tried everything

even reading the tuts given still i dont hav a clue... T_T
Pages: 123