我有這個簡單的程序,我想測試自己,它基本上從終端字符讀取並計算輸入了多少個小寫字符。C被困在無限循環中
在我調用的函數中,它看起來只是在循環中永遠運行。
/*
Create a function that reads the number of lower case letters from the user
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void CopyMe(char []);
main()
{
char letters [100];
int i = 0;
do
{
letters [i++] = getchar();
}
while (letters [i-1] != '\n');
letters [i-1] = '\0';
CopyMe(letters);
printf("Done \n");
system("pause");
return 0;
}
void CopyMe(char a[])
{
int lowercase = 0;
int i=0;
while (a[i] != '\0')
{
if (a[i] >= 'a' && a[i] <= 'z')
{
lowercase++;
}
}
printf("Lowercase: %d \n", lowercase);
}
使用調試器分步循環。原因應該很快變得清晰。 –
你不能用'for(i = 0; i
ugoren
無論何時,一個循環似乎「永遠運行」,你可能會很好地檢查循環的條件,並確定循環體內的任何東西是否會改變它。 – WhozCraig