-
November 6th, 2013, 09:53 AM
#1
[RESOLVED] errors about types
Code:
void write()
{
setlocale(LC_ALL, "en_US.UTF-8");
cout <<"";
}
template <typename A, typename ...B>
void write(string argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
string a;
a=argHead;
TextBlink(a, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(a.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
template <typename A, typename ...B>
void write(char *argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
string a;
a=argHead;
TextBlink(a, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(a.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
string a;
//the error is in next 2 lines
a=to_string(argHead);
TextBlink(a, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(a.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
what i'm doing wrong with my write() function?
i did like the read() function(for work diferent with another types) but i get these error:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|218|error: call of overloaded 'to_string(char*&)' is ambiguous|"
can anyone advice me?
-
November 6th, 2013, 11:06 AM
#2
Re: errors about types
Originally Posted by Cambalinho
... i get these error:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|218|error: call of overloaded 'to_string(char*&)' is ambiguous|"
can anyone advice me?
Google can it for sure!
https://www.google.com/search?source...s%7C&gs_htsa=1
Victor Nijegorodov
-
November 6th, 2013, 11:12 AM
#3
Re: errors about types
Last edited by Cambalinho; November 6th, 2013 at 11:14 AM.
-
November 6th, 2013, 11:31 AM
#4
Re: errors about types
Originally Posted by Cambalinho
i have tested and the problem is other
What did you test and how?
Originally Posted by Cambalinho
by some reason the compiler don't call that functions correctly
It is not a compiler but you yourself "don't call that functions correctly"!
First have a look at the documentation: http://www.cplusplus.com/reference/string/to_string/
Then compare the type you pass in to_string with the available types: what the type is A of?
Is it one of the mentiond in to_string documentation?
Victor Nijegorodov
-
November 6th, 2013, 11:41 AM
#5
Re: errors about types
Originally Posted by VictorN
i ahve seen the problem is other
by some reason the compiler don't call that functions correctly
see these code:
Code:
if (typeid(argHead)==typeid(char*))
{
TextBlink(argHead, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
Position.X=csbi.dwCursorPosition.X+strlen(argHead);
}
else if (typeid(argHead)==typeid(string))
{
TextBlink(argHead.c_str(), csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
Position.X=csbi.dwCursorPosition.X+argHead.length();
}
why the second 'if' is give me errors? don't make sence
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|178|error: request for member 'c_str' in 'argHead', which is of non-class type 'char*'|"
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|179|error: request for member 'length' in 'argHead', which is of non-class type 'char*'|"
-
November 6th, 2013, 11:59 AM
#6
Re: errors about types
Originally Posted by Cambalinho
why the second 'if' is give me errors? don't make sence
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|178|error: request for member 'c_str' in 'argHead', which is of non-class type 'char*'|"
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\console.h|179|error: request for member 'length' in 'argHead', which is of non-class type 'char*'|"
Compiler has no idea what type your argHead is of.
If you are sure it is of the string type then try to cast it to string.
Victor Nijegorodov
-
November 6th, 2013, 12:06 PM
#7
Re: errors about types
i found my error.. i was seen the read() better and my problem was here:
Code:
template <typename ...B>
void write(string argHead, B... argTail)
(like you see, i delete the 'typename A')
heres the entire code:
Code:
void write()
{
setlocale(LC_ALL, "en_US.UTF-8");
cout <<"";
}
template <typename ...B>
void write(string argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
string a;
a=argHead;
TextBlink(a, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(a.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
template <typename ...B>
void write(char *argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
string a;
a=argHead;
TextBlink(a, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(a.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
string a;
//the error is in next 2 lines
a=to_string(argHead);
TextBlink(a, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(a.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
only by the 'typename A' the compiler was confuse... but i fix it... thanks for all
-
November 6th, 2013, 12:12 PM
#8
Re: [RESOLVED] errors about types
Have a look at this
Code:
#include <string>
#include <iostream>
using namespace std;
template<typename A>
void typetest(A t)
{
if (typeid(t)==typeid(char*)) {
cout << "got type char*" << endl;
cout << t << endl;
} else {
if (typeid(t)==typeid(string)) {
cout << "got type string" << endl;
cout << ((string)t).c_str() << endl;
}
}
}
int main()
{
string st = "This is a string";
char carr[] = "This is a c array";
typetest<string>(st);
typetest<char*>(carr);
return 0;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 6th, 2013, 12:21 PM
#9
Re: [RESOLVED] errors about types
Originally Posted by 2kaud
Have a look at this
Code:
#include <string>
#include <iostream>
using namespace std;
template<typename A>
void typetest(A t)
{
if (typeid(t)==typeid(char*)) {
cout << "got type char*" << endl;
cout << t << endl;
} else {
if (typeid(t)==typeid(string)) {
cout << "got type string" << endl;
cout << ((string)t).c_str() << endl;
}
}
}
int main()
{
string st = "This is a string";
char carr[] = "This is a c array";
typetest<string>(st);
typetest<char*>(carr);
return 0;
}
please see my last post
-
November 6th, 2013, 12:27 PM
#10
Re: [RESOLVED] errors about types
Originally Posted by Cambalinho
please see my last post
Yeah - You posted between me reading and replying!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 6th, 2013, 12:33 PM
#11
Re: [RESOLVED] errors about types
Originally Posted by 2kaud
Yeah - You posted between me reading and replying!
thanks for all... you help me so much
-
November 6th, 2013, 12:37 PM
#12
Re: [RESOLVED] errors about types
VictorN thanks for all... thanks
-
November 6th, 2013, 03:08 PM
#13
Re: [RESOLVED] errors about types
Code:
#include "console.h"
int main()
{
char Text[]="hello world fdsfdsaffdsafjkdsnaljkfdsbaohjfbodhsjalbfndsljabnlfjdspaifjdsnkaklfndsçka";
string c ="oi";
int a=10;
double b=20.34;
Console.SetColorAndBackground(FOREGROUND_BLUE,BackColorCyan,true);
Console.Clear();
Console.read(Text);
Console.write(Text,a,b,c);
Console.read();
}
see these test??.. by some reason i have a memory leak. why? because windows tell me that the program stop to answer and then close it.
can you advice me?
(on write, if i use just the Text or if i take of the ',true' in SetColorAndBackground(), i don't get problems)
Last edited by Cambalinho; November 6th, 2013 at 03:12 PM.
-
November 6th, 2013, 03:56 PM
#14
Re: [RESOLVED] errors about types
Without having the code for the Console class it's not possible to say.
Use the debugger and trace through your code and see where is the problem.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 6th, 2013, 04:16 PM
#15
Re: [RESOLVED] errors about types
Originally Posted by 2kaud
Without having the code for the Console class it's not possible to say.
Use the debugger and trace through your code and see where is the problem.
strange if i execute normaly, i get the windows error, if i debug it theres no windows error... nothing
Blink.h
Code:
#include <windows.h>
#include <process.h>
#include <string>
#include <iostream>
using namespace std;
//ForeColor
const int ForeColorBlack = 0;
const int ForeColorBlue = FOREGROUND_BLUE;
const int ForeColorGreen = FOREGROUND_GREEN;
const int ForeColorCyan = FOREGROUND_BLUE | FOREGROUND_GREEN;
const int ForeColorRed = FOREGROUND_RED;
const int ForeColorMagenta = FOREGROUND_BLUE | FOREGROUND_RED;
const int ForeColorBrown = FOREGROUND_GREEN | FOREGROUND_RED;
const int ForeColorLightGray = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
const int ForeColorDarkGray = FOREGROUND_INTENSITY;
const int ForeColorLightBlue = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const int ForeColorLightGreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const int ForeColorLightCyan = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const int ForeColorLightRed = FOREGROUND_RED | FOREGROUND_INTENSITY;
const int ForeColorLightMagenta = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
const int ForeColorYellow = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
const int ForeColorWhite = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
//BackColor
const int BackColorBlack = 0;
const int BackColorBlue = BACKGROUND_BLUE;
const int BackColorGreen = BACKGROUND_GREEN;
const int BackColorCyan = BACKGROUND_BLUE | BACKGROUND_GREEN;
const int BackColorRed = BACKGROUND_RED;
const int BackColorMagenta = BACKGROUND_BLUE | BACKGROUND_RED;
const int BackColorBrown = BACKGROUND_GREEN | BACKGROUND_RED;
const int BackColorLightGray = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
const int BackColorDarkGray = BACKGROUND_INTENSITY;
const int BackColorLightBlue = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
const int BackColorLightGreen = BACKGROUND_GREEN | BACKGROUND_INTENSITY;
const int BackColorLightCyan = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
const int BackColorLightRed = BACKGROUND_RED | BACKGROUND_INTENSITY;
const int BackColorLightMagenta = BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY;
const int BackColorYellow = BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
const int BackColorWhite = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
struct Blink
{
string Text;
SHORT x;
SHORT y;
WORD Attributes;
};
unsigned __stdcall BlinkLoop(void *params)
{
Blink *p = static_cast<Blink *>(params);
SHORT tlen = static_cast<SHORT>(p->Text.length());
SHORT x=p->x, y=p->y;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
const char *text=(char*)p->Text.c_str();
CHAR_INFO *ConsoleText= new CHAR_INFO[tlen];
CHAR_INFO *EmptyConsoleText= new CHAR_INFO[tlen];
COORD a={tlen,1}, b={0,0};
SMALL_RECT c={x, y,SHORT(x+tlen),SHORT(y+1)};
int i=0;
for (i=0; i<=tlen;i++)
{
ConsoleText[i].Char.AsciiChar =text[i];
ConsoleText[i].Attributes=p->Attributes;
EmptyConsoleText[i].Char.AsciiChar =' ';
EmptyConsoleText[i].Attributes=p->Attributes;
}
while (true)
{
WriteConsoleOutput(hout,ConsoleText,a,b,&c);
Sleep(500);
WriteConsoleOutput(hout,EmptyConsoleText,a,b,&c);
Sleep(500);
}
return 0;
}
void TextBlink(const string Text, SHORT x, SHORT y, const WORD TextAttribute=ForeColorWhite | ForeColorBlack)
{
Blink *b = new Blink;
b->Text = Text;
b->Attributes=TextAttribute;
b->x=x;
b->y =y;
_beginthreadex(NULL, 0, BlinkLoop, b, 0, NULL);
}
Console.h
Code:
/*heres a nice class for work with console commands, properties and events*/
/*#ifndef myheadguard1
#define myheadguard1
//insert contents of header here
#endif*/
#include <iostream>
#include <typeinfo>
#include <string>
#include <Windows.h>
#include <conio.h>
#include <sstream>
#include <cstddef>
#include <typeinfo>
#include <stdio.h>
#include <clocale>
#include "TextBlink.h"
#define MY_BUFSIZE 1024
const std::string NewLine = "\n";
using namespace std;
struct Position
{
int X;
int Y;
};
struct Size
{
int Width;
int Height;
};
class Console
{
private:
char pszOldWindowTitle[MY_BUFSIZE];
HWND ConsoleHandle;
HDC ConsoleDC;
bool blnVisible;
bool ReadEnter=false; //these boolean variable is for distinguish the functions used;)
//properties
DWORD GetConsoleSize(HANDLE sout, COORD& size)
{
CONSOLE_FONT_INFO cfinfo;
COORD fsize;
WINDOWINFO winfo;
if (!GetCurrentConsoleFont(sout, FALSE, &cfinfo))
{
return (GetLastError());
}
fsize = GetConsoleFontSize(sout, cfinfo.nFont);
if (!fsize.X && !fsize.Y)
{
return (GetLastError());
}
winfo.cbSize = sizeof(WINDOWINFO);
if (!GetWindowInfo(GetConsoleWindow(), &winfo))
{
return (GetLastError());
}
size.Y = (SHORT)((winfo.rcClient.bottom - winfo.rcClient.top) / fsize.Y);
size.X = (SHORT)((winfo.rcClient.right - winfo.rcClient.left) / fsize.X);
return (0);
}
bool blCaretVisible;
bool blnBlink=false;
public:
void (*print)(){};
//initializate
Console ()
{
COORD consize;
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleSize(hcon, consize);
SetConsoleScreenBufferSize(hcon, consize);
SetVisible(true);
}
Console (void (*create) ())
{
COORD consize;
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleSize(hcon, consize);
SetConsoleScreenBufferSize(hcon, consize);
SetVisible(true);
create();
}
//HDC and Handle and Hwnd
HDC hdc()
{
return GetDC(GetConsoleWindow());
}
HWND hwnd()
{
return GetConsoleWindow();
}
HANDLE handle()
{
return GetStdHandle(STD_OUTPUT_HANDLE);
}
//Console title
string ConsoleTitle()
{
GetConsoleTitle( pszOldWindowTitle,MY_BUFSIZE);
return string(pszOldWindowTitle);
}
void ConsoleTitle(string title)
{
SetConsoleTitle(title.c_str());
}
//Console Position
POINT GetConsolePosition()
{
POINT a;
RECT WindowRect;
GetWindowRect(GetConsoleWindow() ,&WindowRect);
a.x=WindowRect.left;
a.y=WindowRect.top;
return a;
}
void SetConsolePosition(POINT Position )
{
SetWindowPos(GetConsoleWindow(),HWND_TOP,Position.x,Position.y,0,0,SWP_SHOWWINDOW||SWP_NOOWNERZORDER);
}
void write()
{
setlocale(LC_ALL, "en_US.UTF-8");
cout <<"";
}
template <typename ...B>
void write(string argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
TextBlink(argHead, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(argHead.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
template <typename ...B>
void write(char *argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
string a;
a=argHead;
TextBlink(a, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(a.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
setlocale(LC_ALL, "en_US.UTF-8");
if (blnBlink==true)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
string a;
a=to_string(argHead);
TextBlink(a, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y,csbi.wAttributes);
COORD Position;
Position.X=csbi.dwCursorPosition.X+strlen(a.c_str());
Position.Y=csbi.dwCursorPosition.Y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
else
cout << argHead;
write(argTail...);
}
//Read
//empty
void read()
{
if (ReadEnter==false)
{
while(kbhit())
;
HANDLE hConIn = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
DWORD oldMode;
GetConsoleMode(hConIn, &oldMode);
SetConsoleMode(hConIn, ENABLE_LINE_INPUT);
FlushConsoleInputBuffer(hConIn);
char buffer[1];
DWORD read;
ReadConsoleA(hConIn, buffer, sizeof(buffer), &read, nullptr);
SetConsoleMode(hConIn, oldMode);
CloseHandle(hConIn);
}
}
template<typename ...B>
void read(string &s, B&... tail)
{
ReadEnter=true;
while(kbhit())
;
setlocale(LC_ALL, "en_US.UTF-8");
getline(cin, s);
read(tail...);
ReadEnter=false;
}
template<typename ...B>
void read(char s[256], B&... tail)
{
ReadEnter=true;
while(kbhit())
;
setlocale(LC_ALL, "en_US.UTF-8");
cin.getline(s,256);
read(tail...);
ReadEnter=false;
}
template <typename A, typename ...B>
void read(A &argHead, B&... argTail)
{
ReadEnter=true;
while(kbhit())
setlocale(LC_ALL, "en_US.UTF-8");
cin >> argHead;
read(argTail...);
ReadEnter=false;
}
//clear the Console
void Clear(int BackColor=-1)
{
// home for the cursor
COORD coordScreen = {0, 0};
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
// Get the number of character cells in the current buffer
if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
return;
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
// Fill the entire screen with blanks
if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (WCHAR)' ', dwConSize, coordScreen, &cCharsWritten))
return;
// Get the current text attribute.
if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
return;
if (BackColor!=-1)
{
DWORD textcolor = csbi.wAttributes & 0xff0f;
//DWORD backcolor = (csbi.wAttributes & 0xfff0) >> 4;
csbi.wAttributes=textcolor | BackColor ;//here we change the backcolor
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes );
// Set the buffer's attributes accordingly.
if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten))
return;
// Put the cursor at its home coordinates.
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
}
//Turn cursor on
DWORD CursorOn(HANDLE sout)
{
CONSOLE_CURSOR_INFO cur;
if (!GetConsoleCursorInfo(sout, &cur))
{
return (GetLastError());
}
cur.bVisible = TRUE;
return (!SetConsoleCursorInfo(sout, &cur) ? GetLastError() : 0);
}
//Turn cursor off
DWORD CursorOff(HANDLE sout)
{
CONSOLE_CURSOR_INFO cur;
if (!GetConsoleCursorInfo(sout, &cur))
{
return (GetLastError());
}
cur.bVisible = FALSE;
return (!SetConsoleCursorInfo(sout, &cur) ? GetLastError() : 0);
}
bool GetCaretVisible()
{
return blCaretVisible;
}
void SetCaretVisible(bool Visible)
{
if (Visible==true)
{
CursorOn(handle());
}
else
{
CursorOff(handle());
}
blCaretVisible=Visible;
}
void SetColorAndBackground(int ForgC, int BackC=0, bool Blink=false)
{
blnBlink=Blink;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForgC | BackC );
}
void SetConsoleIcon(char *strIconPath)
{
HANDLE lIcon;
lIcon = LoadImage(0,strIconPath , 1, 0, 0, LR_LOADFROMFILE);
SendMessage(GetConsoleWindow() , WM_SETICON, 0,(LPARAM) lIcon) ;
}
COORD GetCaretPosition()
{
COORD s;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
s.X=csbi.dwCursorPosition.X;
s.Y=csbi.dwCursorPosition.Y;
return s;
}
void SetCaretPosition(COORD Position)
{
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),Position);
}
bool GetVisible()
{
return blnVisible;
}
void SetVisible(bool visible)
{
if (visible== true)
{
ShowWindow(GetConsoleWindow(),SW_SHOWNORMAL);
}
else
{
ShowWindow(GetConsoleWindow(),SW_HIDE);
}
blnVisible=visible;
}
COORD GetConsoleWindowSize()
{
COORD consize;
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleSize(hcon, consize);
//SetConsoleScreenBufferSize(hcon, consize);
return consize;
}
void SetConsoleWindowSize(COORD Size)
{
SMALL_RECT s;
POINT WindowPos;
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
WindowPos=GetConsolePosition() ;
s.Left=(SHORT)WindowPos.x;
s.Top=(SHORT)WindowPos.y;
s.Bottom=(SHORT)WindowPos.y+Size.Y;
s.Right= (SHORT)WindowPos.x+Size.X;
GetConsoleScreenBufferInfo( hcon, &csbi);
if (Size.X > (csbi.srWindow.Bottom- csbi.srWindow.Top) || Size.Y > (csbi.srWindow.Right- csbi.srWindow.Left))
{
SetConsoleScreenBufferSize(hcon, Size);
}
SetConsoleWindowInfo(hcon,true,&s);
}
}Console;
the main.cpp
Code:
#include "console.h"
int main()
{
char Text[]="hello world fdsfdsaffdsafjkdsnaljkfdsbaohjfbodhsjalbfndsljabnlfjdspaifj";
string c ="oi";
int a=10;
double b=20.34;
Console.SetColorAndBackground(FOREGROUND_BLUE,BackColorCyan,true);
Console.Clear();
Console.read(Text);
Console.write(a,b,Text,c);
Console.read();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|