Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的字符串只接受三个字符?_C++_String - Fatal编程技术网

C++ 为什么我的字符串只接受三个字符?

C++ 为什么我的字符串只接受三个字符?,c++,string,C++,String,我试图检查所有元音是否都存在于给定的字符串中。如果它们出现在字符串中,则输出应为“是”,否则应为“否”。我还打印了a、e、I、o、和u(计数器变量)的值,但问题是该字符串只接受前3个字母,而不超过这3个字母 在这里,如果我将字符串输入为“aeiouqw”,则输出为: a:1 e:1 i:1 o:0 u:0 没有 #包括 #包括 #包括 使用名称空间std; int main() { int j=0; int a,e,i,o,u; a=e=i=o=u=0; 字符串s; couts[j++]; }而

我试图检查所有元音是否都存在于给定的字符串中。如果它们出现在字符串中,则输出应为“是”,否则应为“否”。我还打印了
a
e
I
o
、和
u
(计数器变量)的值,但问题是该字符串只接受前3个字母,而不超过这3个字母

在这里,如果我将字符串输入为“aeiouqw”,则输出为:

a:1
e:1
i:1
o:0
u:0
没有

#包括
#包括
#包括
使用名称空间std;
int main()
{
int j=0;
int a,e,i,o,u;
a=e=i=o=u=0;
字符串s;
couts[j++];
}而(s[j]!='\0');
int k=0;
而(s[k]!='\0')
{
开关(s[k++]
{
案例“a”:a++;
打破
案例“e”:e++;
打破
案例“i”:i++;
打破
案例“o”:o++;
打破
案例“u”:u++;
打破
默认:中断;
}
}

cout您以错误的方式读取输入。您应该使用该函数,而不是
do/while
循环(请参阅文章:):

然后,您将获得测试字符串的以下输出:

输入字符串:aeiouqw
a:1
e:1
i:1
o:1
u:1


关于你的问题标题:

为什么我的字符串只接受三个字符?

那是因为你在用这些语句打电话

do
{
    cin>>s[j++]; // <<<< UB here ...
} while(s[j]!='\0');  // <<< ... and here

如果要接受包含任何空格字符的输入


上面提到的两个语句都将调整
字符串的大小变量正确。

您的代码相当混乱。首先使用更好的变量名和更多的空格来分隔不同的部分,从而简化它。这一切都有助于提高可读性

此外,只需遍历字符串中的每个字符,并测试它是否是元音,如果是,则累加计数器,这会更容易

#include<iostream>
#include<string>
#include<stdio.h>

using namespace std;

int main()
{
    int a, e, i, o, u;
    a = e = i = o = u = 0;

    string str;
    cout << "Enter the string: ";

    cin >> str;

    for (int index = 0; index < str.length(); index++) {
        if (str[index] == 'a') {
            a++;
        }
        else if (str[index] == 'e') {
            e++;
        }
        else if (str[index] == 'i') {
            i++;
        }
        else if (str[index] == 'o') {
            o++;
        }
        else if (str[index] == 'u') {
            u++;
        }
    }


    cout << "a: " << a << endl;
    cout << "e: " << e << endl;
    cout << "i: " << i << endl;
    cout << "o: " << o << endl;
    cout << "u: " << u << endl;

    if ((a>0) && (e>0) && (i>0) && (o>0) && (u>0))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    system("pause");
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int a,e,i,o,u;
a=e=i=o=u=0;
字符串str;
cout>str;
对于(int index=0;index你到底想做什么?是什么让你认为你可以在放入任何东西之前访问
std::string
的元素?你应该尝试使用数组和更好的变量名,因为你现在的代码很难理解。
do{cin>>s[j++;}while(s[j]!='\0')
-->
cin>>s;
@closevorters只是选择了不清楚你在问什么,这有点太无知了,也不是正确的接近原因。虽然关于未定义行为的问题实际上有点难以回答,也很少为将来的研究提供好的来源。你的硬盘被格式化了,或者鼻孔里冒出了鼻恶魔似乎有点过头了,这是过分夸大了未定义的行为。@PeterSW:你永远不会知道。想想某人的车钥匙;)@PeterSW这只是一些,至少对像我这样的老家伙来说;-)…@Daniel我想我应该更清楚。你说得很对,switch语句也很容易奏效(在较长的列表中,使用甚至更快)。我指的更多的是使用“for”循环而不是“do while”循环来迭代字符串,因为他对它的使用令人困惑。@amallard完全同意
for
更整洁(如果这是标记为c++11,那么范围-
for
会更好!)。
#include<stdio.h>
do
{
    cin>>s[j++]; // <<<< UB here ...
} while(s[j]!='\0');  // <<< ... and here
cin>>s;
getline(cin, s);
#include<iostream>
#include<string>
#include<stdio.h>

using namespace std;

int main()
{
    int a, e, i, o, u;
    a = e = i = o = u = 0;

    string str;
    cout << "Enter the string: ";

    cin >> str;

    for (int index = 0; index < str.length(); index++) {
        if (str[index] == 'a') {
            a++;
        }
        else if (str[index] == 'e') {
            e++;
        }
        else if (str[index] == 'i') {
            i++;
        }
        else if (str[index] == 'o') {
            o++;
        }
        else if (str[index] == 'u') {
            u++;
        }
    }


    cout << "a: " << a << endl;
    cout << "e: " << e << endl;
    cout << "i: " << i << endl;
    cout << "o: " << o << endl;
    cout << "u: " << u << endl;

    if ((a>0) && (e>0) && (i>0) && (o>0) && (u>0))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    system("pause");
    return 0;
}