Running Half an If Statement

I am currently having an issue with the code below. It is a rather simple code that takes a users input to add food to a menu and can take that input and remove it as well. However there is a problem when you add a food, remove the food, and then try to exit the program (answering no to add and remove a food).

I have gone through several tests. If you answer no to remove food after you remove a food, the if statement (indicated below) will run, but only half of it. The first part is skipped so I figured there was just a missing } but I couldn't find one. All of the code prior to the for statement ( also indicated is skipped) however shouldn't it all be skipped.

Thnx in advance.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

void mainMenu();
void FoodInformation();
void FoodInfoInput(string FoodArray[][31]);
void FoodInfoOutput(string FoodArray[][31]);

int main()
{
	mainMenu();

	system("pause");
	return 0;
}
void mainMenu()
{
	int choice;
	cout << "Main Menu:" << endl
		<< "1. Food" << endl;
	cin >> choice;

	switch (choice)
	{
	case 1:
		FoodInformation();
		break;
	default:
		cout << "Input Error. Please enter a valid action." << endl;
		break;
	}
}
void FoodInformation()
{
	string FoodArray[8][31];
	FoodArray[1][0]= "Fruits";

	FoodInfoInput(FoodArray);

	system("CLS");
	int groupChoice;
	cout << "Food Group: " << endl
		<< "1. Fruits " << endl
		<< "Choose a Group: ";
	cin >> groupChoice;

	system("CLS");

	int FoodNum= 1;
	switch (groupChoice)
	{
	case 1:
		cout << "Fruits: " << endl;
		while (FoodArray[1][FoodNum] != "")
		{
			cout << FoodArray[1][FoodNum] << endl;
			FoodNum++;
		}
		break;
	default:
		FoodInformation();
		break;
	}
	string addFood;
	cout << "Add a food (yes/ no) ";
	cin >> addFood;
	transform(addFood.begin(), addFood.end(), addFood.begin(), toupper);

	if (addFood == "YES")
	{
		string FoodName;
		cout << "Enter food name: ";
		cin.get();
		getline(cin, FoodName);
		transform(FoodName.begin(), FoodName.end(), FoodName.begin(), tolower);
		FoodName[0]= toupper(FoodName[0]);
		
		string FoodCheck;
		cout << "Are you sure you wish to add \"" << FoodName << "\" to the menu? ";
		cin >> FoodCheck;
		transform(FoodCheck.begin(), FoodCheck.end(), FoodCheck.begin(), toupper);

		if (FoodCheck == "YES")
		{
			while (FoodArray[groupChoice][FoodNum] != "")
			{
				FoodNum++;
			}
			FoodArray[groupChoice][FoodNum]= FoodName;
		}

		FoodInfoOutput(FoodArray);
		FoodInformation();
	}

	string removeFood;
	cout << "Remove a food (yes/ no) ";
	cin >> removeFood;
	transform(removeFood.begin(), removeFood.end(), removeFood.begin(), toupper);
	if (removeFood == "YES")// This is the misfunctional if statement
	{
		string FoodName;
		cout << "Enter food name: ";
		cin.get();
		getline(cin, FoodName);
		transform(FoodName.begin(), FoodName.end(), FoodName.begin(), tolower);
		FoodName[0]= toupper(FoodName[0]);
		for (int count = 0; count < 31; count++)// This is the misfunctional for statement
		{
			if (FoodArray[groupChoice][count] != FoodName && count == 30)
			{
				cout << "Could not find item with name \"" << FoodName << "\" on the menu" << endl;
			}
			if (FoodArray[groupChoice][count] == FoodName)
			{
				string FoodCheck;
				cout << "Are you sure you wish to remove \"" << FoodName << "\" from the menu? ";
				cin >> FoodCheck;
				transform(FoodCheck.begin(), FoodCheck.end(), FoodCheck.begin(), toupper);

				if (FoodCheck == "YES")
				{
					while (FoodArray[groupChoice][count] != "")
					{
						FoodArray[groupChoice][count]= FoodArray[groupChoice][count+ 1];
						count++;
					}
				}

				FoodInfoOutput(FoodArray);
				FoodInformation();
			}
		}
	}
}
void FoodInfoInput(string FoodArray[][31])
{
	ifstream inFoodInfo;
	inFoodInfo.open("FoodData_DoNotOpen.dat");
	for (int count_= 1; count_ < 8; count_++)
	{
		for (int count= 1; FoodArray[count_][count- 1]!= ""; count++)
			getline(inFoodInfo, FoodArray[count_][count]);
	}
	inFoodInfo.close();
}
void FoodInfoOutput(string FoodArray[][31])
{
	ofstream outFoodInfo;
	outFoodInfo.open("FoodData_DoNotOpen.dat");
	for (int count_= 1; count_ < 8; count_++)
	{
		for (int count= 1; FoodArray[count_][count- 1]!= ""; count++)
			outFoodInfo << FoodArray[count_][count] << endl;
	}
	outFoodInfo.close();
}
That's because your recursive function calls itself inside a for loop.

Let's say FoodArray[1][1] is Apple. You decide to delete Apple, and so it goes into the for loop you start on line 115.

When count is 1, the if check on line 121 is true, then you ask for input and finally delete the Apple. The rest of the array is empty, so count stays at 1.

On line 138, you recursively call FoodInformation() again. But remember, you're still in the for loop begun on line 115! This time you say no, you don't want to remove a food, and so it exits this call of the function.

Now you're back to the previous call of the function, where you just deleted something. You're still in the loop, so it increments count and keeps with the checking. Eventually, count reaches 30 and doesn't find Apple, giving you the message that the item wasn't found.

That's when it exits the loop and finally exits the program.
Last edited on
Thank you so much... I was having a hard time with this. Do you know of any way that I can do the task I was hoping for or no?

Edit: I changed the for statement to this but for some reason there's still a loop.

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
if (removeFood == "YES")// This is the misfunctional if statement
	{
		string FoodName;
		cout << "Enter food name: ";
		cin.get();
		getline(cin, FoodName);
		transform(FoodName.begin(), FoodName.end(), FoodName.begin(), tolower);
		FoodName[0]= toupper(FoodName[0]);
int count= 0;
		while (count < 31 && FoodArray[groupChoice][count] != FoodName)
		{
			if (FoodArray[groupChoice][count] != FoodName && count == 30)
			{
				cout << "Could not find an item with name \"" << FoodName << "\" on the menu" << endl;
			}
			count++;
		} // End of Search Fail
		if (FoodArray[groupChoice][count] == FoodName)
		{
			string FoodCheck;
			cout << "Are you sure you wish to remove \"" << FoodName << "\" from the menu? ";
			cin >> FoodCheck;
			transform(FoodCheck.begin(), FoodCheck.end(), FoodCheck.begin(), toupper);

			if (FoodCheck == "YES")
			{
				while (FoodArray[groupChoice][count] != "")
				{
					FoodArray[groupChoice][count]= FoodArray[groupChoice][count+ 1];
					count++;
				}
			}
		}

		FoodInfoOutput(FoodArray);
		FoodInformation();
}


Edit 2: Never mind found it was the continuation of the loop in the add statement.
Last edited on
closed account (j3Rz8vqX)
Recursive stacking may eventually hit a limit, a work around would be loops.

Possible example:
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;

void mainMenu();
void FoodInformation();
void FoodInfoInput(string FoodArray[][31]);
void FoodInfoOutput(string FoodArray[][31]);

void transform(std::string &str, int (*myFunction)(int));
bool add(bool &repeat,string FoodArray[8][31],int groupChoice, int &FoodNum);
bool sub(bool &repeat,string FoodArray[8][31],int groupChoice);

int main()
{
	mainMenu();

	system("pause");
	return 0;
}
void mainMenu()
{
	int choice;
	cout << "Main Menu:" << endl
		<< "1. Food" << endl;
	cin >> choice;

	switch (choice)
	{
	case 1:
		FoodInformation();
		break;
	default:
		cout << "Input Error. Please enter a valid action." << endl;
		break;
	}
}
void FoodInformation()
{
    string FoodArray[8][31];
    FoodArray[1][0]= "Fruits";

    FoodInfoInput(FoodArray);
    bool repeat;
    do
    {
        repeat = false;
        system("CLS");
        int groupChoice;
        int FoodNum= 1;
        do
        {
            cout << "Food Group: " << endl
                << "1. Fruits " << endl
                << "Choose a Group: ";
            cin >> groupChoice;
            cin.ignore(255,'\n');
            system("CLS");
            if(groupChoice==1)
            {
                cout << "Fruits: " << endl;
                while (FoodArray[1][FoodNum] != "")
                {
                    cout << FoodArray[1][FoodNum] << endl;
                    FoodNum++;
                }
                break;
            }
        }while(true);
        system("CLS");
        if(!add(repeat,FoodArray,groupChoice,FoodNum))
            sub(repeat,FoodArray,groupChoice);
    }while(repeat);
}
void FoodInfoInput(string FoodArray[][31])
{
	ifstream inFoodInfo;
	inFoodInfo.open("FoodData_DoNotOpen.dat");
	for (int count_= 1; count_ < 8; count_++)
	{
		for (int count= 1; FoodArray[count_][count- 1]!= ""; count++)
			getline(inFoodInfo, FoodArray[count_][count]);
	}
	inFoodInfo.close();
}
void FoodInfoOutput(string FoodArray[][31])
{
	ofstream outFoodInfo;
	outFoodInfo.open("FoodData_DoNotOpen.dat");
	for (int count_= 1; count_ < 8; count_++)
	{
		for (int count= 1; FoodArray[count_][count- 1]!= ""; count++)
			outFoodInfo << FoodArray[count_][count] << endl;
	}
	outFoodInfo.close();
}
void transform(std::string &str, int (*myFunction)(int))
{
    for(int i=0;i<str.size();++i)
        str[i] = myFunction(str[i]);
}
bool add(bool &repeat,string FoodArray[8][31],int groupChoice, int &FoodNum)
{
    string addFood;
    cout << "Add a food (yes/ no) ";
    getline(cin,addFood);
    transform(addFood, toupper);
    if (addFood == "YES")
    {
        repeat = true;
        string FoodName;
        cout << "Enter food name: ";
        getline(cin, FoodName);
        transform(FoodName, tolower);
        FoodName[0]= toupper(FoodName[0]);

        string FoodCheck;
        cout << "Are you sure you wish to add \"" << FoodName << "\" to the menu? ";
        getline(cin,FoodCheck);
        transform(FoodCheck, toupper);

        if (FoodCheck == "YES")
        {
            while (FoodArray[groupChoice][FoodNum] != "")
            {
                FoodNum++;
            }
            FoodArray[groupChoice][FoodNum]= FoodName;
        }

        FoodInfoOutput(FoodArray);
        return true;
    }
    return false;
}
bool sub(bool &repeat,string FoodArray[8][31],int groupChoice)
{
    string removeFood;
    cout << "Remove a food (yes/ no) ";
    getline(cin,removeFood);
    transform(removeFood, toupper);

    if (removeFood == "YES")// This is the misfunctional if statement
    {
        repeat = true;
        string FoodName;
        cout << "Enter food name: ";
        getline(cin, FoodName);
        transform(FoodName, tolower);
        FoodName[0]= toupper(FoodName[0]);
        for (int count = 0; count < 31; count++)// This is the misfunctional for statement
        {
            if (FoodArray[groupChoice][count] != FoodName && count == 30)
            {
                cout << "Could not find item with name \"" << FoodName << "\" on the menu" << endl;
            }
            if (FoodArray[groupChoice][count] == FoodName)
            {
                string FoodCheck;
                cout << "Are you sure you wish to remove \"" << FoodName << "\" from the menu? ";
                getline(cin,FoodCheck);
                transform(FoodCheck, toupper);

                if (FoodCheck == "YES")
                {
                    while (FoodArray[groupChoice][count] != "")
                    {
                        FoodArray[groupChoice][count]= FoodArray[groupChoice][count+ 1];
                        count++;
                    }
                }
                FoodInfoOutput(FoodArray);
                break;
            }
        }
        return true;
    }
    return false;
}

Hopefully the code was not too altered, I had minor trouble with the transform method.
Topic archived. No new replies allowed.