控制台输入,直到.然后输入
Console input until . and enter
你好,我对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]
的内容。
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 读取一组用户输入,按升序排序,然后打印结果
- 我将如何计算输入的所有商品的总和,然后计算总价?
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- Qt读取串行输入,然后将其拆分为单独的变量
- 有没有办法将 getline() 与外部文本文件一起使用,然后重新获得 cin 的控制权以从控制台获取输入?
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- 流对象来表示文件输入,然后是标准输入?
- 如何存储多个用户输入,然后以C++显示它们
- 将int输入函数,将int输入开关,然后更改int的值
- 如果用户想要输入 x 数量的数字,我如何设法跟踪它然后结束程序?
- 如何声明一个未知大小的数组,然后输入直到我想要,然后获取数组的大小
- 使用ENUM输入和退出菜单,然后切换SFML
- 获取一个输入字符串,将其转换为其十六进制值,然后将其十六进制存储为 int
- 请求用户输入,然后忽略它
- 它可以工作,直到我输入第二个输入然后它关闭