【发布时间】:2018-02-01 18:50:42
【问题描述】:
我正在使用 Stanley B.Lippman 的 C++primer 一书,此错误是由 Excersise 3.2.3 test 3.10 的解决方案引起的。它需要编写一个程序来读取包括标点符号在内的字符串并写入阅读的内容,但删除了标点符号。
代码如下:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s;
cout << "Please input a string of characters including punctuation:" << endl;
getline(cin, s);
for (auto c : s) {
if (!ispunct(c))
cout << c;
}
cout << endl;
return 0;
}
当我在 Visual Studio 2017 中运行此代码时,它会显示:
Debug Assertion failed.
Expression:c>=-1&&c<=255
For information on how your program can cause an assertion failure,see the Visual C++ documentation on asserts.
为什么会这样显示?看不懂。
【问题讨论】:
-
它的断言在哪一行? (添加评论向我们展示),另外,您的意见是什么?
-
您的字符中可能有一个不是 ASCII 码?
-
向我们展示您的意见!
-
试试
for (unsigned char c : s) -
@manni66 好点 - 更多解释为什么 - 在这里:en.cppreference.com/w/cpp/string/byte/ispunct