Choose 2 ingredients to make 15 foods...

I tried to write a c ++ program that would make 15 foods with six ingredients.
Of the six ingredients, only two are selected.
So, I wanted to make 15 kinds of food.
However, as soon as I entered the two materials, an error message came out immediately.
I didn't know which part was wrong, so I searched hard for the wrong part.
I think it's wrong in the string combin::calc section, but I don't know where to start.
Please help this ignorant human being.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
using namespace std;

class Ingredient {
	string name[6] = { "apple", "eggs", "ham", "potato", "cheese", "tuna" };
public:
	Ingredient();
	void getName();
	string setName() { return name[6]; }
};

Ingredient::Ingredient()
{
	cout << "Ingredient ";
}

void Ingredient::getName()
{
	cout << ": ";
	for (int i = 0; i < 6; i++)
	{
		cout << name[i] << " ";
	}
	cout << endl;
}

class Combin : public Ingredient
{
string names[15] = { "pizza", "burger", "chopstick", "fried rice", "tuna soup", "egg with rice", "Gratin", "Cheese ball", "Tuna mayo rice bowl", "Potato croquette", "sandwitch", "rice with burger", "French fries", "potato soup", "tuna Sashimi"};
public:
Combin();
string calc(string name);
};


Combin::Combin()
{
cout << "Choose two things you like : ";
}

string Combin::calc(string name)
{
string n, a;
cin >> n >> a;
for (int i = 0; i < 6; i++)
{
	if (n == setName() && a == setName())  
	{
		cout << name[i] << name[i]<< endl;
	}
}

{
	for (int i = 0; i < 6; i++)
	{
		if (name[0] && name[i + 1])
			cout << names[i];
	}

	for (int i = 0; i < 4; i++)
	{
		if (name[1] && name[i + 2])
			cout << names[i + 6];
	}

	for (int i = 0; i < 3; i++)
	{
		if (name[2] && name[i + 3])
			cout << names[i + 10];
	}

	for (int i = 0; i < 2; i++)
	{
		if (name[3] && name[i + 4])
			cout << names[i + 12];
	}

	if (name[4] && name[5])
		cout << names[14];
}
return 0;
}

int main()
{
 string d;
 Ingredient i;
 i.getName(); 
 Combin c;
 c.calc(d);
}
The reason for your crash is in your setName() function at line 10. You're attempting to access the 7th element of your name array, but your array only has 6 elements. You're attempting to access memory past the end of the array, which is undefined behaviour.

That function doesn't make any sense. It isn't setting anything at all, and even if that array index were valid, all it would ever do is return the same name every time it was called. What are you trying to do with it?
Oh thank you very much.
I forgot the information I was learning.
I thought I could return each of the 6 arrays defined above.
Thank you for finding my big mistake.
You're welcome.
Topic archived. No new replies allowed.