Visual c++ 未处理的异常??if语句的微小错误我似乎无法修复

Visual c++ 未处理的异常??if语句的微小错误我似乎无法修复,visual-c++,Visual C++,因此,这个项目需要输入U、M或D以及任意数量的输入(我认为,基于charUMD[100]数组,在代码中可能最多只有100个),而且,为了表示结束,您必须以#结束它。我想说的是,如果您不包括#,那么程序应该会显示错误消息error:String不以“#”结尾!如主菜单中的if语句所示。但是,由于“超出范围”,它给了我未处理的异常错误。我怎样才能解决这个问题 #include <iostream> #include <string> #include <vector&g

因此,这个项目需要输入U、M或D以及任意数量的输入(我认为,基于charUMD[100]数组,在代码中可能最多只有100个),而且,为了表示结束,您必须以#结束它。我想说的是,如果您不包括#,那么程序应该会显示错误消息error:String不以“#”结尾!如主菜单中的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;
}
#包括
#包括
#包括
使用名称空间std;
整数长度;
char-UMD[100];
字符值;
int i=0;
字符串列表;
无效交换(字符s[],整数i,整数j)
{
字符x,y;
x=s[i];
y=s[j];
s[j]=x;
s[i]=y;
}
无效排序(字符s[])
{
对于(int c=0;c<(i-1);c++)
{
对于(int d=0;d如果(UMD[d]你首先运行一个依赖于
存在的循环,然后才尝试检查它是否确实存在。此外,条件
UMD[o]!=''.\code>总是正确的:毕竟,我们知道
列表[o]==0
(否则我们就不会退出第一个循环),并且如果第二个循环实际工作,
UMD
的内容将是
list
内容的副本。
std::string
知道自己的长度,可以通过
list.length()
list.size()
访问(这两个是同义词)。所有这些寻找终止字符的方法都是错误的,不管是
0
还是
。那么…我如何解决这个问题以反映我的需要呢?大致如下:
cin>>list;size\t p=list。首先查找(“UMD”)中的字符;如果(p!=string::npos){/*在索引p*/}找到非UMD字符