| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 844 人关注过本帖
标题:统计字符数量为什么总是多一个
只看楼主 加入收藏
dtxwz
Rank: 2
等 级:论坛游民
帖 子:79
专家分:45
注 册:2011-9-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
统计字符数量为什么总是多一个
// 3.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<array>
#include<ctime>
#include<fstream>
#include<cstdlib>
#include<cctype>
using namespace std;
const int strsize = 20;
struct intribute
{
    string name;
    double money;
};
int main()
{
    char ch;
    int count = 0;
    ifstream inFile;
    inFile.open("name.txt");
    while (inFile.good())
    {        

        inFile >> ch;        
        if (isalpha(ch))
            count++;

    }
    cout << count << endl;
    return 0;
}

name.txt
abcde
搜索更多相关主题的帖子: 统计 include name int count 
2018-08-02 22:37
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
while (inFile.good())
    {      

        inFile >> ch;   
当 inFile.good() 成功,就确定 inFile >> ch 能读取成功???

程序代码:
#include <iostream>
#include <fstream>
using namespace std;

int main( void )
{
    size_t count = 0;

    ifstream infile( "name.txt");
    for( char ch; infile>>ch; )
    {
        if( isalpha(ch) )
            count += 1;
    }

    cout << count << endl;
}
或者
程序代码:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;

int main( void )
{
    ifstream infile( "name.txt");
    size_t count = count_if( std::istream_iterator<char>(infile), std::istream_iterator<char>(), isalpha );
    cout << count << endl;
}


  
2018-08-03 08:50
快速回复:统计字符数量为什么总是多一个
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034030 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved