while() 循环让程序崩溃,但没有语法错误或其他任何问题
while() loop lets program crash, but there are no syntax errors or anything else
我需要编写一个程序,让用户输入一个字符串,然后继续使用 islower
、isupper
、isdigit
检查单个字符,但是在字符串的第一个循环它总是崩溃并在第二个回合后一次给出 printfs
。
有人可以帮忙吗?
#include <stdio.h>
#include <string.h>
#ifndef MAIN_H_
#include "main.h"
#endif
int main()
{
char cString[MAX]; //alle Felder auf NULL gesetzt für spätere Überprüfung ob Entertaste gedrückt wurde
char cUebergabe[MAX];
int iLauf=0;
int iNULL=0;
while(iLauf<MAX)
{
cString[iLauf]=iNULL;
iLauf++;
}
iLauf=0;
printf("geben sie maximal 20 Zahlen zur auswertung des String ein, bei der Eingabe von Enter ohne ein Symbol wird das Einlesen abgebrochen\n");
while(iLauf<MAX)
{
printf("Geben Sie nun das %d . Zeichen ein\n",iLauf+1);
scanf("%c",&cString[iLauf]);
iLauf++;
if(cString[iLauf-1]==iNULL)
{
iLauf=MAX;
}
else
;
}
strcpy(cUebergabe,cString);
VerarbeitungAusgabe(cUebergabe, iNULL);
return 0;
}
问题是 %c
格式说明符禁止跳过前导白色 space 字符。并且新行(当用户按下 Enter 时)在读取数字后的下一次迭代中被读取。
来自手册页:
Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating null byte is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the for‐ mat.
所以只需在前面添加一个 space:
scanf(" %c", &cString[iLauf]);
另请注意,您可以使用 memset
轻松地用零填充内存。所以你可以替换这个:
while (iLauf < MAX) {
cString[iLauf] = iNULL;
iLauf++;
}
iLauf = 0;
有了这个:
memset(cString, 0, sizeof(cString));
甚至这样:
char cString[MAX] = {0};
我需要编写一个程序,让用户输入一个字符串,然后继续使用 islower
、isupper
、isdigit
检查单个字符,但是在字符串的第一个循环它总是崩溃并在第二个回合后一次给出 printfs
。
有人可以帮忙吗?
#include <stdio.h>
#include <string.h>
#ifndef MAIN_H_
#include "main.h"
#endif
int main()
{
char cString[MAX]; //alle Felder auf NULL gesetzt für spätere Überprüfung ob Entertaste gedrückt wurde
char cUebergabe[MAX];
int iLauf=0;
int iNULL=0;
while(iLauf<MAX)
{
cString[iLauf]=iNULL;
iLauf++;
}
iLauf=0;
printf("geben sie maximal 20 Zahlen zur auswertung des String ein, bei der Eingabe von Enter ohne ein Symbol wird das Einlesen abgebrochen\n");
while(iLauf<MAX)
{
printf("Geben Sie nun das %d . Zeichen ein\n",iLauf+1);
scanf("%c",&cString[iLauf]);
iLauf++;
if(cString[iLauf-1]==iNULL)
{
iLauf=MAX;
}
else
;
}
strcpy(cUebergabe,cString);
VerarbeitungAusgabe(cUebergabe, iNULL);
return 0;
}
问题是 %c
格式说明符禁止跳过前导白色 space 字符。并且新行(当用户按下 Enter 时)在读取数字后的下一次迭代中被读取。
来自手册页:
Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating null byte is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the for‐ mat.
所以只需在前面添加一个 space:
scanf(" %c", &cString[iLauf]);
另请注意,您可以使用 memset
轻松地用零填充内存。所以你可以替换这个:
while (iLauf < MAX) {
cString[iLauf] = iNULL;
iLauf++;
}
iLauf = 0;
有了这个:
memset(cString, 0, sizeof(cString));
甚至这样:
char cString[MAX] = {0};