-2

I've tried to look into various sites about my problem but I still can't understand what's wrong with the code.

Instead of getting the intended written string I'm getting a random 100-long number and char combination (that starts with the inputted string followed by the delimitator) that is equal to null.

#include <stdio.h>
#include <string.h>

int main()
{
    char message[100], ans;
    int key = 3;


    printf("Enter a message:"); 
    scanf("%s", message); //keeps on going until reaches 100 random characters and numbers
    printf("Encrypt [E] or decrypt [D]?: ");
    scanf("%s", &ans);
}

I've tried various methods online but none seem to be working.

EDIT: Even if I try a simple string program does not work

#include <stdio.h>
#include <string.h>

    int main()
    {
        char message[100];


        printf("Enter a message:");
        scanf("%s", message);
        printf("Encrypted message: %s", message[100]);
    }

I'm giving the input through the dev c++ console.

EDIT2: This the message input received by the program: message

Note that the delimiter (the "\" after the word) does not actually do his job.

EDIT3: This is the whole code of the program

#include <stdio.h>
#include <string.h>

void decryptmessage(char message[], int key)
{   
    char ch;
    int i;

    for(i = 0; message[i] != '\0'; ++i){
        ch = message[i];

        if(ch >= 'a' && ch <= 'z'){
            ch = ch - key;

            if(ch < 'a'){
                ch = ch + 'z' - 'a' + 1;
            }

            message[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = ch - key;

            if(ch < 'A'){
                ch = ch + 'Z' - 'A' + 1;
            }

            message[i] = ch;
        }
    }

    printf("Decrypted message: %s", message);
}


void encryptmessage(char message[], int key)
{
        char ch;
        int i;

        for(i = 0; message[i] != '\0'; ++i){
        ch = message[i];

        if(ch >= 'a' && ch <= 'z'){
            ch = ch + key;

            if(ch > 'z'){
                ch = ch - 'z' + 'a' - 1;
            }

            message[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = ch + key;

            if(ch > 'Z'){
                ch = ch - 'Z' + 'A' - 1;
            }

            message[i] = ch;
        }
    }

    printf("Encrypted message: %s", message);
}

int main()
{
    char message[100], ans;
    int key = 3;


    printf("Enter a message:");
    scanf("%s", message);
    printf("Encrypt [E] or decrypt [D]?: ");
    scanf("%c", &ans);

    if (ans == 'E') //this has to be substituted by its numerical value
       encryptmessage(message, key);
    else if (ans == 'D') //same as line 78
       decryptmessage(message, key);
    else
       printf("Goodbye.");
    return 0;
}

Now while the message is working as intended, the char ans goes automatically to the value of 10, without letting me give it an input. I really can't tell why.

  • `ans` is only 1 `char`, too small for a result from `scanf("%s", &ans);`. – chux - Reinstate Monica Oct 01 '18 at 16:02
  • 1
    `scanf("%s", &ans);` -> `scanf(" %c", &ans);` – Jabberwocky Oct 01 '18 at 16:02
  • The problem isn't the ans (I know it should be %c but the program crashes if I do so) but the message. EDIT: Nvm it does not crash anymore. – Alexandru N. Oct 01 '18 at 16:04
  • 4
    Please make a [mcve], accompanied by sample input, output and desired output. Explain how the input is provided; stdin? File read? User input? ... – Yunnosch Oct 01 '18 at 16:04
  • 1
    AlexandruN. Respectfully "The problem isn't the ans" is not certain. – chux - Reinstate Monica Oct 01 '18 at 16:06
  • 2
    If you "know it should be `%c`..." THEN WHY DIDN'T YOU SAY SO??? And why didn't you tell us about this "crash"??? Please update your post with a COMPLETE example (one we could builld) and the specific error/problem you're having with that specific code. – paulsm4 Oct 01 '18 at 16:06
  • 2
    Your code creates no output (apart from initial prompt). How do you know what you are "getting a random 100-long number". – Yunnosch Oct 01 '18 at 16:06
  • 2
    How can you see what the string contains if there is no output? Please give a complete example... – Alexander James Pane Oct 01 '18 at 16:08
  • @AlexanderPane by debugging – Alexandru N. Oct 01 '18 at 16:09
  • In the question edit `message[100]` is not a string but a single `char` and out-of-range at that. You need `printf("Encrypted message: %s", message);` – Weather Vane Oct 01 '18 at 16:10
  • 1
    @AlexandruN. As other commets state, you should provide a code that represents your issue, you can't expect for people to run through debugging without first showing the issue! – Alexander James Pane Oct 01 '18 at 16:11
  • 1
    What input are you typing, and what do you expect to be in the `message` variable after the call to `scanf`? What do you means by "does not work"? – liberforce Oct 01 '18 at 16:14
  • Watch the edits 1 and 2 for a more detailed description of my problem. – Alexandru N. Oct 01 '18 at 16:19
  • Please, use a new question if you have other questions to avoid over-editing the initial question. – liberforce Oct 01 '18 at 16:44
  • 1
    Why did you code `scanf("%c", &ans);` versus the [suggested](https://stackoverflow.com/questions/52594863/why-does-the-scanf-not-work-with-my-string-and-instead-does-not-stop-to-the-deli#comment92124461_52594863) `scanf(" %c", &ans);`? Why did you code without the space? – chux - Reinstate Monica Oct 01 '18 at 16:45
  • @chux because it worked beforehand but mostly because I didn't notice – Alexandru N. Oct 01 '18 at 16:49
  • 1
    "char ans goes automatically to the value of 10, without letting me give it an input." --> You did provide input to get the `10`. Did you hit the enter key on the previous prompt? That is often character code 10. – chux - Reinstate Monica Oct 01 '18 at 16:53
  • Alexandru N., Rather than put answer in the question. Post the answer below as an _answer_. That is how SO works. Yet in this case, might be best to just delete the question. Review [Help Center](https://stackoverflow.com/help) and https://stackoverflow.com/help/how-to-ask – chux - Reinstate Monica Oct 01 '18 at 16:59
  • @chux I guessed I wasn't answering my own question properly, thanks for the info. – Alexandru N. Oct 01 '18 at 17:06

3 Answers3

3

You created 'ans' as a char while you expect the user to input a string, you should use %c to get a char variable from input. Edit:on your "just scan and print a string" I tried to run it, the problem is in the print part, you are printing "message[100]" which doesn't exists since the string has 0-99 placements, you just need to print "message"

IlanK
  • 122
  • 6
2

You're telling the debugger to show the content of a 100 chars buffer, and that's what it does. It shows you the 100 characters. If what you want to know is the string that is in the buffer, just stop reading after the first \000, or use printf to display the string it contains.

liberforce
  • 11,189
  • 37
  • 48
  • @Alexandru N: Regarding your "EDIT 2": - *YOU'RE LOOKING AT THE DEBUGGER*!. That's the way the debugger displays a string. It's deliberate - you often *WANT* to see *ALL* the characters in the character array. With regard to your *ORIGINAL* question - I would "accept" this answer! – paulsm4 Oct 01 '18 at 17:13
  • @paulsm4 I would accept it if the printf displayed the string in the original question, but it didn't. – Alexandru N. Oct 01 '18 at 17:30
-1

EDIT4: ALRIGHT IT WORKS PROPERLY NOW, THE ISSUE WAS THAT IS WASN'T " %c".

And for anybody asking why I didn't do that before asking the question: I did, but it still wasn't working properly for some reason.

Thank you all for the help.

  • 2
    Q: So what exactly do you mean by "it works property now"? Q: What exactly have you learned from this exercise? – paulsm4 Oct 01 '18 at 17:15
  • I've learned that I need to be EXTREMELY careful when printing chars and strings in c and to always put " %c" (with a space punctuation) to avoid scanf errors. – Alexandru N. Oct 01 '18 at 17:23