Encryption>>IO program bringing up a char error :'(

Hey, below is the code I have for reading a user inputted char, then encrypting it and writing it to a file.
I have written another program to read the file, and then unencrypt the contents, but that is not an issue at the moment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
void encrypt( char [ ] );
int main( )
{
char uinput;
// create a string to encrypt
cout << "Enter a theoretical string for func: (Game>>Enc>>File>>Game>>Dec>>Read): ";
cin.get(uinput);
cout << "Original string is: " << uinput << endl;
encrypt( uinput );
// call to the function encrypt( )
cout << "Encrypted string is: " << uinput << endl;
ofstream myfile;
myfile.open ("EncString.txt");
myfile << uinput;
myfile.close();
system("pause");
return 0;
}

//encrypt data
void encrypt (char e[] ) 
{
for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt 


Output:
line    File                                                              Message
13	W:\Resx\C++\DevC++\USB devprojects\IOencryption\EncFile\Enc.cpp	  Invalid conversion from 'char' to 'char*'
13	W:\Resx\C++\DevC++\USB devprojects\IOencryption\EncFile\Enc.cpp	  Initializing argument 1 of 'void encrypt(char*)'


admittedly I only just realised that it said 'char' to 'char*', I thought that it said 'char' to 'char', so I may now be able to find a fix through some googling, but any help would be deeply appreciated :)

Thanks a lot, superant10000.
Last edited on
Invalid conversion from 'char' to 'char*'


It wants a character pointer, and you're feeding it a character. They're two completely different things.

You can make a char pointer with the '&' operator; try

encrypt( &uinput );

You will then have the problem that uinput is a single character. I take it you want the user to be able to input more than one character?
Last edited on
Yes, that's exactly what I was looking for.
Also yeah, as I was posting, I noticed that it was a pointer not a char, that's why I said that I might be able to find a fix for it, but I got exactly what you said, single character input only :/
Do you happen to know a possible fix for this? :)
Thanks for the quick reply, superant10000 :)
Yes. Use a std::string instead of a char.
Okay, I just tried changing the following lines:

Line 5, 8 and 25 to char (tried every combination) from char to string using std::string and I still get errors such as below.


11 W:\Resx\C++\DevC++\USB devprojects\IOencryption\EncFile\Enc.cpp no matching function for call to `std::basic_istream<char, std::char_traits<char> >::get(std::string*)'
 note W:\Resx\Dev-CppPortable\App\devcpp\include\c++\3.4.2\bits\istream.tcc:441 candidates are: typename std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_CharT, _Traits>::get() [with _CharT = char, _Traits = std::char_traits<char>]
 note W:\Resx\Dev-CppPortable\App\devcpp\include\c++\3.4.2\bits\istream.tcc:441                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
 note W:\Resx\Dev-CppPortable\App\devcpp\include\c++\3.4.2\bits\istream.tcc:441                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
 note W:\Resx\Dev-CppPortable\App\devcpp\include\c++\3.4.2\bits\istream.tcc:441                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
 note W:\Resx\Dev-CppPortable\App\devcpp\include\c++\3.4.2\bits\istream.tcc:441                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
 note W:\Resx\Dev-CppPortable\App\devcpp\include\c++\3.4.2\bits\istream.tcc:441                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]
13 W:\Resx\C++\DevC++\USB devprojects\IOencryption\EncFile\Enc.cpp cannot convert `std::string' to `std::string*' for argument `1' to `void encrypt(std::string*)'
 W:\Resx\C++\DevC++\USB devprojects\IOencryption\EncFile\Enc.cpp In function `void encrypt(std::string*)': 
27 W:\Resx\C++\DevC++\USB devprojects\IOencryption\EncFile\Enc.cpp no match for 'operator!=' in '*((+(((unsigned int)i) * 4u)) + e) != '\000''
27 W:\Resx\C++\DevC++\USB devprojects\IOencryption\EncFile\Enc.cpp no match for 'operator++' in '++*((+(((unsigned int)i) * 4u)) + e)' 


This couldn't be because I'm using DevC++ portable on my USB could it? I'm only using this because I have to at the moment, my program files install seems to have gone corrupt (I will install it again, but I want to install it on my own pc which is being repaired at the moment - bloody CPU fan socket on mobo broke...)

Thanks a lot for the help so far!
Okay I just saw that when I hover my mouse over
void encrypt (string e[] )
it reads (Idk if it's DevC++ intellisense or something, it just hovers)
void encrypt (char e[] )
Even though the code says string :S
Do you think that the void might be assuming that the 'string' in void encrypt (string e[] ) is a char ?

------
| Edit |
------

Okay just hovered again and it says that 'string' is a char pointer (char * string)
Why is it saying this when I have removed all char's from my code now?
I literally have no char's in it at all.

<code>#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
void encrypt( string [ ] );
int main( )
{
string uinput;
// create a string to encrypt
cout << "Enter a theoretical string for func: (Game>>Enc>>File>>Game>>Dec>>Read): ";
cin.get(&uinput);
cout << "Original string is: " << uinput << endl;
encrypt( &uinput );
// call to the function encrypt( )
cout << "Encrypted string is: " << uinput << endl;
ofstream myfile;
myfile.open ("EncString.txt");
myfile << &uinput;
myfile.close();
system("pause");
return 0;
}

//encrypt data
void encrypt (&string e[] )
{
for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt</code

I'm probably making some elementary error here, but at gone 1am with practically zero sleep in two days, I'm just incapable of making a rational understanding of this.
I might just take a nap, make some coffee and come back to it, I'll probably figure it out that way.
Last edited on
cannot convert `std::string' to `std::string*' for argument `1' to `void encrypt(std::string*)'


You still have to fix the error where you send a pointer to a function not expecting a pointer. See how you get on with something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::cin;
using std::endl;

void encrypt(std::string &e);
int main( )
{
  std::string uinput;
// create a string to encrypt
cout << "Enter a theoretical string for func: (Game>>Enc>>File>>Game>>Dec>>Read): ";
 getline(cin,uinput);
cout << "Original string is: " << uinput << endl;
encrypt( uinput );
// call to the function encrypt( )
cout << "Encrypted string is: " << uinput << endl;
 std::ofstream myfile;
myfile.open ("EncString.txt");
myfile << uinput;
myfile.close();

return 0;
}

//encrypt data
void encrypt (std::string &e) 
{
  const char* tempCharArray = e.c_str();
  for( int i=0; i<e.size(); ++i )
    e[i] = tempCharArray[i]+1;
  
} // encrypt  
Holy **** it works!
Thank you so much, I don't know why, but I just couldn't see it in my head, but now that it's here for me to see, I totally understand what you was trying to say!

Thanks a lot, Moschops; I have much to learn.
Topic archived. No new replies allowed.