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