Having some issues with a basic program I am messing around with.
The intent of this program is just that it uses a multiplier of 2 as a key for shifting a number that a user enters, eventually it will accept character input, followed by whole files. First off I am just learning how to use function prototypes and branching/looping within my programs.
My problem is simple, but I can't seem to solve it. When the first iteration of my loop completes, it wouldn't ask for additional user input, so then I inserted a second print function with my defined string. What I wanted would then print out, but instead of one time, it printed out twice. I figure it has something to do with my while loop or that I just put things out of order.
Sample:
"Here it is encrypted: 10, and decrypted: 20.00
Enter 'E' to encrypt, 'D' to decrypt
Enter 'E' to encrypt, 'D' to decrypt"
If someone could provide feedback as to why it is double printing, would greatly appreciate that.
The intent of this program is just that it uses a multiplier of 2 as a key for shifting a number that a user enters, eventually it will accept character input, followed by whole files. First off I am just learning how to use function prototypes and branching/looping within my programs.
My problem is simple, but I can't seem to solve it. When the first iteration of my loop completes, it wouldn't ask for additional user input, so then I inserted a second print function with my defined string. What I wanted would then print out, but instead of one time, it printed out twice. I figure it has something to do with my while loop or that I just put things out of order.
Sample:
"Here it is encrypted: 10, and decrypted: 20.00
Enter 'E' to encrypt, 'D' to decrypt
Enter 'E' to encrypt, 'D' to decrypt"
Code:
/*Preprocessor area, has strings that I don't want to
individually type in, and it also my two functions
which break out the work*/
#include <stdio.h>
#define CHOICE "Enter 'E' to encrypt, 'D' to decrypt"
#define EN "Enter integer for encrypt"
#define DE "Enter integer for decrypt"
float encryp1 (int);
float decryp1 (int);
int main(void)
{
int a;
char b;
printf ("%s\n", CHOICE); //This is the first instance
while (scanf ("%c", &b) == 'E' | 'D' | 1){
if (b == 'E'){
printf ("%s\n", EN);
scanf ("%d", &a);
printf ("Here it is before: %d, and after: %.2f\n",
a, encryp1(a));
}
else if (b == 'D'){
printf ("%s\n", DE);
scanf ("%d", &a);
printf ("Here it is encrypted: %d, and decrypted: %.2f\n",
a, decryp1(a));
}
printf ("%s\n", CHOICE); //This won't show-up unless I type it
//here, but then it prints twice
}
return 0;
}
//Encryption fuction
float encryp1 (int a)
{
int b;
b = a / 2;
return b;
}
//Decryption function
float decryp1 (int a)
{
int b;
b = a * 2;
return b;
}
If someone could provide feedback as to why it is double printing, would greatly appreciate that.