c++ - 当我第二次调用函数两次时,函数运行时我无法输入
问题描述
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
const int Length = 61;
void getinput (char []);
int main()
{
char a[Length];
char b[Length];
getinput(a);
getinput(b);
}
void getinput (char input[]){
cout << "Enter Input: ";
cin.get(input, 60);
cout << "You Entered " << input <<endl;
}
当我运行此代码时,我无法输入我的第二个输入吗?我不明白我所做的只是两次调用相同的函数。
这是得到的输出:
输入输入:嗨
您输入:您好
输入输入:您输入
解决方案
那是因为cin
缓冲输入
cin.get(input, 60);
第二次调用会将第一次调用留下的换行符解释为其输入。
为防止此类错误,您可以getline
改用:
std::cin.getline (input,60);
推荐阅读
- .htaccess - Opencart Redirect 301 最后带有“/”
- r - Lavaan 错误:要替换的项目数不是替换长度的倍数
- google-bigquery - Google Cloud Stackdriver 日志记录 - 将日志导出到 BigQuery - 拒绝访问数据集
- caching - BigQuery 中的查询缓存
- jupyter-notebook - Jupyter:单击散点图中的点时触发输出,该点替换以前的输出(而不是被附加)
- c# - 在开关中访问返回的字符串数组时出现问题
- django - 使用 Django 的 ORM 进行复杂的 M2M 过滤
- node.js - ReactJS:使用 Bearer 授权获取 GET 方法
- docker - 一个容器如何生活在两个网络中可以找到每个网络的同名容器
- aws-lambda - Secrets Manage:无法轮换密钥,无法调用指定的 Lambda 函数