0

I'm a begginer so I'm stuck in this part. I need to type a message, and a shift amount by which letters should be shifted to 'encrypt' a message. The problem is that It's not displaying any text, it just never exits the while loop.

Any help would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

   char ch,message[50]={0};
   int shift;


   printf("Enter message to be encrypted: ");
   scanf("%s",message);
   printf("Enter shift amount (1-25): ");
   scanf("%d",&shift);

   printf("Encrypted message: ");

   while((sscanf(message," %c",&ch) == 1) && (ch != '\n'));
   {
       ch += shift;
       putchar(ch);
   }

    return 0;
}

Output:

Enter message to be encrypted: abcABC
Enter shift amount (1-25): 3
Encrypted message: 

(Program is stucked there in an infite loop)

6
  • 2
    You have ";" in the end of the while statement Commented Sep 25, 2016 at 4:28
  • Also take a look at cplusplus.com/reference/cstdio/sscanf you are always reading only the first character form the message Commented Sep 25, 2016 at 4:29
  • But since your message has a defined size I would suggest to use a for loop for this -> it will be much safer Commented Sep 25, 2016 at 4:33
  • when I add message++ it's giving me a warning ' lvalue required as increment operand' Commented Sep 25, 2016 at 4:34
  • 2
    See Using sscanf() in loops. Commented Sep 25, 2016 at 4:35

2 Answers 2

1

There is couple of things with your code a) you have ";" at the end of your while b) you always reading only the first character inside the sscanf thats why you are in the infinite loop. I would suggest to replace while with the for loop like shown below.

scanf automagically appends'\0' to the end of the string.

int i;
for(i=0 ; i<50 && mesage[i] != '\0'; i++) 
{ 
    ch = message[i]; 
    ch += shift; 
    putchar(ch); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm getting the encrypted message but with lots of characters after it.
1

You are wrongly using sscanf. It will always read only the first character.

Try this to achieve what you want.

int i=0;
while(i<strlen(message))
{
    ch=message[i++];
    ch += shift;
    putchar(ch);
}

6 Comments

I might be mistaken, but is there any reason why you can use 'fscanf' to go through each character on a file but not 'sscanf', which is suppose to do the same thing but for a string?
@tadm123 fsacnf is sued to work with FILES see reference cplusplus.com/reference/cstdio/fscanf .
I know, that's what I'm saying @user3351949. I thought that using sscanf for a string was equivalent to using fscanf for a file.
sscanf is basically used to retrieve information from a string in a specific format. See use of sscanf here : cplusplus.com/reference/cstdio/sscanf
@tadm123 Well it is not, when reading from file, it has a pointer to the place you are reading/writing (don't remember it's name it) and it increments that pointer after you are reading. for string it doesn't have that pointer you should manage it your self.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.