好的,因为我最近开始阅读关于C++的文章,并尝试阅读这本书,我正在使用C++第二版进行编程原理和实践。
我完全是一个新手,所以这可能就是原因,但这里有。
好的,在这本书中,你实现了一个头.h文件,而不是(iostream)等,所以它一开始就有了所有这些,因为这本书不想让我们在学习开始时关注那些库。所以我实现并使用了它(不确定这是否与问题有关)。不管怎么说,到了77页,我被卡住了。
基本上,输入的是一个错误的值,它应该只显示-1 (或0),因为int得到一个false值,等等,卡洛斯(字母,而不是整数),所以int没有得到正确的值,所以应该工作的代码(并显示0或-1,因为它是输入的不正确的值)是这样的:
#include "std_lib_facilities.h"
using namespace std;
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";
}我是这样写的:
#include "std_lib_facilities.h"
using namespace std;
int main()
{
cout << "Please enter your first name and age" << endl;
string First_Name = "???";
int Age = -1;
cin >> First_Name >> Age;
cout << "Hello, " << First_Name << "(age" << Age << ")" << endl;
keep_window_open();
return 0;
}然而,对于我来说,使用visual c++的结果是当我例如编写22caros时崩溃。根据这本书,它应该输出Hello,22 (age -1)
但对我来说,当我输入单词Carlos作为年龄的值时,它就崩溃了。当我运行和编译它时,我没有得到一个错误或任何东西,但当我为age提供false值后,它就崩溃了。
我做错了什么?add:我知道我可以使用字符串让它工作,但是我只是对它为什么不工作感兴趣,因为我正在阅读这本书,我希望它不会有这些问题,因为它本来是可以工作的。
当我这样做时,这里有一个gif:http://imgur.com/a/ERjhz解决方案:使用system("pause");而不是keep_window_open();然而,阅读这本书时仍然很恼火,因为书中的代码并不总是有效的:(
发布于 2017-05-21 19:12:05
嗯,这不是问题,但速度太快了,我们的眼睛无法注意到。
我添加了keep_window_open()的函数定义
inline void keep_window_open()
{
cin.clear();
cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
return;
}正如你所看到的,它只是从用户输入字符,你将了解到输入流及其缓冲区,所以当你输入一个字符而不是整数时,流中有错误标记(背景),并且只使用了一个字符输入(在你的例子中,'C‘用于标记)。
我使用输入作为卡洛斯22
所以现在输入流仍然有字符'a','r','l','o','s',22所以现在keep_window_open函数的'a'被用作输入,程序结束时不等待你的输入,所以没有错误或崩溃,但是由于字符已经在keep_window_open函数的输入中,所以它非常快
发布于 2017-05-21 19:23:12
问题出在keep_window_open上。这是一个非常棘手的函数,很难做到绝对正确。您可以将其替换为以下代码,该代码可绕过该问题,但仅适用于Windows:
inline void keep_window_open()
{
system("pause");
}或者使用这个稍微复杂一点的代码,它试图考虑更多的情况:
inline void keep_window_open()
{
cout << "Please enter a character to exit\n";
if (!cin)
{
cin.clear();
cin.ignore(120, '\n');
}
char ch;
cin >> skipws >> ch;
}(这里假设您输入的行长度不超过120个字符。如果您希望120不受任意限制,请将其替换为numeric_limits<streamsize>::max()。在这种情况下,std_lib_facilities.h中的其他函数也会使用120。您可以将#include <limits>指令添加到程序中,也可以不添加。
我已经在几个案例中进行了测试,它似乎对程序的正确输入和错误输入都有效,但使用它的风险自负。如果您的程序需要更复杂的输入,它仍然可能正常工作,也可能无法正常工作。
发布于 2017-05-21 17:14:46
一个好的规则是,除了终端用户是笨蛋之外,你需要以一种可以处理所有类型的输入而不会崩溃的方式来编写程序。因此,始终将输入数字读取为std::string,然后尝试将其转换为int、short、double、unsigned int或您正在使用的任何数据类型。
您的代码应该如下所示:
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
int main(int argc, char **argv)
{
int age;
std::string first_name;
std::string input_age;
std::cout << "Please enter your first name and age: " << std::endl;
std::cin >> first_name >> input_age; // Read the age as 'std::string'
try
{
age = std::stoi(input_age); // Try to convert age from 'std::string' to 'int'
std::cout << "Hello, " << first_name << " (" << age << ")" << std::endl;
}
catch(std::invalid_argument& ex) // 'std::stoi()' will throw 'std::invalid_argument' if it's not able to convert 'std::string' to 'int'
{
std::cerr << "Unable to convert input value ('" << input_age << "') to 'int'" << std::endl;
std::cerr << "Debug info: " << ex.what() << std::endl;
}
catch(std::out_of_range& ex) // 'std::stoi()' will throw 'std::out_of_range' if 'std::string' is too small or too big to store in a 'int'
{
std::cerr << "The input value (" << input_age << ") is out of range, please input a value inside the range" << std::endl;
std::cerr << "Debug info: " << ex.what() << std::endl;
}
return EXIT_SUCCESS;
}https://stackoverflow.com/questions/44094545
复制相似问题