473,413 Members | 1,854 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,413 software developers and data experts.

cout won't print string

I'm trying to print a string, and cout isn't printing it; here's my
code:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;

namespace
{
void convert(string& String)
{
for(int i=0;i<String.length();i++)
String[i]=(toupper(String[i]));
return;
}

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

string decrypt(const string& ciphertext,const string& key)
{
string decrypted;
for(int i=0;i<ciphertext.length();i++)
decrypted[i]+=(ciphertext[i]-key[i%key.length()])%26;
return decrypted;
}
}

int main()
{
string cleartext;
string key;
cout << "Enter cleartext: ";
getline(cin,cleartext,'\n');
cout << "Enter key: ";
getline(cin,key,'\n');
convert(cleartext);
convert(key);
string ciphertext=encrypt(cleartext,key);
cout << "Ciphertext: " << ciphertext << endl;
cout << "Cleartext: " << decrypt(ciphertext,key) << endl;
system("PAUSE");
return 0;
}

Any ideas as to what might be wrong? Thanks!!!

Oct 2 '05 #1
6 4980
* Protoman:
I'm trying to print a string, and cout isn't printing it; here's my
code:

#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
Add

#include <stdexcept>

using namespace std;

namespace
{
void convert(string& String)
{
for(int i=0;i<String.length();i++)
String[i]=(toupper(String[i]));
return;
}

string encrypt(const string& cleartext,const string& key)
{
string encrypted;
for(int i=0;i<cleartext.length();i++)
encrypted[i]+=(cleartext[i]+key[i%key.length()])%26;
If you consistently replace "[i]" with ".at(i)" you'll find some errors.

return encrypted;
}

string decrypt(const string& ciphertext,const string& key)
{
string decrypted;
for(int i=0;i<ciphertext.length();i++)
decrypted[i]+=(ciphertext[i]-key[i%key.length()])%26;
return decrypted;
}
}

int main()
{
string cleartext;
string key;
cout << "Enter cleartext: ";
getline(cin,cleartext,'\n');
cout << "Enter key: ";
getline(cin,key,'\n');
convert(cleartext);
convert(key);
string ciphertext=encrypt(cleartext,key);
cout << "Ciphertext: " << ciphertext << endl;
cout << "Cleartext: " << decrypt(ciphertext,key) << endl;
system("PAUSE");
return 0;
}

Any ideas as to what might be wrong? Thanks!!!


Change that to
int main()
{
try
{
// place here contents of main's {...} above, but
// omit the "system(PAUSE)" and
// replace "return 0;" at the end with
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 2 '05 #2
Protoman wrote:
string encrypted;
for(int i=0;i<cleartext.length();i++)
encrypted[i]+=(cleartext[i]+key[i%key.length()])%26;


It looks like you meant to write:

encrypted += (cleartext[i]+key[i%key.length()])%26;

and similarly in the decrypt function. Better yet, use a
std::ostringstream for efficiency.

Oct 2 '05 #3

Pete C wrote:
Protoman wrote:
string encrypted;
for(int i=0;i<cleartext.length();i++)
encrypted[i]+=(cleartext[i]+key[i%key.length()])%26;


It looks like you meant to write:

encrypted += (cleartext[i]+key[i%key.length()])%26;

and similarly in the decrypt function. Better yet, use a
std::ostringstream for efficiency.


OK, how do you use an ostringstream obj?

Oct 2 '05 #4
Protoman wrote:
OK, how do you use an ostringstream obj?


#include <sstream>

string decrypt(const string& ciphertext,const string& key)
{
std::ostringstream decrypted;
for(int i=0;i<ciphertext.length();i++)
decrypted.put(((ciphertext[i]-key[i%key.length()])%26));
return decrypted.str();
}
Like that. Your arithmetic needs work though, I think. Bear in mind
that subtracting one char from another can yeild a negative result, and
that your modulus will still be negative. Also, did you also mean to
use offsets from 'A'?

Oct 2 '05 #5

Pete C wrote:
Protoman wrote:
OK, how do you use an ostringstream obj?


#include <sstream>

string decrypt(const string& ciphertext,const string& key)
{
std::ostringstream decrypted;
for(int i=0;i<ciphertext.length();i++)
decrypted.put(((ciphertext[i]-key[i%key.length()])%26));
return decrypted.str();
}
Like that. Your arithmetic needs work though, I think. Bear in mind
that subtracting one char from another can yeild a negative result, and
that your modulus will still be negative. Also, did you also mean to
use offsets from 'A'?


Yes, I did.

Oct 3 '05 #6
Protoman wrote:
Pete C wrote:
Protoman wrote:
string encrypted;
for(int i=0;i<cleartext.length();i++)
encrypted[i]+=(cleartext[i]+key[i%key.length()])%26;


It looks like you meant to write:

encrypted += (cleartext[i]+key[i%key.length()])%26;

and similarly in the decrypt function. Better yet, use a
std::ostringstream for efficiency.


OK, how do you use an ostringstream obj?


Why would it make it more efficeient?

Oct 3 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: George Bell | last post by:
OK BIG newbie question here. I've been trying to write cout statements in a C++ program compiled with gcc. The problem I'm having with the cout statements is that I can' get anything to print...
3
by: Eric Lilja | last post by:
Hi, I need a function that expects a const std::string& containing the visual representation of a number in binary format. It should then output this number in groups of four bits separated by a...
10
by: Gurikar | last post by:
How to make cout not printing on the console. i mean void main() { cout<<"Hello world"<<endl; } Is there any way where i can block printing on console even when iam
16
by: mrDumbass | last post by:
#include <iostream> #include <algorithm> #include <string> #include <fstream> using namespace std; /* i would like to keep the numbers of the output : Power , step etc, on the same place...
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
3
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
10
by: laikon | last post by:
Hello, everyone: this is about overflow in C and C++. int c = 400; printf("%c", c); it print ? on screen, and ascii of '?' is 63.
5
by: wongjoekmeu | last post by:
Dear All, I have written a small program to read in from console a user string. I wanted to be able to read in a string containing of all sorts of characters untill the user press enter. I have to...
1
by: zackp | last post by:
Hello, I am learning C++ lately. This morning, out of curiosity I constructed a simple code below, but to my surprise, when char* is used, the program fails to print almost anything. The only...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.