繁体   English   中英

我的 C 程序中的 function 出现问题。 程序在执行“encript()”function 中的倒数第二个“printf()”之前终止

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM