Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_While Loop_Char - Fatal编程技术网

C++ 检查字符中的空格和字符串中的数字

C++ 检查字符中的空格和字符串中的数字,c++,string,while-loop,char,C++,String,While Loop,Char,所以我面临这个问题: 编写一个程序,读取文本文件并检查其正确性 这个词。如果单词仅以字符开头,且 不包含任何数字。输入以分号结尾 ; 我试着用两种方法做到这一点: #include<iostream> using namespace std; int main() { char text; cout<<"Enter a group of words ending with a semicolon ; "; cin>>text;

所以我面临这个问题:

编写一个程序,读取文本文件并检查其正确性 这个词。如果单词仅以字符开头,且 不包含任何数字。输入以分号结尾 ;

我试着用两种方法做到这一点:

#include<iostream>
using namespace std;
int main()
{
    char text;

    cout<<"Enter a group of words ending with a semicolon ; ";
    cin>>text;
    int ctr=0;
    while(text !=';')
    {
     if (text  == ' ')   ctr++;
        cin>>text;
    }

    cout<<ctr;



    return 0;
}
#包括
使用名称空间std;
int main()
{
字符文本;
couttext;
int ctr=0;
而(文本!=';')
{
如果(文本=“”)ctr++;
cin>>文本;
}

cout
cin>>文本
忽略前导空格

text
是单个
char
时,
>
将读取下一个字符(如果可用),否则将失败

text
char
数组时,
>
将读取字符,直到遇到空白、达到其最大宽度或失败

无论哪种方式,
>
都不会返回它跳过的空白。因此
文本
永远不会等于
'
。此外,计数器应该计算实际读取的单词,而不是它们之间的空格

尝试类似以下内容:

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

using namespace std;

int main()
{
    cout << "Enter a group of words ending with a semicolon ; ";

    char text[512];
    int ctr = 0;

    while (cin >> setw(512) >> text)
    {
        if (strcmp(text, ";") == 0) break;
        ++ctr;
    }

    cout << ctr;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout>setw(512)>>text)
{
如果(strcmp(文本,“;”)==0)中断;
++ctr;
}

cout
cin>>文本
忽略前导空格

text
是单个
char
时,
>
将读取下一个字符(如果可用),否则将失败

text
char
数组时,
>
将读取字符,直到遇到空白、达到其最大宽度或失败

无论哪种方式,
>
都不会返回它跳过的空白。因此
文本
永远不会等于
'
。此外,计数器应该计算实际读取的单词,而不是它们之间的空格

尝试类似以下内容:

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

using namespace std;

int main()
{
    cout << "Enter a group of words ending with a semicolon ; ";

    char text[512];
    int ctr = 0;

    while (cin >> setw(512) >> text)
    {
        if (strcmp(text, ";") == 0) break;
        ++ctr;
    }

    cout << ctr;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout>setw(512)>>text)
{
如果(strcmp(文本,“;”)==0)中断;
++ctr;
}

cout
cin>>文本
忽略前导空格

text
是单个
char
时,
>
将读取下一个字符(如果可用),否则将失败

text
char
数组时,
>
将读取字符,直到遇到空白、达到其最大宽度或失败

无论哪种方式,
>
都不会返回它跳过的空白。因此
文本
永远不会等于
'
。此外,计数器应该计算实际读取的单词,而不是它们之间的空格

尝试类似以下内容:

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

using namespace std;

int main()
{
    cout << "Enter a group of words ending with a semicolon ; ";

    char text[512];
    int ctr = 0;

    while (cin >> setw(512) >> text)
    {
        if (strcmp(text, ";") == 0) break;
        ++ctr;
    }

    cout << ctr;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout>setw(512)>>text)
{
如果(strcmp(文本,“;”)==0)中断;
++ctr;
}

cout
cin>>文本
忽略前导空格

text
是单个
char
时,
>
将读取下一个字符(如果可用),否则将失败

text
char
数组时,
>
将读取字符,直到遇到空白、达到其最大宽度或失败

无论哪种方式,
>
都不会返回它跳过的空白。因此
文本
永远不会等于
'
。此外,计数器应该计算实际读取的单词,而不是它们之间的空格

尝试类似以下内容:

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

using namespace std;

int main()
{
    cout << "Enter a group of words ending with a semicolon ; ";

    char text[512];
    int ctr = 0;

    while (cin >> setw(512) >> text)
    {
        if (strcmp(text, ";") == 0) break;
        ++ctr;
    }

    cout << ctr;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
cout>setw(512)>>text)
{
如果(strcmp(文本,“;”)==0)中断;
++ctr;
}

cout也许最简单的方法是将输入读入
std::string
中,然后搜索不在一组有效字符中的字符

例如:

const std::string valid_characters = "abcdefghijklmnopqrstuvwxyz"
                                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string text_from_input;
std::getline(std::cin, text_from_input);
std::string::size_type position_of_invalid_char =
    text_from_input.find_first_not_of(valid_characters);

也许最简单的方法是将输入读入
std::string
,然后搜索一组无效字符中的字符

例如:

const std::string valid_characters = "abcdefghijklmnopqrstuvwxyz"
                                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string text_from_input;
std::getline(std::cin, text_from_input);
std::string::size_type position_of_invalid_char =
    text_from_input.find_first_not_of(valid_characters);

也许最简单的方法是将输入读入
std::string
,然后搜索一组无效字符中的字符

例如:

const std::string valid_characters = "abcdefghijklmnopqrstuvwxyz"
                                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string text_from_input;
std::getline(std::cin, text_from_input);
std::string::size_type position_of_invalid_char =
    text_from_input.find_first_not_of(valid_characters);

也许最简单的方法是将输入读入
std::string
,然后搜索一组无效字符中的字符

例如:

const std::string valid_characters = "abcdefghijklmnopqrstuvwxyz"
                                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string text_from_input;
std::getline(std::cin, text_from_input);
std::string::size_type position_of_invalid_char =
    text_from_input.find_first_not_of(valid_characters);