By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

Output problem

P: n/a
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.

Nov 11 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Protoman wrote:
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
Nov 11 '05 #2

P: n/a
OK, but now it doesn't show the string "decrypted".

Nov 11 '05 #3

P: n/a

John Harrison wrote:
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();

Nov 11 '05 #4

P: n/a
Protoman wrote:
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
Nov 11 '05 #5

P: n/a
Neelesh wrote:
John Harrison wrote:
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.


What if the user has typed a space between the boolean and the newline?
...
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();


Why is that better?

john
Nov 11 '05 #6

P: n/a
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[i]-'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[i])
{
decrypted+=static_cast<char>(j+'A');
break;
}
}
return decrypted;
}
#endif

Nov 11 '05 #7

P: n/a
OK, but now it doesn't show the string "decrypted".

Nov 11 '05 #8

P: n/a
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[i]-'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[i])
{
decrypted+=static_cast<char>(j+'A');
break;
}
}
return decrypted;
}
#endif

Nov 11 '05 #9

P: n/a
John Harrison wrote:
Neelesh wrote:
John Harrison wrote:
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.


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.
...
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();


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.

Nov 11 '05 #10

P: n/a
On 2005-11-11, Neelesh <ne***********@gmail.com> wrote:

John Harrison wrote:
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();


Or, even more specifically:

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

--
Neil Cerutti
Nov 11 '05 #11

P: n/a
Protoman wrote:

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 :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 11 '05 #12

P: n/a
Protoman wrote:
Here's the rest:


I ran your code. It worked for me provided I remembered to enter
uppercase text.

john
Nov 11 '05 #13

P: n/a

"Neelesh" <ne***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I was assuming that user


Such an attitude is the source of uncountable bugs.

-Mike
Nov 11 '05 #14

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
OK, but now it doesn't show the string "decrypted".


Still haven't learned to quote what you're responding to?

-Howard
Nov 22 '05 #15

P: n/a

Howard wrote:
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
OK, but now it doesn't show the string "decrypted".


Still haven't learned to quote what you're responding to?

-Howard


I have, just forgot to.

Nov 22 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.