在尝试学习C ++时,我在Visual Studio上遇到本地调试器的问题,一旦到达代码末尾,它将立即关闭,并且不允许我自己关闭它,因此我可以甚至看不到输出的最后一位。
据我所知,我的代码中没有错误,我尝试了getchar()
,以便可以在调试器关闭之前输入一个字符。
这是我的代码,以防万一有错误,但我不认为有
#include <iostream>
using namespace std;
int search(char* pchs, int size, char key) {
int count = 0;
for (int i = 0; i < size; i++) {
if (pchs[i] == key)
count++;
}
return count;
}
int main() {
int size = 0;
char key = 0;
cout << "Please enter the size of the array" << endl;
cin >> size;
cout << "Please enter the key (a-z)" << endl;
cin >> key;
char* pchs = new char[size];
if (pchs != NULL) {
for (int i = 0; i < size; i++) {
pchs[i] = 97 + rand() % 26;
cout << pchs[i] << " ";
}
cout << endl;
cout << "No. Occurences: " << search(pchs, size, key) << endl;
}
delete[] pchs;
return 0;
}