-
Long CHAR
I'm having a slight problem with a program I'm working on : I need the user to be able to input a large number of characters ( this is why CHAR is not suitable, it limits characters to 256 ) and I want the user to be able to use new lines. I tried something like this :
Code:
char data[256];
...
cin.getline(data, 256, '*');
but that limits it to 256, so I tried this :
Code:
wchar_t data[50000];
...
cin.getline(data, 50000, '*');
However, the getline command doesn't seem to work with wchar_t variables. I haven't worked much with strings, will they work, and can they take multiple lines ?
-
"char" limits your string to 256 *different* characters. Unless you plan to display things like japanese text, etc, you can use strings of chars. It places no limits on the length of the string; as long as you have memory, you could in theory have 4 billion characters before you have a problem. The 256-character limit means you can only use ASCII characters, you can't use Unicode characters, which are 16-bits wide.
wchar_t is used if you're using Unicode (16 bit characters instead of 8-bit). To use it, you need to use all Unicode functions. If you're not compiling for Unicode, don't use it.
It's perfectly acceptable to do "char c[50000];" if you needed 50,000 characters. getline only returns one line of data, but there are other types of reads which will return multiple lines; strings can have multiple lines in them if you want them to.
-
For wide character streams, use std::wcin, std::wcout, sdt::wcerr, wclog. They're all the same as their normal equivalents. They're also in the same library: <iostream>.
Code:
#include <iostream>
using namespace std;
int main()
{
wchar_t str[64];
wcin.getline(str, 64);
wcout << L"You entered: " << str << endl;
return 0;
}
-
Alright, thank you, now my problem is as follows :
Code:
fout << (char)(data^key[count++]);
Returns :
Code:
invalid operands `char[50000]' and `char' to binary `operator ^'
How do I solve this ? Thanks.
-
yes i have the same question
-
Quote:
Originally posted by Korhedron
Alright, thank you, now my problem is as follows :
Code:
fout << (char)(data^key[count++]);
Returns :
Code:
invalid operands `char[50000]' and `char' to binary `operator ^'
How do I solve this ? Thanks.
Well, what is that line of code supposed to do? Without knowing that, it's hard to say.
-
It seems like you're trying to do a binary XOR between a single char and a char[5000] which is wrong. Or did you mistake it for the power-operator? But raising a char to the power of a string still seems kinda weird :).
-
Hehehehe, no, I didn't mistake it for the power operator.
Char KEY is a coding key, a long line of complicated characters, and Char data[50000] is a long line of data I want to be able to type in. Basically, I want the user to type in some stuff, including being able to miss lines, whatever, and I want the program to write to a file what the user gave in, but encrypted through XOR.
-
Show some more code then, especially the definitions of Key and Data and the loop. I tried this and that works:
Code:
char Key = 45;
char Buffer[SIZE] = "Hello";
for(int i=0; i<strlen(Buffer); i++)
{
cout << (char)(Key ^ Buffer[i]);
}
-
Code:
char filename[256];
char data[50000];
int count = 0;
char key[52] = "&^%5/{?>:]!@A1c%~<,.;yX+_|`':8$^M6h #6Hmd 5/-+?,;:'";
...
void createcrypt()
{
clrscr();
t("Enter the name of the file you wish to create.\nInclude the extension [EG. .doc, .txt]\n");
cin.getline(filename, 256, '\n');
t("\n\nEnter your data. End it wish a \"*\" ( without quotes ) followed by [ENTER].\n");
cin.getline(data, 50000, '*');
clrscr();
t("Preparing to encrypt your data.\n");
t("Creating empty file : ");
t(filename);
done();
fout.open(filename, ios::binary | ios::out);
fout << (char)(data^key[count++]);
if(count == strlen(key))
{
count = 0;
}
fout.close();
}
Void t is simply like COUT, but using typewriter effect.
-
Code:
char data[50000];
char key[52] = "&^%5/{?>:]!@A1c%~<,.;yX+_|`':8$^M6h #6Hmd 5/-+?,;:'";
fout << (char)(data^key[count++]);
As mentioned above, you do bitwise XOR on a char and a char[50000] which is plain wrong. You must use the same datatype on both sides.
If you want to do it on every charcater in Data, create a loop.