Control keeps falling through cin
Apr 27, 2014 at 5:59pm UTC
Hi there, I'm trying to write a decrypting/encrypting program. It uses a seed (uses it as a password of some sort) to generate random shifts for the ACSII values of the characters. The problem is as soon as you enter a space in the string you need to encrypt, the cin falls through when it asks for the seed (rendering it useless). I have read about the cin.ignore function but I'm unsure how to use it here. The program works perfectly if the string doesn't include a space but not otherwise. Any help would be greatly appreciated, thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void Encrypter()
{
cout << "Insert a string to encrypt: " ;
std::string cOriginal_string;
std::cin>> cOriginal_string;
int nSeed=0;
cin.clear();
cout << "Insert seed: " ;
std::string cSeed;
cin>> cSeed;
for (int nIndex=0; cSeed[nIndex]!='\0' ; nIndex++)
{
nSeed+=int (cSeed[nIndex]);
}
srand(nSeed);
std::string cString_changed=cOriginal_string;
for (int nIndex=0; cOriginal_string[nIndex]!='\0' ; nIndex++)
{
int nShift_temp=rand()%94+32;
int nASCII_number=int (cOriginal_string[nIndex]);
nASCII_number+=nShift_temp;
if (nASCII_number<127)
{
cString_changed[nIndex]=char (nASCII_number);
}
else
{
cString_changed[nIndex]=char ((nASCII_number-126)+32);
}
}
cout << endl << "The string encrypted is: [" << cString_changed << "]" << endl;
}
void Decrypter()
{
cout << "Insert a string to decrypt: " ;
std::string cOriginal_string;
std::cin>> cOriginal_string;
int nSeed=0;
cin.clear();
cout << "Insert seed: " ;
std::string cSeed;
cin>> cSeed;
for (int nIndex=0; cSeed[nIndex]!='\0' ; nIndex++)
{
nSeed+=int (cSeed[nIndex]);
}
srand(nSeed);
int nTemp_shift;
int nASCII_number;
std::string cChanged_string=cOriginal_string;
for (int nIndex=0; cOriginal_string[nIndex]!='\0' ; nIndex++)
{
nASCII_number=int (cOriginal_string[nIndex]);
nTemp_shift=rand()%94+32;
nASCII_number=nASCII_number-nTemp_shift;
if (nASCII_number>31)
{
cChanged_string[nIndex]=char (nASCII_number);
}
else
{
cChanged_string[nIndex]=char (nASCII_number+126-32);
}
}
cout << "The string decrypted is: [" << cChanged_string << "]" << endl;
}
int main()
{
cout << "This program encrypts or decrypts a string." << endl << endl;
while (true ){
cout << "Do you want to encrypt or decrypt? (E/D): " ;
char cAnswer;
cin >> cAnswer;
if (cAnswer=='e' || cAnswer=='E' )
{
Encrypter();
}
else
{
Decrypter();
}
cout << endl << "Do you want to do another encryption/decryption?(Y/N): " ;
cin>> cAnswer;
if (cAnswer=='y' || cAnswer=='Y' )
{
continue ;
}
else
{
break ;
}
cout << endl;
}
system("PAUSE" );
return 0;
}
Thanks!
Topic archived. No new replies allowed.