Output problem

P

Protoman

My code keeps putting the input statements on the same line, instead of
letting me input them normally; here's the code:

int main()
{
cout << "Encrypt or decrypt?[1=encrypt|0=decrypt] ";
bool select;
cin >> select;
if(select==true)
{
string cleartext;
string key;
cout << "Enter the cleartext: " << endl;
getline(cin,cleartext,'\n');
cout << "Enter the key: " << endl;
getline(cin,key,'\n');
string ciphertext(encrypt(cleartext,key));
cout << "Ciphertext: " << ciphertext << endl;
}
string ciphertext;
string key;
cout << "Enter the ciphertext: ";
getline(cin,ciphertext,'\n');
cout << "Enter the key: ";
getline(cin,key,'\n');
string decrypted(decrypt(ciphertext,key));
cout << "Cleartext: " << decrypted << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Can you help me? Thanks.
 
J

John Harrison

Protoman said:
My code keeps putting the input statements on the same line, instead of
letting me input them normally; here's the code:

int main()
{
cout << "Encrypt or decrypt?[1=encrypt|0=decrypt] ";
bool select;
cin >> select;
if(select==true)
{
string cleartext;
string key;
cout << "Enter the cleartext: " << endl;
getline(cin,cleartext,'\n');
cout << "Enter the key: " << endl;
getline(cin,key,'\n');
string ciphertext(encrypt(cleartext,key));
cout << "Ciphertext: " << ciphertext << endl;
}
string ciphertext;
string key;
cout << "Enter the ciphertext: ";
getline(cin,ciphertext,'\n');
cout << "Enter the key: ";
getline(cin,key,'\n');
string decrypted(decrypt(ciphertext,key));
cout << "Cleartext: " << decrypted << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

Can you help me? Thanks.

I'm not completely sure I follow you but I think the problem is this

cin >> select;

followed by this

getline(cin,cleartext,'\n');

(BTW you don't need to put '\n', it is the default).

Think carefully about what the two statements do. The first reads a
boolean value but does not read the newline character. Even though you
have typed a newline character (i.e. the enter key) it has not been read
yet. Then you get to the getline statement, that reads upto the next
newline. Well the next newline character is the one you typed after
entering the boolean value, which is not what you want.

The answer is to tell C++ to ignore the newline character after the
boolean value. You do that with this strange code

cin >> select;
cin.ignore(INT_MAX, '\n');
....
getline(cin,cleartext);

You need to include <limits.h> to get INT_MAX defined.

This should be a FAQ but it doesn't seem to be.

john
 
N

Neelesh

John said:
The answer is to tell C++ to ignore the newline character after the
boolean value. You do that with this strange code

cin >> select;
cin.ignore(INT_MAX, '\n');

you can also use
cin.ignore(1);
since it is only one newline character which you want to ignore.
...
getline(cin,cleartext);

You need to include <limits.h> to get INT_MAX defined.
Prefer <climits>. Still better, include <limits> and use
std::numeric_limits<int>::max();
 
J

John Harrison

Protoman said:
OK, but now it doesn't show the string "decrypted".

You mean that when you execute

cout << "Cleartext: " << decrypted << endl;

nothing appears on the screen?

I don't know why that would be.

This might be a case where it would help to post a complete program. You
could replace encrypt with a dummy function (e.g.)

string encrypt(string x, string y)
{
return x;
}

and then post all the rest of the code.

john
 
J

John Harrison

Neelesh said:
you can also use
cin.ignore(1);
since it is only one newline character which you want to ignore.

What if the user has typed a space between the boolean and the newline?
Prefer <climits>. Still better, include <limits> and use
std::numeric_limits<int>::max();

Why is that better?

john
 
P

Protoman

OK, here's the rest:

#ifndef VIGENERE_HPP
#define VIGENERE_HPP
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <string>
using namespace std;

namespace
{
const char vTable[26][27]=
{
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'},

{'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','\0'},

{'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','\0'},

{'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','\0'},

{'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','\0'},

{'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','\0'},

{'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','\0'},

{'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','\0'},

{'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','\0'},

{'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','\0'},

{'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','\0'},

{'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','\0'},

{'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','\0'},

{'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','\0'},

{'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','\0'},

{'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','\0'},

{'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','\0'},

{'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','\0'},

{'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','\0'},

{'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','\0'},

{'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','\0'},

{'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','\0'},

{'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','\0'},

{'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','\0'},

{'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','\0'},

{'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','\0'},

};

string encrypt(const string& cleartext,const string& key)
{
string encrypted;
for(int i=0;i<cleartext.length();i++)
encrypted+=vTable[cleartext-'A'][key[i%key.length()]-'A'];
return encrypted;
}
}

string decrypt(const string& ciphertext, const string& key)
{
string decrypted;
for (int i=0;i<ciphertext.length();i++)
for (char j=0;j<27;j++)
{
if (vTable[j][key[i%key.length()]-'A']==ciphertext)
{
decrypted+=static_cast<char>(j+'A');
break;
}
}
return decrypted;
}
#endif
 
P

Protoman

Here's the rest:

#ifndef VIGENERE_HPP
#define VIGENERE_HPP
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <string>
using namespace std;

namespace
{
const char vTable[26][27]=
{
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'},

{'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','\0'},

{'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','\0'},

{'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','\0'},

{'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','\0'},

{'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','\0'},

{'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','\0'},

{'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','\0'},

{'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','\0'},

{'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','\0'},

{'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','\0'},

{'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','\0'},

{'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','\0'},

{'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','\0'},

{'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','\0'},

{'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','\0'},

{'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','\0'},

{'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','\0'},

{'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','\0'},

{'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','\0'},

{'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','\0'},

{'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','\0'},

{'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','\0'},

{'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','\0'},

{'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','\0'},

{'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','\0'},

};

string encrypt(const string& cleartext,const string& key)
{
string encrypted;
for(int i=0;i<cleartext.length();i++)
encrypted+=vTable[cleartext-'A'][key[i%key.length()]-'A'];
return encrypted;
}
}

string decrypt(const string& ciphertext, const string& key)
{
string decrypted;
for (int i=0;i<ciphertext.length();i++)
for (char j=0;j<27;j++)
{
if (vTable[j][key[i%key.length()]-'A']==ciphertext)
{
decrypted+=static_cast<char>(j+'A');
break;
}
}
return decrypted;
}
#endif
 
N

Neelesh

John said:
What if the user has typed a space between the boolean and the newline?

Yes, we will need your version in that case. I was assuming that user
will simply hit return after the boolean.
Why is that better?

I am not saying that the previous one will not work. But from what I
have read at various places on net and in various books is that
<climits> or <limits> is specifically designed for usage with C++ (for
defining implementation dependent limits) - may be because it puts the
names in a namespace and doesnot pollute the global namespace. Please
correct me if I am wrong.
 
N

Neil Cerutti

you can also use
cin.ignore(1);
since it is only one newline character which you want to ignore.

Prefer <climits>. Still better, include <limits> and use
std::numeric_limits<int>::max();

Or, even more specifically:

std::numeric_limits<streamsize>::max();
 
K

Karl Heinz Buchegger

Protoman said:
OK, but now it doesn't show the string "decrypted".

Fire up your debugger, step through the code and watch
as the variables change.

That's what the I do, what all programmers around me do, probably
what most programmers around the world do.

Should be good enough for you also :)
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top