Not necessarily. If you did that twice in a row you'd have
Code:
while(getchar() != '\n') {}
scanf("%c", &option2);
while(getchar() != '\n') {}
while(getchar() != '\n') {}
scanf("%c", &option2);
while(getchar() != '\n') {}
which would cause the program to ignore every other line of input.
Just putting the character-eating code right after every scanf() call will probably do what you want. I've modified your original code to be this way, though I'm not sure if it works because I'm not certain what it's supposed to do.
Code:
#include <stdio.h>
#include <stdlib.h>
void strip_characters(void);
void encrypt(int number1);
void decrypt(int number2);
int main(void)
{
int number;
char option, option2;
printf("Encode or Decode? (e/d)");
scanf("%c", &option);
strip_characters();
if(option == 'e'){
printf("Enter four digit number: ");
scanf("%d", &number);
strip_characters();
encrypt(number);
}
else if(option == 'd'){
printf("Enter four digit number: ");
scanf("%d", &number);
strip_characters();
decrypt(number);
}
else{
printf("please choose an option.");
}
printf("\nContinue? (y/n) ");
scanf("%c", &option2);
strip_characters();
if(option2 == 'y'){
do{
printf("Encode or Decode? (e/d)");
scanf("%c", &option);
strip_characters();
if(option == 'e'){
printf("Enter four digit number: ");
scanf("%d", &number);
strip_characters();
encrypt(number);
}
else if(option == 'd'){
printf("Enter four digit number: ");
scanf("%d", &number);
strip_characters();
decrypt(number);
}
else{
printf("please choose an option.");
}
printf("\nContinue? (y/n) ");
scanf("%c", &option2);
strip_characters();
}while(option2 != 'n');
}
else{
printf("Please remember to secure your password before exiting.\n");
}
system("pause");
return 0;
}
void strip_characters(void) {
int disregard;
do {
disregard = getchar();
} while(disregard != '\n' && disregard != EOF);
}
void encrypt(int number1)
{
int i;
int j = 0;
int temp;
int code[4];
int a, b, c, d, m, n, o, p;
for(i = 1000; i >= 1; i /= 10){
temp = number1 % i;
code[j] = (number1 - temp) / i;
number1 = temp;
j++;
}
m = code[0];
n = code[1];
o = code[2];
p = code[3];
m += 7, n += 7, o += 7, p += 7; /*add 7 to digit*/
m %= 10, n %= 10, o %= 10, p %= 10; /*replace digit with remainder of 10*/
a = m * o/m; /*swap first digit with third*/
c = o * m/o; /*swap third digit with first*/
b = n * p/n; /*swap second digit with fourth*/
d = p * n/p; /*swap second digit with fourth*/
printf("Encrypted number is: %d%d%d%d", a, b, c, d); /*print new number*/
}
void decrypt(int number2)
{
int t = 0;
int s;
int temp;
int code[4];
int a, b, c, d, i, j, k, l;
for(s = 1000; s >= 1; s /= 10){
temp = number2 % s;
code[t] = (number2 - temp) / s;
number2 = temp;
t++;
}
i = code[0];
j = code[1];
k = code[2];
l = code[3];
a = i * k/i; /*swap first digit with third*/
c = k * i/k; /*swap third digit with first*/
b = j * l/j; /*swap second digit with fourth*/
d = l * j/l; /*swap second digit with fourth*/
a += 3, b += 3, c += 3, d += 3; /*simplified - remainder of ten, then minus seven*/
printf("Decrypted number is: %d%d%d%d", a, b, c, d); /*print new number*/
}
It's a bit of a hassle, isn't it? The alternative is to read input in line by line with fgets(), and then use sscanf() to parse what you want from the line. That way you never have to discard characters from the input.