控制台输入,直到.然后输入

Console input until . and enter

本文关键字:输入 然后 直到 控制台      更新时间:2023-10-16

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

#include <stdio.h>
#include <iostream>
using namespace std;
void reverse(char x[])
{
    if(*x=='')
    {
        return;
    }
    else 
    {
        reverse(x+1);
        cout<<*x;
    }
}
int main(){
    char a[]="";
    while (cin.get()!='n'&&cin.get()!='.') {
        cin>>a;
    }
    reverse(a);  
}

输入:foo。输出:-o它切下了我最后两个单词

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

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

  • 读取标准输入,直到输入'.''n'
  • 存储除CCD_ 11或CCD_
  • 以相反的顺序将读取的字符发送到标准输出

我认为第二点是关键。你读到的字符存放在哪里?在您的代码中,您似乎使用了c样式的以null结尾的char数组,但您没有为此分配必要的内存。

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

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

#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::字符串中,并按相反的顺序显示:

#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 ''
    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] = '';          
    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 ''
    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] = '';          
    printf("n");
    // you can print what you read with:        printf("%sn",a);
    while( counter > 0 ) {
        --counter;
        putchar(a[counter]);            
    }
    printf("n");
    return 0;
}

您可以将第一个get替换为peek:

while (cin.peek() != 'n' && cin.get() != '.')

std::basic_istream::peek将字符保留在缓冲区中,因此std::basic_istream::get读取相同的字符,但将其与其他字符进行比较

-错了。这将使n保持在缓冲区中,但.没有。

右:使用只提取一次并比较两次的char变量:

char c;
while (cin.get(c) && c != 'n' && c != '.')
    ...

执行get会从输入流中删除字符,因此在进入循环之前,while循环条件中的前两个字符就会被删除。cin >> a;读取下一个单词(空格分隔)并将其放入a中。它不会逐字符读取。这将"o"放入a中,因为"f"answers"o"已从输入流中删除。然后读取条件检查中的换行符,然后退出循环。

相反,您应该调用cin.get()一次,并将返回值存储为int,以便使用它,即

int ch = cin.get();
while(ch!='n' && ch != '.') {
    // Do something with ch
    //get the next character ready for the next iteration
    ch = cin.get();
}

我还应该提到,a只有足够的空间容纳一个字符。您应该制作一个更大的,像char a[200];这样的东西,它可以为200个字符创建空间。您还应该检查是否尝试访问任何超过a[199]的内容。

相关文章: