Monday, May 24, 2010

I can't get this program to work. What am i doing wrong .C++?

Write a funtion that uses a Rectange structure reference variable as its parameter and stores the user's input


in the structure's members.








#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;string%26gt;


using namespace std;





struct Rectangle


{


int lenght;


int width;





};





void gettangle(Rectangle%26amp;);








int main()


{


Rectangle len;


gettangle (len);


return 0;


}





void gettangle(Rectangle %26amp;value)


{


cout %26lt;%26lt; "Enter Lenght:";


cin %26gt;%26gt; value.lenght;


cout %26lt;%26lt; "Enter Width:";


cin %26gt;%26gt; value.width;





}


void gettangle(Rectangle value)


{


cout %26lt;%26lt; value.lenght %26lt;%26lt; ":" %26lt;%26lt;value.width%26lt;%26lt; ":"%26lt;%26lt;endl;


}

I can't get this program to work. What am i doing wrong .C++?
Man, you're code is pretty hashed up. I don't know what you're trying to do in some of it.


First off, I see two Functions both with void type that operate on a given Rectangle's members (length, width). Both of which are named gettangle().


One of these functions takes the variable name, and the other takes the variable address. You only have one prototype above main. You have


void gettangle(Rectangle%26amp;);


you will also need one that takes a rectangle object (leave off the ampersand)


Second to that, your first function prototype is wrong and if you build your second prototype that you need the same way, it will be wrong too. You need to add in the variable name. You should have two prototypes and they will look like this.


void gettangle(Rectangle %26amp;value);


and


void gettangle(Rectangle value);


Now you're prototypes are right, let's move down to your functions. The first function you have is taking an address. Inside the function, to access the members of the rectangle object, you can't use the dot operator. Right now you're code looks like this "cin %26gt;%26gt; value.lenght;" It needs to look like this "cin %26gt;%26gt; value-%26gt;lenght;" Do you see the difference. You'll need a hyphen and the right arrow character to access the object members length and width.


That should do it for your functions.


Your main however is only going to run the program and it is going to run your second function running this code."{


cout %26lt;%26lt; value.lenght %26lt;%26lt; ":" %26lt;%26lt;value.width%26lt;%26lt; ":"%26lt;%26lt;endl;


}"


You're going to get output, but your rectangle variables haven't been set. You need to add the code "gettangle(%26amp;len);" in your main for it to ask you for the inputs before it outputs them. I think that's it. Make those changes and post if you're still not running.


BTW, the person above addressed that the compiler couldn't tell which function to use. That is correct, however if you have two prototypes up there, one that looks like this


gettangle(Rectangle%26amp; value);


(A note on the line of code above, I am not sitting at a compiler right now, so I can't check to see, but I'm not sure that that is where the ampersand goes to let the compiler know that it will be using an address. It may go just before the name like this %26amp;Rectangle, or it could go somewhere else. I just don't remember. So that may mess you up. Looking it up should be easy if you have a book or soemthign though.)


and another that looks like this


gettangle(Rectangle value);


it will be able to tell the difference just by whether you're passing in a object or an address. There isn't a need to rename the function however I would just because it's a good habit not to use the same function name for multiple operations, especially operations that have nothing to do with each other.
Reply:Generally if you provide the error message, it helps.





I think the problem here is that you have these two functions:





void gettangle(Rectangle %26amp;value);


void gettangle(Rectangle value);





Because both take a Rectangle, the C++ compiler can't tell which to use. You should rename the 2nd one to puttangle or similar (or change the function signature).
Reply:why r u having two gettangle function??


the program is already correct. if the problem is u not getting the output(cout%26lt;%26lt;value...)because u got 2 gettangle fuction.





ps:use system("pause"); in main fuction to see output :P


No comments:

Post a Comment