c++ - 控制台输入直到。并输入

标签 c++

你好,我对 cin 有个小问题。我如何在 != '.''\n' 时执行 cin ?下面是我的代码

#include <stdio.h>
#include <iostream>
using namespace std;
void reverse(char x[])
{
    if(*x=='\0')
    {
        return;
    }
    else 
    {
        reverse(x+1);
        cout<<*x;
    }
}

int main(){
    char a[]="";
    while (cin.get()!='\n'&&cin.get()!='.') {
        cin>>a;
    }
    reverse(a);  
}

输入:foo。 输出:.o 它削减了我的最后 2 个词

最佳答案

您不应该同时包含 "stdio.h" (或更好的 <cstdio> )和 <iostream> .前者允许您使用 C 标准 IO 函数,而后者是 C++ 的标准。选择一个,例如在您的代码中您实际上使用了一些 <iostrem>设施,包括那个。

您面临的问题基本上需要 3 个步骤:

  • 读取标准输入直到 '.''\n'已输入。
  • 存储除'.'以外的每个字符或 '\n' .
  • 将读取的字符以相反的顺序发送到标准输出。

我认为第二点是重点。你在哪里存储你读到的字符?在您的代码中,您似乎使用了 c 风格的 null 终止字符数组,但您没有为此分配必要的内存。

然后,当您使用 cin.get() 时,您实际上是从输入流中删除一个字符,并且在读取一个字符之前执行了 2 次。

我也不认为在最后一步使用递归是个好主意。也许它在您分配的任务中是强制性的,但是像这样使用递归(以及典型的阶乘示例)最好留在书本中。如果您想使用堆栈来完成任务,最好明确地这样做,如下所示:

#include <iostream>
#include <stack>

int main() {
    char c;
    std::stack<char> sc;

    while ( std::cin.get(c) && c!='\n' && c!='.' ) 
        sc.push(c);
    std::cout << std::endl;
    while ( !sc.empty() )  {
        std::cout << sc.top();
        sc.pop();
    }
    std::cout << std::endl;

    return 0;
}

完成此任务的一种更“自然”的方法是将字符存储在 std::string 中并以相反的顺序显示它们:

#include <iostream>
#include <string> 

int main()
{
    std::string a;      
    char c;        

    while ( std::cin.get(c)  &&  c != '.'  &&  c != '\n' )
        a += c;      

    std::cout << std::endl;
    // you can print what you read with:        std::cout << a;

    for ( std::string::reverse_iterator rit = a.rbegin(); rit != a.rend(); ++rit )
        std::cout << *rit;

    std::cout << std::endl;
    return 0;
}

如果要使用 char 数组来存储输入,则必须根据需要预先分配足够的内存。

#include <iostream>

#define MAX_CHARS 128       

int main()
{
    char a[MAX_CHARS + 1];      // enough to store 128 char and the '\0'
    char c;        
    int counter = 0;            // better knowing how many char you read

    for ( counter = 0; counter < MAX_CHARS
            && std::cin.get(c) && c!='.' && c!='\n'; counter++ )
        a[counter] = c;

    a[counter] = '\0';          

    std::cout << std::endl;
    // you can print what you have read with:        std::cout << a;

    while( counter > 0 ) {
        --counter;
        std::cout << a[counter];            
    }
    std::cout << std::endl;
    return 0;
}

如果您正在从终端读取输入,则在任何情况下都必须输入一整行后跟换行符,因为您正在读取缓冲输入。如果您需要无缓冲输入并在 . 时停止读取字符或 enter按下则没有标准的 C++ 解决方案,这取决于您的环境。为了简单起见,您可以使用 c 风格(和弃用的)getch():

#include <cstdio>
#include "conio.h"

#define MAX_CHARS 128       

int main()
{
    char a[MAX_CHARS + 1];      // enough to store 128 char and the '\0'
    char c;        
    int counter = 0;            // better know how many char you read

    while ( counter < MAX_CHARS && (c=getch())
            && c!='.' && c!='\n' && c!='\r' ) {
        putchar(c);            
        a[counter] = c;
        counter++
    }
    a[counter] = '\0';          

    printf("\n");
    // you can print what you read with:        printf("%s\n",a);

    while( counter > 0 ) {
        --counter;
        putchar(a[counter]);            
    }
    printf("\n");
    return 0;
}

关于c++ - 控制台输入直到。并输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34212272/

相关文章:

c++ - 在镜像模式下为监视器创 build 备上下文

c++ - CUDA NVCC 编译器错误

c++ - WAVEFORMATEX - 最后如何读取编解码器数据?

c++ - 在编写 iterator 和 const_iterator 时避免代码重复

c++ - 使类不可 move 的用例(如果有)是什么?

c++ - 如何在运行时添加osgEarth功能?

c++ - Linux 上的 pthread_cancel() 导致异常/核心转储,为什么?

C++ 字符编码

c++ - 使用 boost::asio 时的引用问题(我猜)

c++ - OpenCV VideoWriter 断言失败 img.cols == width && img.rows == height*3