[英]Having a problem with a function in my C program. Program terminates before executing the second last “printf()” in the “encript()” function
當我調用“encript()”時,它會按照我想要的方式工作,直到倒數第二個“printf()”,此時程序終止而不執行任何更多行。 我嘗試在這里搜索類似的問題並嘗試修復我的代碼,但我找不到的任何東西似乎都不起作用。 我是初學者,所以如果這只是一個簡單的錯誤,請原諒我。
char encript(int x){
printf("You selected Encription with the key: %d\n\n", x); //Tells user the key value they input
Sleep(1000);
int msglength = 100; //Variable for the amount of characters in the messge
printf("\n\nEnter aproximate number of characters in you message. \nThe number must be at least 1 over the amount of characters in your message: ");
scanf("%d", msglength); //assigns user input integer to msglength
printf("\n\n");
char message[msglength]; //Creates a character array for the message with a length of "msglength"
printf("Insert message here: "); ////THIS LINE DOES NOT SEEM TO EXECUTE. NOR DO THE TWO BELOW IT.
scanf("%s", message);
printf("\n\n%s", message);
}
謝謝!
注意這一行scanf("%d", msglength);
,對於掃描變量,您應該將它們的地址發送到scnaf
,所以這應該是scanf("%d", &msglength);
(添加&
到scanf
)
這個 function 應該return
一個字符,如果你只是想scan
一條消息並print
它,你應該像這樣使用void
void encript(int x)
如果你想返回消息,你可以使用char * encript(int x)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.