-2
uchar szPlaintext[128];                        //dato da criptare 
cout << "\nInserisci testo : ";
getline(cin, szPlaintext);

I tried it with a getline (cin, szPlaintext); but I have a lot of errors. I compile with VS2015. Premise that I am trying to implement an AES (not mine), to my program; the uchar declaration was in this way.

uchar szPlaintext [128] = "text that I want to insert";

The errors are:

1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2672: 'getline': no matching overloaded function found
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'uchar [128]'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(157): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(146): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'uchar [128]'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(126): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(73): note: see declaration of 'std::getline'
Pietrob0b
Utente Junior

Messaggi: 36
Iscritto il: 10 dic 2015, 20:44
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Pietro Ariano
  • 113
  • 1
  • 2
  • 12

2 Answers2

0

std::getline takes a std::string reference, not a raw pointer.

string sPlaintext;
cout << "\nInserisci testo : ";
getline(cin, sPlaintext);

uchar* szPlaintext = (uchar*) sPlaintext.c_str();  //dato da criptare 
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • if I try to declare string or char, it conflicts with other functions, the original program is designed to work only with uchar. For this I asked. I am trying to implement an AES (not mine), to my program. – Pietro Ariano Dec 22 '15 at 13:29
  • then you should get a string and parse it to uchar array. – ForeverStudent Dec 22 '15 at 13:47
  • @jpo :Negative for this line "uchar* szPlaintext = (uchar*) sPlaintext.c_str();" .do you think this will work for some reason ? – SACHIN GOYAL Dec 22 '15 at 13:52
  • @SACHIN uchar must be a typedef of unsigned char. Even if not explicitly told in the OP, I doubt it could be anything else. – jpo38 Dec 22 '15 at 15:01
  • @jpo : no issue with uchar or char .I will advice you to run your program .will it work like that ? – SACHIN GOYAL Dec 22 '15 at 15:03
  • Thank you for the advice, but it would have been better if you had tested it yourself. Because it definitely works : http://www.cpp.sh/5nfpe – jpo38 Dec 22 '15 at 17:19
-1

As others have already pointed out std::getline takes a reference of std::string not "char *" , so your program has failed to compile . Below are the two version of std::getline .

istream& getline (istream& is, string& str, char delim);

istream& getline (istream& is, string& str);

Now , how to make it work assuming you want input string in char* .Follow the below program .

// Example program
#include <iostream>
#include <string>
#include<cstring>

int main()
{
    char szPlaintext[128];                        //dato da criptare 
    std::cout << "\nInserisci testo : ";
    std::string str ;
    getline(std::cin, str);
    strcpy(szPlaintext,str.c_str());

    std::cout << szPlaintext;
}
jpo38
  • 20,821
  • 10
  • 70
  • 151
SACHIN GOYAL
  • 955
  • 6
  • 19
  • Your code is unsafe and will seg fault if user input string is greater than 128. – jpo38 Dec 22 '15 at 17:42
  • See the OP's question .He himself has taken a string of size 128 .Only problem he was facing was to insert input to this string ,So i guess he has assumed that input will not be greater than 128 length .Anyways , idea of the program was to display , how is it done .if OP require changes in string length , he can easily do that . – SACHIN GOYAL Dec 23 '15 at 03:09
  • Size should be tested before doing strcpy. – jpo38 Dec 23 '15 at 13:44