copying a member of a struct array to another member of another struct array

Hi, i am trying to copy a member of a struct within an array to a member of another array in a struct... I dont know where i am going wrong?

I either want to copy member by member or i want to copy the whole struct to another struct. This is for an assignment so although there might be a better way to do this i have to stay within these parameters.
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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

struct menuItemType
{
        string menuItemDescription;
        double menuItemPrice;
};


int main()
{

    menuItemType menuItem[2];
    menuItemType menuOrderItem[2];

    menuItem[0].menuItemDescription = "Eggs";
    menuItem[0].menuItemPrice = 30.00;

    menuItem[1].menuItemDescription = "Toast";
    menuItem[1].menuItemPrice = 15.00;

    menuItem[0].menuItemDescription = menuItem[0].menuItemDescription;
    menuItem[0].menuItemPrice = menuItem[0].menuItemPrice;

    menuItem[1].menuItemDescription = menuItem[1].menuItemDescription;
    menuItem[1].menuItemPrice = menuItem[1].menuItemPrice;

    cout << menuItem[0].menuItemDescription << ": R" << menuItem[0].menuItemPrice << endl;
    cout << menuItem[1].menuItemDescription << ": R" << menuItem[1].menuItemPrice << endl << endl;

    cout << menuOrderItem[0].menuItemDescription << ": R" << menuOrderItem[0].menuItemPrice << endl;
    cout << menuOrderItem[1].menuItemDescription << ": R" << menuOrderItem[1].menuItemPrice << endl;

    return 0;
}
[code]
[/code]
Last edited on
You mean something like
 
menuOrderItem[0] = menuItem[0];
?
Correct, but for some reason it doesn't run correctly

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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

struct menuItemType
{
        string menuItemDescription;
        double menuItemPrice;
};


int main()
{

    menuItemType menuItem[2];
    menuItemType menuOrderItem[2];

    menuItem[0].menuItemDescription = "Eggs";
    menuItem[0].menuItemPrice = 30.00;

    menuItem[1].menuItemDescription = "Toast";
    menuItem[1].menuItemPrice = 15.00;

    menuItem[0] = menuOrderItem[0];
    menuItem[1] = menuOrderItem[1];

    cout << menuItem[0].menuItemDescription << ": R" << menuItem[0].menuItemPrice << endl;
    cout << menuItem[1].menuItemDescription << ": R" << menuItem[1].menuItemPrice << endl << endl;

    cout << menuOrderItem[0].menuItemDescription << ": R" << menuOrderItem[0].menuItemPrice << endl;
    cout << menuOrderItem[1].menuItemDescription << ": R" << menuOrderItem[1].menuItemPrice << endl;

    return 0;
}

I think i see it now.......................................................................................................................................................................................

Thank you.
@MHS1986, look at what Peter87 wrote. Again and this time very closely.

Long-winded explanation.....

With lines 20 - 24 you are assigning some values to your array to menuItem. The menuOrderItem array has bogus data.

Lines 26-27 you assign the bogus data in menuOrderItem to menuItem, overwriting valid data with not valid data. Oooops!

26
27
   menuOrderItem[0] = menuItem[0];
   menuOrderItem[1] = menuItem[1];

You are using std::string, you should #include <string> . Don't assume a compiler's implementation will automatically add it.

And don't include headers you aren't using. They won't cause problems, but they do add IMO needless clutter.

To recap:
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
#include <iostream>
#include <string>

struct menuItemType
{
   std::string menuItemDescription;
   double      menuItemPrice;
};

int main()
{
   menuItemType menuItem[2];
   menuItemType menuOrderItem[2];

   menuItem[0].menuItemDescription = "Eggs";
   menuItem[0].menuItemPrice       = 30.00;

   menuItem[1].menuItemDescription = "Toast";
   menuItem[1].menuItemPrice       = 15.00;

   menuOrderItem[0] = menuItem[0];
   menuOrderItem[1] = menuItem[1];

   std::cout << menuItem[0].menuItemDescription << ": R" << menuItem[0].menuItemPrice << '\n';
   std::cout << menuItem[1].menuItemDescription << ": R" << menuItem[1].menuItemPrice << "\n\n";

   std::cout << menuOrderItem[0].menuItemDescription << ": R" << menuOrderItem[0].menuItemPrice << '\n';
   std::cout << menuOrderItem[1].menuItemDescription << ": R" << menuOrderItem[1].menuItemPrice << '\n';
}
Eggs: R30
Toast: R15

Eggs: R30
Toast: R15
Since C++ considers a struct to be similar to a class, how default access is handled (private vs. public) is the main difference, it is possible to initialize your struct array when created (works with at least C++14):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

struct menuItemType
{
   std::string menuItemDescription;
   double      menuItemPrice;
};

int main()
{
   menuItemType menuItem[2] { { "Eggs", 30.00 }, { "Toast", 15.00 } };

   std::cout << menuItem[0].menuItemDescription << ": R" << menuItem[0].menuItemPrice << '\n';
   std::cout << menuItem[1].menuItemDescription << ": R" << menuItem[1].menuItemPrice << "\n\n";

   menuItemType menuOrderItem[2];

   menuOrderItem[0] = menuItem[0];
   menuOrderItem[1] = menuItem[1];

   std::cout << menuOrderItem[0].menuItemDescription << ": R" << menuOrderItem[0].menuItemPrice << '\n';
   std::cout << menuOrderItem[1].menuItemDescription << ": R" << menuOrderItem[1].menuItemPrice << '\n';
}
Last edited on
You could create a vector in order to store all your dishes. Just an idea ++

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>
#include <vector>
#include <iomanip>

struct menuItemType
{
    std::string menuItemDescription;
    float menuItemPrice;
};

std::vector<menuItemType> menuList;

void newBreakfast(std::string food, float price)
{
    menuItemType dish;
    dish.menuItemDescription = food;
    dish.menuItemPrice = price;

    menuList.push_back(dish);
}

int main()
{
    newBreakfast("Egg", 30.20f);
    newBreakfast("Toast", 15.50f);
    newBreakfast("Spam", 20.40f);

    for (auto& i : menuList)
        std::cout << i.menuItemDescription << "\t" << std::fixed << std::setprecision(2) << i.menuItemPrice << '\n';

    return 0;
}


Egg     30.20
Toast   15.50
Spam    20.40
Last edited on
Topic archived. No new replies allowed.