473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Output problem

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
15 1804
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
OK, but now it doesn't show the string "decrypted".

Nov 11 '05 #3

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
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
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
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
OK, but now it doesn't show the string "decrypted".

Nov 11 '05 #8
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
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
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
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
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

"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

"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jay Chan | last post by:
I am trying to use a command line program to run a stored procedure that generates output in a comma-delimitted format. Somehow, ISQL or OSQL always wrap the lines at 256 characters. I believe this...
1
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records....
11
by: Etienne Charland | last post by:
Hi, I have a solution containing 6 C# projects; 1 WinForms project and 5 class libraries. I didn't have any problems until recently. I added a new project containing reports. I am using...
3
by: Dalan | last post by:
Perhaps someone has experienced this problem. I developed an Access 97 Runtime database on a Windows 98 SE machine. I have three reports that can be optionally copied to a floppy disk or hard drive...
5
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned...
4
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG:...
5
by: Brad | last post by:
I created a base page class which sets a response filter and the filter injects additional html into the response output stream. The filter works fine and everything works as expected except for...
6
by: Alec MacLean | last post by:
Hi, I've created a small application for our company extranet (staff bulletins) that outputs a list of links to PDF's that are stored in a SQL table. The user clicks a link and the PDF is...
4
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using XslCompiledTransform to do the trnasform (namely the output...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.