Image

Imagelilbrattyteen wrote in Imagecpp

problems with "if" statements

This is a program to maintain a dynamic array of strings. It runs fine except that:

- If I enter two strings and use the SearchAndRemove function to remove the first one, it removes the second one as well. In this case, I set that index value to "99" to remove the string; when displaying, it won't show the "99". (Is it possible to simply deallocate that index value of the array so the program won't even use it?)

- At the end of the SearchAndRemove function it gives me this:



---------------------------------
To fill the array, type 'Fill' or '1'.
To display the contents of the array, type 'Display' or '2'.
To add some more values into the array after filling it, type 'Add' or '3'.
To search for a particular value, type 'Search' or '4'.
To search for a value and remove it too, type 'Remove' or '5'.
To reprint this menu at any time, type 'Menu' or '6'.
To exit the program, type 'Exit' or '7'.
---------------------------------
Enter Command: 1
Enter string, 'stop' to stop:
hello
Enter string, 'stop' to stop:
world
Enter string, 'stop' to stop:
stop

Enter Command: 5
Enter search argument: hello

The value at StringArrayPointer[0] matches your search argument.
Enter 1 if you would like to delete this value. If not, enter 0.
1

Enter Command:
Invalid Command
Enter Command: 2

The contents of the array are:

Enter Command:



That "Invalid Command" that I italicized shows up automatically after hitting "1" then enter from the SearchAndRemove function. What's causing that?



#include
#include
using namespace std;

int main();
void Menu();
int Fill(int Count, string* StringArrayPointer);
int Display(int Count, string* StringArrayPointer);
int Add(int Count, string* StringArrayPointer);
void Search(int Count, string* StringArrayPointer);
int SearchAndRemove(int Count, string* StringArrayPointer);


int main()
{
string* StringArrayPointer;
StringArrayPointer = new string[100];

int Count=0;
string Command;
Menu();
while (true)
{
cout << "\nEnter Command: ";
getline(cin, Command);
if (Command == "Menu" || Command == "6")
Menu();
else if (Command == "Fill" || Command == "1")
Count = Fill(Count, StringArrayPointer);
else if (Command == "Display" || Command == "2")
Display(Count, StringArrayPointer);
else if (Command == "Add" || Command == "3")
Count = Add(Count, StringArrayPointer);
else if (Command == "Search" || Command == "4")
Search(Count, StringArrayPointer);
else if (Command == "Remove" || Command == "5")
Count = SearchAndRemove(Count, StringArrayPointer);
else if (Command == "Exit" || Command == "7")
break;
else
cout << "\nInvalid Command";
}
delete[] StringArrayPointer;
}

void Menu()
{
cout << "\n---------------------------------";
cout << "\nTo fill the array, type 'Fill' or '1'.";
cout << "\nTo display the contents of the array, type 'Display' or '2'.";
cout << "\nTo add some more values into the array after filling it, type 'Add' or '3'.";
cout << "\nTo search for a particular value, type 'Search' or '4'.";
cout << "\nTo search for a value and remove it too, type 'Remove' or '5'.";
cout << "\nTo reprint this menu at any time, type 'Menu' or '6'.";
cout << "\nTo exit the program, type 'Exit' or '7'.";
cout << "\n---------------------------------";
}

int Fill(int Count, string* StringArrayPointer)
{
string Temp;
for (Count=0 ; Count < 100 ; )
{
cout << "Enter string, 'stop' to stop: " << endl;
getline (cin, Temp);
if (Temp == "stop")
break;
else
{
StringArrayPointer[Count] = Temp;
Count++;
}
}
return Count;
}

int Display(int Count, string* StringArrayPointer)
{
if (Count == 0)
{
cout << "/nThe array is empty.";
return 0;
}
int K;
cout << "\nThe contents of the array are: " << endl;
for (K=0; K < Count; K++)
{
if (StringArrayPointer[K] != "99")
cout << "\nThe string at StringArrayPointer[" << K << "] is " << StringArrayPointer[K] << endl;
}
return 0;
}

int Add(int Count, string* StringArrayPointer)
{
string Temp2;
for ( ; Count < 100 ; )
{
cout << "Enter string, 'stop' to stop: " << endl;
getline (cin, Temp2);
if (Temp2 == "stop")
break;
else
{
StringArrayPointer[Count] = Temp2;
Count++;
}
}
return Count;
}

void Search(int Count, string* StringArrayPointer)
{
string SearchArgument;
cout << "Enter search argument: ";
getline(cin, SearchArgument);
int L, P;
for (L=0, P=0 ; L <= Count ; L++)
{
if (StringArrayPointer[L] == SearchArgument)
{
cout << "\nThe value at StringArrayPointer[" << L << "] matches your search argument.";
P++;
}
}
if (P == 0)
cout << "\nNo values match your search argument.";
}

int SearchAndRemove(int Count, string* StringArrayPointer)
{
string SearchArgument2;
cout << "Enter search argument: ";
getline(cin, SearchArgument2);
int M, QuestionOfRemoval, N;
for (M=0, N=0 ; M <= Count ; M++)
{
if (StringArrayPointer[M] == SearchArgument2)
{
cout << "\nThe value at StringArrayPointer[" << M << "] matches your search argument.";
while(true)
{
cout << "\nEnter 1 if you would like to delete this value. If not, enter 0." << endl;
cin >> QuestionOfRemoval;
if (QuestionOfRemoval == 1)
{
StringArrayPointer[M] = "99";
Count = Count - 1;
N++;
break;
}
if (QuestionOfRemoval == 0)
{
N++;
break;
}
if (QuestionOfRemoval != 1 && QuestionOfRemoval != 0)
cout << "\nPlease enter a valid answer.";
}
}
}
if (N==0)
cout << "\nNo values match your search argument.";
return Count;
}



Sorry for the huge post. Thanks for the help!