【问题标题】:if(isspace()) statement not working C++if(isspace()) 语句不起作用 C++
【发布时间】:2016-11-03 18:42:49
【问题描述】:

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

这是完整的程序

#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++;
        }
     }
  }
}

我遇到问题的代码部分是

 if(isspace(next))
        {
            cout << "Space!" << endl;
            out << firstname << ' ';
            i++;
        }

代码应该(在我看来)从文件中读取每个字符并添加到字符串中,一旦到达空格,就将该字符串 firstname 写入输出文件,但它没有t,而是在控制台中得到这个输出

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

等等……

注意名字应该是 Hess Dam.... 并且应该发生的事情是将 Hess 保存到 firstname 和 Dam... 保存到 lastname .相反,它只是将整个内容添加到 firstname 字符串中姓氏之后的选项卡,并且它永远不会写入输出文件。它读取选项卡是因为它退出了 for 循环(来自 isalpha(next)),但 isspace(next) 参数由于某种原因不起作用

【问题讨论】:

  • 据我所知in &gt;&gt; next 从不给你空格。如here 中所述;一开始,它会跳过空白,然后在空白处停止。

标签: c++ loops for-loop isspace


【解决方案1】:

对不起,没有足够的声誉来评论它,但是有两个错误的答案。 zahir 的评论是正确的。 std::isspace(c,is.getloc()) 对于 is 中的下一个字符 c 为真(此空白字符保留在输入流中)。运算符 >> 永远不会返回空格。

【讨论】:

    【解决方案2】:

    您正在检查 next 是否是 for 循环中的字母字符:for(; isalpha(next) &amp;&amp; i &lt; 3; in &gt;&gt; next)
    根据the documentation,在默认的 C 语言环境中,“空格”字符不被视为字母字符。您可以更改您的语言环境来解决这个问题,或者(在我看来)更可取的是修改 for 循环以接受空格。
    for(; (isalpha(next) || isspace(next)) &amp;&amp; i &lt; 3; in &gt;&gt; next) 这样的东西应该允许循环处理空格以及字母字符

    编辑:正如其他几个人指出的那样,我错过了这样一个事实,即在这里使用 >> 运算符会导致您一开始就看不到空格,所以我的回答并不完整。我会留下它,以防它仍然有用。

    【讨论】:

    • OP 不会看到带有&gt;&gt;的空格,它们会被丢弃。否则正确。
    【解决方案3】:

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

    in >> noskipws >> next;
    

    【讨论】:

      【解决方案4】:

      您的问题不在于isspace 函数,而在于您的for 循环,您正在强制for 循环使用isalpha 仅处理字母字符,在这种情况下,该字符不能是@ 987654326@、isdigitispunctisspace)。看看this

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-12
        • 1970-01-01
        • 2012-07-16
        • 2021-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        相关资源
        最近更新 更多