Question about C++ programming. Giving values to all the members?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Question about C++ programming. Giving values to all the members?

  1. #1
    Join Date
    Oct 2013
    Posts
    1

    Question about C++ programming. Giving values to all the members?

    I have a c++ assignment. Which is:
    Write a C++ program to implement the following description:
    1. Define a global structure and name it GStruct with the following members:
    a. X as integer
    b. Y as integer.
    2. Define a local structure inside the main and name it LStruct with the following members:
    a. X as integer
    b. Y[3] as GStruct
    3. Inside the main declare two variables V1 and V2 of type LStruct.
    4. Give values to all of their members by using input statement (cin).
    5. If V1 equal V2 print "They are equal" else print "Not Equal".

    I did everything that's asked from me and i got no errors. But it's not working like it's asked from me. Been working on this questions for more than 5 hours. It's driving me crazy. I went over it like 100 times and no use. Please help....
    This is what i came up with and am sure it's all right but there is something missing but i don't know what it is.



    #include <iostream>
    using namespace std;
    struct GStruct
    {
    int x;
    int y;
    };

    int main()
    {
    struct LStruct
    {
    int x;
    GStruct y[3];
    };
    LStruct V1, V2;
    cin>>V1.x;
    cin>>V2.x;

    for (int i=0; i<3;i++)
    {
    cin>>V1.y[i].x;
    cin>>V1.y[i].y;
    }

    for (int i=0; i<3;i++)
    {
    cin>>V2.y[i].x;
    cin>>V2.y[i].y;
    }

    if (V1.x == V2.x && V1.y == V2.y)
    cout<<"They are equal"<<endl;
    else
    cout<<"Not equal"<<endl;

    return 0;
    }
    help me
    thanks
    Catalog Designing Companies in Banglore

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,366

    Re: Question about C++ programming. Giving values to all the members?

    Before posting please format your code properly and use code tags. Go advanced, select code and click '#'

    there is something missing but i don't know what it is
    the y member of LStruct is an array of GStruct. Therefore V1.y and V2.y are pointers to the array, and not the contents of the array itself. They contain the memory address of the start of the array and as V1 and V2 are different variables, the memory address of V1.y and V2.y are therefore different hence the equality condition for V1.y and V2.y is always false.

    You need to compare each element of the y array.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center