谷歌代码大学的C++教程曾经有过这样的代码:
// Description: Illustrate the use of cin to get input
// and how to recover from errors.
#include <iostream>
using namespace std;
int main()
{
int input_var = 0;
// Enter the do while loop and stay there until either
// a non-numeric is entered, or -1 is entered. Note that
// cin will accept any integer, 4, 40, 400, etc.
do {
cout << "Enter a number (-1 = quit): ";
// The following line accepts input …Run Code Online (Sandbox Code Playgroud) 来自Stroustrup的TC++ PL,第3版,第21.3.3节:
如果我们尝试读入变量v并且操作失败,则v的值应该保持不变(如果v是istream或ostream成员函数处理的类型之一,则它不会改变).
以下示例似乎与上述引用相矛盾.基于上面的引用,我期待v的值保持不变 - 但它会变为零.对这种明显的矛盾行为有什么解释?
#include <iostream>
#include <sstream>
int main( )
{
std::stringstream ss;
ss << "The quick brown fox.";
int v = 123;
std::cout << "Before: " << v << "\n";
if( ss >> v )
{
std::cout << "Strange -- was successful at reading a word into an int!\n";
}
std::cout << "After: " << v << "\n";
if( ss.rdstate() & std::stringstream::eofbit ) std::cout << "state: eofbit\n";
if( ss.rdstate() & std::stringstream::failbit ) std::cout << "state: failbit\n"; …Run Code Online (Sandbox Code Playgroud) 例如,
#include <iostream>
int main() {
unsigned n{};
std::cin >> n;
std::cout << n << ' ' << (bool)std::cin << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输入时-1,clang 6.0.0输出,0 0而gcc 7.2.0输出4294967295 1.我想知道谁是对的.或者两者都是正确的标准没有指定这个?如果失败,我认为(bool)std::cin被评估为假.clang 6.0.0也输入失败-0.
取自:cppreference
直到C++ 11:
如果提取失败(例如,如果输入了预期数字的字母),则值保持不变,并设置failbit.
从C++ 11开始:
如果提取失败,则将零写入值并设置failbit.如果提取导致值太大或太小而无法拟合值,
std::numeric_limits<T>::max()或者std::numeric_limits<T>::min()被写入并且设置了failbit标志.
由于此更改,这意味着以下代码段:
int x = 1;
std::cin >> x;
return x;
Run Code Online (Sandbox Code Playgroud)
如果数值转换失败,将1在C++ 11之前返回,0否则返回.
为什么标准委员会会引入如此微妙的突破性变化?或者更确切地说,在C++ 11之前可以使用哪种代码来保证这种变化?
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter your name and age\n";
string first_name;
int age;
cin >> first_name;
cin >> age;
cout << "Hello, " << first_name << " (age " << age << ")!\n";
}
Run Code Online (Sandbox Code Playgroud)
如果我输入Carlos对age或任何其他字符串,我收到了0.这是如何工作的(为什么)?
所以这是一个非常基本的问题和超级微不足道的但我只是通过c ++中的编程原则和实践,我的程序用于读取字符串和int的行为与Bjarne Stroustrup编写的书不同,所以如果他感到惊讶犯了一个错误.无论如何这里是代码:
#include "..\std_lib_facilities.h"
int main()
{
cout << "Please enter your first name and age\n";
string first_name = "???"; // string variable
// ("???” means “don’t know the name”)
int age = -1; // integer variable (1 means “don’t know the age”)
cin >> first_name >> age; // read a string followed by an integer
cout << "Hello, " << first_name << " (age " << age << ")\n";
}
Run Code Online (Sandbox Code Playgroud)
当我在提示符输入"22 Carlos"时它输出"Hello,22(0岁)",基本上使我的错误检查的初始化值无用.这是c ++或其他东西的新功能,这就是为什么这本书出错了?
编辑1:BTW我在Windows 7上使用GCC for cygwin,并且-std = …
我试图通过使用 char +、-、/、* 来获取和处理用户决定,为什么 switch 语句忽略它们,因此我在我的代码中看不到任何错误。
#include <iostream>
using namespace std;
void optionMenu();
double UserOutput(int);
int main ()
{
int UserChoice;
optionMenu();
cout << " Choice: ";
cin >> UserChoice;
UserOutput(UserChoice);
return 0;
}
void optionMenu()
{
cout << " Select your choice" << '\n';
cout << " + for Addition" << '\n';
cout << " - for Subtraction" << '\n';
cout << " / for Division" << '\n';
cout << " * for Multiplication" << '\n';
}
double UserOutput …Run Code Online (Sandbox Code Playgroud)