如何检查循环输出是否为数字,仅将数字输出打印到控制台。 [英] How do I check if loop output is a digit and only print the output with digits to the console.

查看:99
本文介绍了如何检查循环输出是否为数字,仅将数字输出打印到控制台。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此for循环中的每个其他输出都是一系列数字。我想检查输出是否为数字,如果是。将它们打印到控制台屏幕。



我尝试过:



Every other output in this for loop is a series of digits. I would like to check if the output is digits and if they are. Print them to the console screen.

What I have tried:

#include<iostream> //i.o
#include<cctype> //for isdigit
#include<sstream> //to phrase string
#include<string>
#include <vector>
#include <fstream>

using namespace std;
int main( void )
{
    std::string word;
    std::vector<std::string> file;

    std::ifstream in( "Files.txt" );

    while ( in >> word )
        file.push_back( word );

    for ( size_t i=0; i<file.size(); i++ )

        std::cout<< file.at(i) << '\n';
    in.close();

    return 0;
}

推荐答案

您需要获取字符串的内容并比较每个字符以查看它是否为数字。您可以使用C库函数 isdigit,iswdigit,_isdigit_l,_iswdigit_l [ ^ ]。
You need to get the content of the string and compare each character to see if it is a digit. You could use the C library function isdigit, iswdigit, _isdigit_l, _iswdigit_l[^].


这篇关于如何检查循环输出是否为数字,仅将数字输出打印到控制台。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆