[英]While Loop will not Execute in C
當我運行該程序時,除了while循環塊的末尾之外,其他所有內容都將執行。 我被問到“每行要打印的符號數”,然后程序結束。 任何幫助將不勝感激。
#include <stdio.h>
int main(void) {
int num_lines;
printf("Enter a number of lines, greater than or equal to 7, to print : ");
scanf("%d", &num_lines);
if (num_lines < 7) {
while ( num_lines < 7 ) {
printf("Enter the number of lines to print\nMust be greater than or equal to 7 : ");
scanf("%d", &num_lines);
}
}
char symbol;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf("%s", &symbol);
int num_symbols;
printf("Enter the number of symbols to print per line : ");
scanf("%d", &num_symbols);
if (num_symbols < 7 || num_symbols > 27) {
num_symbols = 19;
}
while (num_lines > 0) {
while (num_symbols > 0) {
printf("%s", symbol);
--num_symbols;
}
printf("\n");
--num_lines;
}
return;
}
您的代碼有一個非常嚴重的錯誤,
char symbol;
scanf("%s", &symbol);
是未定義的行為,因為"%s"
期望指向至少大小為2的char
數組的指針。在您的情況下,您正在將指針傳遞給單個char
,此類代碼的作用是不確定的。
相反,它可能是( 至少 )
char symbol[2];
scanf("%1s", symbol);
或者,請參閱@ BLUEPIXY的建議。
調用未定義的行為后 ,程序的內存可能會損壞,因此程序的其余部分可能由於多種原因而失敗。
您也不要檢查scanf("%d", ...)
是否成功可能是導致未定義行為的另一原因。
始終檢查scanf()
返回正確的值。
在此代碼段中
char symbol;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf("%s", &symbol);
使用了無效的格式說明符%s
。 而是使用格式說明符" %c"
char symbol;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf(" %c", &symbol);
同樣在printf的調用中
printf("%s", symbol);
您必須使用格式說明符%c
printf("%c", symbol);
提供這些循環將正常工作
while (num_lines > 0) {
while (num_symbols > 0) {
printf("%s", symbol);
--num_symbols;
}
printf("\n");
--num_lines;
}
您需要使用一個中間變量來存儲外循環迭代之間的num_symbols
值。 例如
while (num_lines > 0) {
int n = num_symbols;
while (n > 0) {
printf("%c", symbol);
--n;
}
printf("\n");
--num_lines;
}
讓我們將問題歸結為它的核心:您正在覆蓋調用堆棧上的num_lines,因為您在真正想要一個字符時要求讀取字符串。 當scanf創建字符串時,它將以null終止...,因此在堆棧上的單個char之后添加一個null字符 。 在C語言中使用指針的樂趣之一。
(這不是堆棧溢出,而只是堆棧覆蓋。)
這是失敗的最小示例。 只需將%s
更改為%c
,一切都會起作用:
#include <stdio.h>
int main(void) {
int num_lines;
sscanf("7", "%d", &num_lines);
printf("at first, num_lines is %d\n", num_lines);
char symbol;
sscanf("x", "%s", &symbol);
printf("but now, num_lines has become %d\n", num_lines);
printf("value changed because the second sscanf overwrote\n");
printf(" num_lines on the call stack!!!\n");
return;
}
您會發現我很懶。 在測試過程中使用sscanf
可以減少煩人的輸入。
運行它,您會得到:
$ gcc scan.c ; ./a.out
at first, num_lines is 7
but now, num_lines has become 0
value changed because the second sscanf overwrote
num_lines on the call stack!!!
您的問題是char變量symbol
。 您將symbol
聲明為字符變量,並將其讀取為字符串。 還有一個問題。 使用scanf
同時讀取字符串或字符和數字時,請始終在讀取數字值之前讀取字符串或char值。 原因是scanf
還會再讀取一個字符\\n
,這總是會造成問題。
現在更改您的代碼,如下所示:-
#include <stdio.h>
int main(void) {
int num_lines;
char symbol;
printf("Choose a symbol/character to be displayed */&/+/0/x : ");
scanf("%c", &symbol);
printf("Enter a number of lines, greater than or equal to 7, to print : ");
scanf("%d", &num_lines);
if (num_lines < 7) {
while ( num_lines < 7 ) {
printf("Enter the number of lines to print\nMust be greater than or equal to 7 : ");
scanf("%d", &num_lines);
}
}
int num_symbols;
printf("Enter the number of symbols to print per line : ");
scanf("%d", &num_symbols);
if (num_symbols < 7 || num_symbols > 27) {
num_symbols = 19;
}
while (num_lines > 0) {
while (num_symbols > 0) {
printf("%s", symbol);
--num_symbols;
}
printf("\n");
--num_lines;
}
return 0;//you have to return a value
}
如果仍然給您帶來任何問題,只需硬編碼symbol = '*';
並嘗試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.