我有以下代码,提示用户输入仅包含数字的代码.如果用户输入两次无效代码,程序将进一步没有有效代码.
int main()
{
char code[10];
cout << "Enter the code: ";
cin >> code;
int codeLength = strlen(code);
int i = 0;
while (code[i] >= '0' && code[i] <= '9')
i++;
if (i != codeLength)
{
cout << "The code is not valid: " << codDat << endl;
cout << "Enter the code again: ";
cin >> code;
}
cout << code <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何在输入的代码仅包含数字之前,如何提示用户输入新代码?我已经尝试过了:
do {
cout << "Enter the code again: ";
cin >> code; …Run Code Online (Sandbox Code Playgroud) c++ ×1