Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/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++ - Fatal编程技术网

C++ 在数组中存储字符串

C++ 在数组中存储字符串,c++,C++,我创建了一个函数,将字符串存储在一个不带空格和标点的数组中: int main() { char arr[1];int b,i=0; bool newl = false; for(;!newl;) { arr[i]=cin.get(); b = arr[i]; if(b>=65&&b<=90) i++; else if(b>=97&&b<=122

我创建了一个函数,将字符串存储在一个不带空格和标点的数组中:

int main()
{
    char arr[1];int b,i=0;
    bool newl = false;
    for(;!newl;)
    {
        arr[i]=cin.get();
        b = arr[i];
        if(b>=65&&b<=90) i++;
        else if(b>=97&&b<=122) i++;
        if(arr[i]=='\n') newl =true;
    }
    for(int j=0;j<i;j++)
    cout << arr[j];
}
intmain()
{
字符arr[1];int b,i=0;
bool-newl=false;
对于(;!newl;)
{
arr[i]=cin.get();
b=arr[i];

如果(b>=65&&b=97&&b您仅将一个元素分配给
arr
,则禁止访问
arr[1]
或进一步访问

std::vector
用作可变长度数组

此外,我建议您应该使用库
cctype
中的
isalpha()
,而不是与魔法数字进行比较,因为魔法数字相对难以理解,并且依赖于字符代码

#include <iostream>
#include <vector>
#include <cctype>
using std::cin;
using std::cout;

int main()
{
    std::vector<char> arr;int b,i=0;
    bool newl = false;
    for(;!newl;)
    {
        b = cin.get(); // store the input directly into b
        if(isalpha(b)) arr.push_back(b);
        if(b=='\n') newl = true;
    }
    for(size_t j=0;j<arr.size();j++)
        cout << arr[j];
}
#包括
#包括
#包括
使用std::cin;
使用std::cout;
int main()
{
标准::向量arr;int b,i=0;
bool-newl=false;
对于(;!newl;)
{
b=cin.get();//将输入直接存储到b中
如果(isalpha(b))到达,则向后推(b);
如果(b=='\n')newl=true;
}

对于(SiHeZiTj=0;JoWHOST <代码> ARR < /代码>有1的大小,不需要整数,实际上可以使用类似<代码>(b>‘a’)< /> >,当然,char b;我认为C++没有任何绑定检查,所以我只把1放进去。“乔尔,我不明白你的意思。我想检查它是否是字母表(和CAP形式的C)。OOOPS这是一个愚蠢的错误。谢谢你的建议,但是我认为C++没有任何绑定检查,为什么它现在有?