2

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;
    LStruct V2;

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


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


    for (int i=0; i<3; i++)
    {
        if (V1.y[i].x == V2.y[i].x && V1.y[i].y == V2.y[i].y && V1.x == V2.y)
            continue;
        else
            cout<<"Not equal"<<endl;
    }
    return 0;
}
4

1 回答 1

1

首先按照我V1.x == V2.y之前V1.x == V2.x的建议进行更改。然后将相等性检查更改为此,因为如果所有成员都相等,您只希望两个对象相等。

bool equal = true;
for (int i=0; i<3; i++)
{
    if (V1.y[i].x == V2.y[i].x && V1.y[i].y == V2.y[i].y && V1.x == V2.x)
        continue;
    else
    {
        equal = false;
        break;
    }
}
if ( equal ) 
   cout<<"Equal"<<endl;
else 
   cout<<"Not equal"<<endl;
于 2013-10-09T15:29:32.870 回答