【问题标题】:Why is my read function throwing away the first char when calling it a second time?为什么我的读取函数在第二次调用时会丢弃第一个字符?
【发布时间】:2016-03-03 20:25:10
【问题描述】:

我写了这个函数来从 cin 中读取一个 char 数组:

#include <iostream>
#include <stdlib.h>
#include <cstring>
#include "main.h"
int read (char* buffer, int size) {

//read from standart input (with whitespaces)
//cin >> buffer;
cin.get(buffer, size);
cin.ignore(1, '\n');
cout << "cin get buffer: " << buffer << endl;

//if not a correct input
if (!cin.good())
    return 0;

cout << "cin.good: " << cin.good() << endl;

//user wants to quit
if (!strcmp (buffer, "end"))
    return 0;

return 1;
}

当我第一次在我的主 returncode = read (first, MAX) 中调用此函数并输入 blabla 时,它会将“blabla”读入缓冲区。 (我通过 cout-for-loop 进行了检查)

当我想读取另一个数组(用于比较)并执行完全相同的操作 returncode = read(second, MAX) 时,它只会读取“labla”,而 second[0] 仍然为空。

我的错在哪里?随意询问其余的代码,但我认为问题出在这段代码 sn-p 之内。

提前谢谢你!

ps:我真的是 C++ 新手,所以请耐心等待我 :) pps:这是一个大学测试,我们不允许使用字符串类..

编辑:测试上述内容的简单主要方法

main.cpp:

#include <iostream>
#include "main.h"
#define MAX 200
using namespace std;

int main () {
   char first [MAX] = {0};
   char second [MAX] = {0}; 

   cout<<"Please enter the first string to compare: "<<endl;
   returncode = read (first, MAX);
   cout<<"Please enter the second string to compare: "<<endl;
   returncode = read (second, MAX);

    switch (strcmp_ign_ws(first, second, MAX)) {
    case EQUAL:
       cout << "Strings are equal!" << endl;
       break;
    case SMALLER:
       cout << "String 1 is lexically smaller!" << endl;
       break;
    case BIGGER:
       cout << "String 1 ist lexically bigger!" << endl;
       break;
   }
}

和main.h:

#define EQUAL 0
#define SMALLER -1
#define BIGGER 1

int read (char*, int);
int strcmp_ign_ws (char*, char*, int);
int main ();

编辑 2:添加字符串比较忽略空格函数

由于错误似乎不在读取函数中,这是使用第一和第二两个输入缓冲区的文件:

#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <iostream>
#include <ctype.h>
#include "main.h"

using namespace std;

char * rm_ws (char * buffer, const int size) {

    int i,j;
    char *output=buffer;
    for (i = 0, j = 0; i<size; i++,j++)
    {
        if (buffer[i]!=' ')
            output[j]=buffer[i];
        else
            j--;
    }
    output[j]=0;
    return output;

}


int strcmp_ign_ws (char * first, char * second, int size) {
    first = rm_ws(first, size);
    second = rm_ws(second, size);


    if (strcmp (first, second) == 0)
        return EQUAL;
    if (strcmp (first, second) < 0)
        return SMALLER;
    if (strcmp (first, second) > 0)
         return BIGGER;

    }
}

ps:rm_ws 函数已经来自stackoverflow

【问题讨论】:

  • 您能否尝试创建一个Minimal, Complete, and Verifiable Example,包括一个简单的main 函数,该函数调用您显示的函数。还包括程序的实际(和预期)输出。
  • 我编辑我的帖子。请一秒钟。
  • 是否为第二次通话输入了一些内容?
  • 首先你的循环是错误的,它不应该使用i &lt; size作为条件,而是first[i] != '\0'
  • 在修复一个小问题后没有使用 GCC 或 MSVC 进行复制(这可能表明发布的代码不是 OP 实际运行的代码?)。

标签: c++ arrays


【解决方案1】:

您是在 Windows 还是基于 Unix 的系统上调用它?在 Unix 上它没有这个问题,所以它可能与以 Windows 结尾的不同行('\r\n' vs. '\n')有关,而您使用的是

cin.ignore(1, '\n');

不确定是否是问题所在,但只需尝试用简单的替换该行

cin.ignore();

【讨论】:

  • 感谢您的回答。我正在使用 Ubuntu - 现在尝试你的答案
  • 就像现在一样,您的代码不会重现该问题,至少在我的系统上是这样。您应该考虑一个事实,即该错误可能完全在其他地方。
  • 是的,我编辑了我的帖子并添加了其余代码。谢谢
【解决方案2】:

经过几个小时的调试,我终于找到了答案:

在我的rm_ws 函数中出现了一个小错误:

必须是output[j-1] = 0,而不是output[j] = 0,就在return 语句之前。

谢谢你的帮助!

【讨论】:

  • rm_ws() 没有问题。你的问题在别处。请提供一个简短而完整的示例代码来重现该问题。
  • 感谢您的努力,但问题已经解决,正如第二次编辑中提到的,这是我正在运行的整个代码
猜你喜欢
  • 2020-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-08
  • 2020-08-19
  • 1970-01-01
  • 2016-12-08
  • 2019-07-24
相关资源
最近更新 更多