hi
so this project requires an input of U, M, or D and as many as you like of them (i think, might be only up to 100 based on charUMD[100] array in code) also, you have to end it with # in order to indicate the end. i am trying to say that if you dont include #, then program should cout the error message Error: String Does Not End With '#'! as seen in if statement in main. however, it gives me unhandled exception error because of "out of range". how can i fix this?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int length;
char UMD[100];
char value;
int i = 0;
string list;
void swap(char s[], int i, int j)
{
char x, y;
x = s[i];
y = s[j];
s[j] = x;
s[i] = y;
}
void sort(char s[])
{
for (int c = 0; c < (i - 1); c++)
{
for (int d = 0; d < i - c - 1; d++)
{
if (UMD[d] <= UMD[d + 1]) /* For decreasing order use < */
{
swap(UMD, d, (d + 1));
}
}
}
}
int main()
{
cout << "TYPE U, M, or D for the array" << endl;
cout << "Type any other letter to stop adding!" << endl;
cin >> list;
int o = 0;
while (list[o] != 0)
{
i++;
o++;
}
int p = 0;
while ((list.at(p)) != '#')
{
UMD[p] = list.at(p); p++;
}
if (UMD[o] != '#'){ cout << "Error: String Does Not End With '#'!" << endl; system("pause"); exit(0); }//<------IF UMD[o] is not '#', then it should display this message! However it does not work for '#' IDK WHY!
for (int z = 0; z<p; z++)
{
if (list.at(z) != 'U'&&list.at(z) != 'M'&&list.at(z) != 'D')
{
cout << "Error: character other than U, M, or D provided" << endl; system("pause"); exit(0);
cout << list.at(z);
}
}
sort(UMD);
cout << endl;
cout << "Here is the sorted version of the array" << endl;
for (int z = 0; z<i; z++)
{
cout << UMD[z];
}
system("pause");
return 0;
}