这是我的代码:
int main ()
{
const int MAX = 10;
char *buffer = nullptr; // used to get input to initialize numEntries
int ARRAY_SIZE = 20; // default is 20 entries
int numEntries;
success = false;
while (!success)
{
delete buffer;
buffer = nullptr;
buffer = new char [MAX];
cout << "How many data entries:\t";
cin.getline(buffer, MAX, '\n');
cout << endl << endl;
while (*buffer)
{
if (isdigit(*buffer++))
success = true;
else
{
success = false;
break;
}
}
}
numEntries = atoi(buffer);
问题是当我输入一个任意数字时,它只显示“numEntries = 0”,如果我输入一个字符串,它就会崩溃。
有人可以解释到底发生了什么吗?