Keyboard input on unsigned char? - c++

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

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

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;
}

Related

How to add #include <iostream> to project? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I just started learning C++ and I was trying to create a simple code with Visual Studio 2017.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int x;
std::cout >> "Enter Age";
std::cin >> x;
if (x >= 18) {
std::cout << "You are an adult." << endl;
}
else {
std::cout << "You are a child." << endl;
}
return 0;
}
But I think I was getting a lot of errors such as iostream didn't exist because I didn't see it in the header file or iostream.h
I tried creating a new header file and simply typing #include <iostream> but that didn't seem to work. Here are my errors.
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2784: 'std::basic_istream<char,_Traits> &std::operator >>
(std::basic_istream<char,_Traits> &,signed char &)': could not deduce
template argument for 'std::basic_istream<char,_Traits> &' from
'std::ostream'
1>c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1068):
note: see declaration of 'std::operator >>'
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2784: 'std::basic_istream<char,_Traits> &std::operator >>
(std::basic_istream<char,_Traits> &,signed char *)': could not deduce
template argument for 'std::basic_istream<char,_Traits> &' from
'std::ostream'
1>c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1061):
note: see declaration of 'std::operator >>'
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>
(std::basic_istream<_Elem,_Traits> &,_Elem &)': could not deduce template
argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
1>c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1036):
note: see declaration of 'std::operator >>'
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>
(std::basic_istream<_Elem,_Traits> &,_Elem *)': could not deduce template
argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
1>c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(995): note:
see declaration of 'std::operator >>'
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2676: binary '>>': 'std::ostream' does not define this operator or a
conversion to a type acceptable to the predefined operator
1>Done building project "ConsoleApplication3.vcxproj" -- FAILED.
Thanks for any help.
Check this: std::cout >> "Enter Age";. I suppose it to be std::cout << "Enter Age"; instead.
BTW, not related to the question, since you are writing std::cout and std::cin then it would be better if you keep the consistency with std::endl instead of endl.
Problem is in line 8. It shoud be:
std::cout << "Enter Age";

My libraries are not recognized in visual studio Express 2015

I am doing a c++ assignment and I am using visual studio express. In my header.h file I have the following libraries:
#include <cstdlib>
#include<iostream>
#include <cstring>
#include<fstream>
#include<string>
#include <vector>
#include<iomanip>
#ifndef _HEADER_H_
#define _HEADER_H_
using namespace std;
and on my main.cpp and Source.cpp I have a reference to the Header.h:
#include"Header.h"
It seams that the libraries are not recognized since I get the following errors:
Severity Code Description Project File Line
Error C2079 'ss' uses undefined class 'std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>'
Error C2440 'initializing': cannot convert from 'std::string' to 'int'
Error C2780 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': expects 2 arguments - 3 provided
Error C2784 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
Error C2780 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)': expects 2 arguments - 3 provided
Error C2784 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'int'
Error C2780 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': expects 2 arguments - 3 provided
Error C2784 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
However the project runs fine in Visual studio professional. But we have been asked to do it in Express.
Please help
Thank you
The failing code would be nice, but it looks as if you are missing the
#include <sstream>
header. The full suite seems to have that included in some other header file, hence the different behavior.

Priority queue works with vectors but not lists

I am tightening up some rendering code and instead of storing all my renderable objects in a plain ol vector I decided to use a priority queue (this way things like transparency can be automatically prioritized correctly). I cannot get it to work with a list as the underlying data structure however. I have tried it using both a functor and by overloading the < operator. It complains about:
Error 8 error C2676: binary '-' : 'std::_List_iterator<std::_List_val<std::_List_simple_types<IRenderable *>>>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 2425 1 ObjLoaded
Error 4 error C2676: binary '-' : 'std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<IRenderable *>>>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 2331 1 ObjLoaded
Error 7 error C2784: 'unknown-type std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<IRenderable *>>>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 2425 1 ObjLoaded
Error 3 error C2784: 'unknown-type std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<IRenderable *>>>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 2331 1 ObjLoaded
Error 6 error C2784: 'unknown-type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<IRenderable *>>>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 2425 1 ObjLoaded
Error 2 error C2784: 'unknown-type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<IRenderable *>>>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 2331 1 ObjLoaded
Error 5 error C2784: 'unknown-type std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<IRenderable *>>>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 2425 1 ObjLoaded
Error 1 error C2784: 'unknown-type std::operator -(std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'std::move_iterator<_RanIt> &' from 'std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<IRenderable *>>>' c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 2331 1 ObjLoaded
This is how I am declaring the priority queue:
priority_queue<IRenderable*, list<IRenderable*>> m_renderlist;
with the overloaded < operator. If I make one simple change, everything runs:
priority_queue<IRenderable*, vector<IRenderable*>> m_renderlist;
Any Idea why might be happening here? For completeness, here is my overload and functor:
bool IRenderable::operator<(const IRenderable* comp)
{
if (this->GetPriority() < comp->GetPriority())
return true;
return false;
}
//class IRenderableComp
//{
//public:
// bool operator()(const IRenderable* first, const IRenderable* second)
// {
// if (first->GetPriority() < second->GetPriority())
// return true;
// return false;
// }
//};
Not super important because I can get it working with vectors, but this little stuff bugs me and I want to understand why. Any insight would be appreciated. Thanks
23.6.4/1 Any sequence container with random access iterator and supporting operations front(), push_back() and pop_back() can be used to instantiate priority_queue.
23.3.5.1/1 list is a sequence container that supports bidirectional iterators...
Emphasis mine.

error C2784 ,class in key map

I have a problem with the container map. I need to store my own class Person in key but i have error C2784 (i.e., "The compiler cannot determine a template argument from the supplied function arguments."). It's example from the book "Ivor Horton's beginning Visual C++ 2010"
#include<map>
#include<string>
#include <iostream>
using namespace std;
void main()
{
class Person{
public:
string c_name,c_surname;
Person(string name,string surname){
c_name=name;
c_surname=surname;
}
};
map<Person,string> phonebook;
phonebook.insert(make_pair(Person("Mel","GIBSON"),"24 32 23"));
phonebook[Person("Mel2","Gibson2")]="243 32 23";
/* it doesn`t work too
typedef pair<Person,string> Entry;
Entry entry1= Entry(Person("Jack","Jones"),"213 567 1234");
phonebook.insert(entry1);*/
system("Pause");
}
Error 1 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 2 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 3 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 4 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 5 error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 6 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 7 error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 8 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 9 error C2676: binary '<' : 'const main::Person' does not define this operator or a conversion to a type acceptable to the predefined operator e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
The problem here is that std::map requires your keys to be comparable with the < operator. Custom structures/classes are not that by default, you need to make a custom operator< for comparison.
In C++03 you could not use local classes (classes defined within functions) as template arguments.
In C++11 you can.
So one fix is to update the compiler (there is Visual C++ 2013), and another fix is to move the class definition out of main.
By the way, void main is invalid as standard C++, and as standard C, and it's more to type than standard int main. If your book has void main, then that's a very ungood book. Microsoft's examples that include void main are also very ungood.
Also, by the way, the
system("Pause");
at the end is also very ungood practice because
it is not necessary, has no advantage, but
it makes the program more difficult to use and has some other problems, and to top it all,
it's Windows-specific, non-portable code.
To run a console program so that it stops at the end
in Visual Studio use Ctrl+F5, or
in Visual Studio place a breakpoint at the end of main (just click in the left margin) and run it with debugging (e.g. via keypress F5), or
run it from a command interpreter.
UPDATE: the now added error messages (even the first one) mention operator<. You need to define that also. That is, define an operator< function for your class Person.

TR1 regex_replace with wstring in VS2010?

#include <iostream>
#include <string>
#include <regex>
#include <ios>
#include <locale>
using namespace std;
int main ()
{
const wstring wstr(L"<(.|\\n)*?>");
static const wregex wr(wstr);
wstring line (L"<tag>Random text<tag>");
wstring line2 (L"");
wcout << regex_replace<wchar_t>(line,wr,line2) << endl;
}
Compiler says:
ClCompile:
html.cpp
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef "std::tr1::regex_replace"
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef of "std::tr1::regex_replace"
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef "std::tr1::regex_replace"
Partial answer:
You should use regex_repalce this way:
wcout << regex_replace(line,wr,line2) << endl;
i.e., without the wchar_t. The first argument is for the Element Traits class, which you'd only rarely want to modify.
Edit
I've checked your code with VC++ 2010. Changing the line as I specified allowed the code to compile, and return the result as expected. Can you try it again?