简体   繁体   中英

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. 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); , for scanning variables you should send their address to scnaf , so this should be scanf("%d", &msglength); (add & to 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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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