simple c/c++ programming question about encryption

J

jagivens

Hi,

I have two identical programs that encrypt characters. One is written
in C++, and it works, but the other one is written in C, and it does
not work. I have copied the code below. There is a problem somewhere
in the while loop in the C program. It goes through the code one full
time (and updates the counter) before asking for user input - thus it
updates the counter twice for every one input. Since I'm not so
familiar with C, can anyone explain this to me. The same program works
perfectly with C++, which is why I don't understand this. Any help
would be much appreciated.

Thanks,
JAG

============================== c program
====================================

#include <stdio.h> // input, output
#define BASE 33
#define RANGE 93
main(){
char originalLetter;
char encryptedLetter;
int k;
int i;
int counter = 1;

// asks the user for the number of characters to encrypt
printf("How many characters would you like to encrypt?\n");
scanf("%d", &i);
printf("What is the shifting distance (less than 93!)?\n");
scanf("%d", &k);

while (counter <= i) {
printf("Input a character: ");
scanf("%c", &originalLetter);

if(originalLetter >= '!' && originalLetter <= '~' && k < 93 && k >=
0){
encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the
calculation*/
printf("The encrypted character is %c.\n", encryptedLetter);
/*Output */
}
counter = counter + 1;

}
}

========================================c++ program
=====================

#include <iostream>
#define BASE 33
#define RANGE 93
using namespace std;

int main()
{
char originalLetter;
char encryptedLetter;
int k;
int i;
int counter = 1;

// asks the user for the number of characters to encrypt
cout << "How many characters would you like to encrypt?\n";
cin >> i;
cout << "What is the shifting distance (less than 93!)?\n";
cin >> k;

while (counter <= i) {
cout << "Input a character: \n";
cin >> originalLetter;

if(originalLetter >= '!' && originalLetter <= '~' && k < 93 && k >=
0){
encryptedLetter = BASE+(originalLetter-BASE+k)%RANGE;/*the
calculation*/
cout << "The encrypted character is " << encryptedLetter << "\n";
/*Output */
}
counter = counter + 1;
 
G

Gordon Burditt

I have two identical programs that encrypt characters. One is written
in C++, and it works, but the other one is written in C, and it does
not work. I have copied the code below. There is a problem somewhere
in the while loop in the C program. It goes through the code one full
time (and updates the counter) before asking for user input - thus it
updates the counter twice for every one input. Since I'm not so
familiar with C, can anyone explain this to me. The same program works
perfectly with C++, which is why I don't understand this. Any help
would be much appreciated.

What character do you type after you type the character in response
to the "input a character" prompt? ENTER? Newline? Newline is a
real character. Really. And scanf() treats it as one when matching
a %c. Believe it. Worship it. Now what does your program do after
it gets a newline in originalLetter?

This program would be a lot easier to use if you asked for
an input line, encrypted each character on the line, and
output the encrypted line.

Gordon L. Burditt
 
W

Walter Roberson

I have copied the code below. There is a problem somewhere
in the while loop in the C program. It goes through the code one full
time (and updates the counter) before asking for user input - thus it
updates the counter twice for every one input. Since I'm not so
familiar with C, can anyone explain this to me.
while (counter <= i) {
printf("Input a character: ");
scanf("%c", &originalLetter);

You aren't outputing a newline, and you aren't flushing the
output buffer, and you aren't setting the terminal to raw mode
or equivilent. Under the circumstances, the output from the
printf may well not show up until -after- the scanf() is executed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top