---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!
Program: C:\Windows\SYSTEM32\MSVCP140D.dll
File: d:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring
Line: 1682
Expression: string subscript out of range
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
中止(A) 重试(R) 忽略(I)
---------------------------
//for ( index = 0; index != sizeof(str1); ++index)
//sizeof是查看字节的,写法错误。
for ( index = 0; index != str1.size(); ++index)
{
if (ispunct(str1[index]))
str1_sum += 1;
}
#include <iostream>
#include <string>
int main()
{
using namespace std;
string str1;
int str1_sum(0);
cout << "请输入一个字符串,进行统计与判断" << endl;
getline(cin,str1);
cout << "输入的字符串是:" << endl;
cout << str1 << endl;
string::size_type index;
//for ( index = 0; index != sizeof(str1); ++index)
//sizeof是查看字节的,写法错误。
for ( index = 0; index != str1.size(); ++index)
{
if (ispunct(str1[index]))
str1_sum += 1;
}
cout << "index最后的值是: " << index <<". 它的字节是:"<<sizeof(index)<< endl;
if (!str1_sum)
cout << str1 << "里面没有标点符号" << endl;
else
cout << str1 << "中包含有 " << str1_sum << " 个标点符号" << endl;
getchar();
return 0;
}