#include <stdafx.h>
#include <string>
#include <iostream>
打印出&#34;零&#34;对于计数,即使我在字符串中达到所需字符时特别增加计数。
using namespace std;
int main()
{
string userString = "I would like you to stop your chatting right now!";
char userChar = 'o';
int count = 0;
int i = 0;
for (i = 0; i < userString.length(); i++);
{
if (userString[i] == userChar)
{
count = count + 1;
}
}
cout << "There are a total of " << count << " " << userChar << "'s." << endl;
int rnd; cin >> rnd;
return 11;
}
答案 0 :(得分:4)
删除;在for (i = 0; i < userString.length(); i++);
你永远不会遍历你的代码
答案 1 :(得分:2)
这是正确的代码。您在for循环结束时添加了分号
#include <string>
#include <iostream>
using namespace std;
int main()
{
string userString = "I would like you to stop your chatting right now!";
char userChar = 'o';
int count = 0;
int i = 0;
for (i = 0; i < userString.length(); i++)
{
cout <<userString.length();
if (userString[i] == userChar)
{
count = count + 1;
}
}
cout << "There are a total of " << count << " " << userChar << "'s." << endl;
int rnd; cin >> rnd;
return 11;
}