所以这个项目需要输入U,M或D以及你喜欢的任意数量(我认为,基于代码中的charUMD [100]数组可能只有100个),你必须结束用#来表示结束。我想说,如果你不包括#,那么程序应该输出错误消息错误:字符串不会以'#'!如在主要的if语句中所见。然而,由于"超出范围"它给了我未处理的异常错误。我该如何解决这个问题?
#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];
}
cin >> list;
system("pause");
return 0;
}