简体   繁体   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

When I call "encript()", it works how I want it to until the second last "printf()" at which point the program terminates without executing any more lines.当我调用“encript()”时,它会按照我想要的方式工作,直到倒数第二个“printf()”,此时程序终止而不执行任何更多行。 I tried searching for similar questions here and tried to fix my code but nothing that I could find seemed to work.我尝试在这里搜索类似的问题并尝试修复我的代码,但我找不到的任何东西似乎都不起作用。 I am a beginner so please forgive me if it's just a simple mistake.我是初学者,所以如果这只是一个简单的错误,请原谅我。

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);
}

Thanks!谢谢!

pay attention to this line scanf("%d", msglength);注意这一行scanf("%d", msglength); , for scanning variables you should send their address to scnaf , so this should be scanf("%d", &msglength); ,对于扫描变量,您应该将它们的地址发送到scnaf ,所以这应该是scanf("%d", &msglength); (add & to scanf ) (添加&scanf

this function should return a character,if you just want to to scan a message and print it, you should use void like this void encript(int x) also if you want to return message you can use char * encript(int x) .这个 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