首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >if(isspace())语句不能工作C++

if(isspace())语句不能工作C++
EN

Stack Overflow用户
提问于 2016-11-03 18:42:49
回答 4查看 1.9K关注 0票数 1

我正在为我的程序编写一个函数,该函数从文本文件中读取名字和姓氏,并将它们保存为两个字符串。但是,当for-循环到达名字和姓氏之间的第一个空格时,我无法执行if(isspace(next))语句。

这是完整的程序

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <ctype.h>

using namespace std;

void calcAvg(ifstream& in, ofstream& out);

int main()
{
  //open input and output file streams and check for any failures
  ifstream input;
  ofstream output;
  input.open("lab08_in.txt");
  output.open("lab08_out.txt");
  if(input.fail())
  {
    cout << "Error: File not Found!" << endl;
    exit(1);
  }
  if(output.fail())
  {
    cout << "Error: File creation failed!" << endl;
    exit(1);
  }
  calcAvg(input, output);

  return 0;
}

void calcAvg(ifstream& in, ofstream& out)
{
  int sum = 0;
  double average;

  //save first and last name to strings
  string firstname, lastname;
  char next;
  int i = 1;
  in >> next;
  for(; isalpha(next) && i < 3; in >> next)
  {
    if(i == 1)
    {
        firstname += next;
        cout << next << " was added to firstname" << endl;
        if(isspace(next))
        {
            cout << "Space!" << endl;
            out << firstname << ' ';
            i++;
        }
    }
    else if(i == 2)
    {
        lastname += next;
        cout << next << " was added to lastname" << endl;
        if(isspace(next))
        {
            cout << "Space!" << endl;
            out << lastname << ' ';
            i++;
        }
     }
  }
}

我遇到麻烦的代码部分是

代码语言:javascript
复制
 if(isspace(next))
        {
            cout << "Space!" << endl;
            out << firstname << ' ';
            i++;
        }

代码应该(在我看来)从文件中读取每个字符并添加到字符串中,一旦到达空格,将该字符串名写入输出文件,但它不会,相反,我在控制台中获得了这个输出

代码语言:javascript
复制
H was added to firstname
e was added to firstname
s was added to firstname
s was added to firstname
D was added to firstname
a was added to firstname
m was added to firstname

等等。

注意这个名字应该是赫斯大坝..。应该发生的是把赫斯的名字和大坝.敬姓。相反,它只是将整件事情加起来,直到姓氏后面的选项卡在名字字符串中,并且它从不写入输出文件。它读取选项卡,因为它退出for循环(从isalpha(next)),但是isspace(next)参数由于某种原因无法工作。

EN

回答 4

Stack Overflow用户

发布于 2016-11-03 18:53:06

您正在检查next是否是for-循环:for(; isalpha(next) && i < 3; in >> next)中的alpha字符。

根据文献资料,在默认的C语言环境中,“空格”字符不被认为是alpha字符。您可以更改区域设置以避免这种情况,或者更好地(在我看来)修改for-循环以接受空格。

类似于for(; (isalpha(next) || isspace(next)) && i < 3; in >> next)的内容应该允许循环与alpha字符一起处理空间。

编辑:正如其他一些人指出的那样,我忽略了这样一个事实:在这里使用>>运算符会导致您一开始就看不到空格,所以我的答案不完整。我要把它放在一边,以防它还能证明有用。

票数 1
EN

Stack Overflow用户

发布于 2016-11-03 19:03:29

对不起,没有名号来评论它,但是有两个错误的答案。查希尔的评论是对的。isspace(c,is.getloc())对于is中的下一个字符c(此空白字符保留在输入流中)是真的。运算符>>永远不会返回空白空间。

票数 1
EN

Stack Overflow用户

发布于 2022-03-09 17:59:43

默认情况下,operator>>将忽略所有空白字符,但您可以使用机械手的noskipws来规避这种行为,如下所示:

代码语言:javascript
复制
in >> noskipws >> next;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40409039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档