C++控制台在放置cin.get后关闭
c++ console closing after cin.get is placed
我正在编写一个将米转换为英尺的基本程序
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
# include <iostream>
int main()
{
using namespace std;
double feet;
short int input;
const double feettometer = 3.28 ; (changed after second comment, small mistake)
cout << "Enter meter value: ";
cin >> input;
feet = feettometer * input ;
cout << "your meter value of " << input << " in feet is " << feet ;
cin.get();
return 0;
}
为什么这个 con.get() 不能让控制台保持活动状态?
当您键入数字(如 123
并按回车键)时,输入流中123n
。提取到 input
时,123
将被删除,n
保留在流中。然后,当您调用cin.get()
时,将提取此n
。它不需要等待任何输入,因为此字符已经在那里等待提取。
因此,一种解决方案是在执行get
之前用ignore
清除输入流:
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
这将提取并丢弃流中下一个n
之前的任何内容。所以如果你的输入是123hellon
,它甚至会丢弃hello
。
另一种方法是使用 std::getline
读取输入行(这也将提取n
),然后解析该行以获取输入数字。
因为,它读取最后一个字符,写两次,糟糕的解决方案,所以:
cin.get();
cin.get();
或只是尝试
系统("暂停");
这将释放您的屏幕
相关文章:
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 使用 cin.get() 初始化字符数组
- 编写 cin.get() 以接收字符数组时出现问题
- 使用 cin.get 读取字符数组
- cin.get()的返回类型
- 在循环中使用 cin.get() 输入字符串
- 为什么 cin.get(char, int) 不会溢出?
- cin.get() 导致"no instance of overloaded function"错误
- 为什么当我输入字符时,这个特定的 cin.get() 语句在C++中不起作用?
- 为什么程序在 cin 之后跳过 cin.get()?
- 如何终止 std::cin.get() 的 ''
- 使用 cin.get() 读取字符
- 无法让我了解 cin.get 和 cout 在这里的工作方式
- cin.get()函数在C 中
- 如何在 c++ 中使用 cin.get() 给出更多数字?
- (m = cin.get())!=EOF&m = cin.get()!=EOF 之间的差异
- 为什么我的应用程序即使我的应用程序也关闭,即使我有cin.get();
- C 使用CIN.Get()暂停此程序
- cin.get() 之后的回车;C++
- 我是否缺少 cin.get() 的东西