Linear interpolation is not working as expected - c++11

I have a map that represents the values of a coefficient Y for a given range of temperatures. I'm trying to get the coeff_Y whenever the input key designTempfalls anywhere between the upper and lower limits of keys. I was able to get the three cases: a) when the value of the input designTemp is below the first key then coeff_Y is the first value, b) if the value of the input designTemp is beyond the last key then coeff_Y is the last value and c) if designTemp matches a key then the coeff_Y becomes the corresponding value. The case if the key falls anywhere within the key range is not working. The code showing the failed attempt of interpolation is shown below. Please note that I'm not a programmer, I'm a piping engineer just trying to write my own programs and trying to become proficient at coding with C++. Also, if there is any better solution please show so.
`cout << "\n Enter design temp. in degF: ";
float designTemp;
cin.clear(); cin.ignore(10000, '\n'); cin >> designTemp;
map<float, float> ferriticsteels_Y = { {900, 0.4}, {950, 0.5}, {1000, 0.7} };
if (ferriticsteels_Y.find(designTemp) != ferriticsteels_Y.end())
{
float coeff_Y = ferriticsteels_Y[designTemp];
cout << "\n Y: " << coeff_Y << endl;
}
if (designTemp < ferriticsteels_Y.begin()->first)
{
float coeff_Y = ferriticsteels_Y.begin()->second;
cout << "\n Y: " << coeff_Y << endl;
}
if (designTemp > ferriticsteels_Y.rbegin()->first)
{
float coeff_Y = ferriticsteels_Y.rbegin()->second;
cout << "\n Y: " << coeff_Y << endl;
}
auto lower = ferriticsteels_Y.lower_bound(designTemp) == ferriticsteels_Y.begin() ? ferriticsteels_Y.begin() : --(ferriticsteels_Y.lower_bound(designTemp));
auto upper = ferriticsteels_Y.upper_bound(designTemp);
float coeff_Y = lower->second + (upper->second - lower->second) * float(designTemp - lower->first)/fabs(upper->first - lower->first);
time_t rawtime_end;
struct tm * timeinfo_end;
time(&rawtime_end);
timeinfo_end = localtime(&rawtime_end);
cout << "\n" << asctime(timeinfo_end);
cout << "\nEnter any character and hit enter to exit: ";
char ans;
//cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cin >> ans;...giving error at 'max()'
cin.clear(); cin.ignore(10000, '\n'); cin >> ans;
return 0;}`

It works. I just was making stupid mistake. It only required to revise the nesting of the if-statements and to add a cout for looking the interpolated value at the last else. Below is the code which works as expected:
#include "../../std_lib_facilities.h"
#include <Windows.h>
#include <map>
int main()
{
SetConsoleTitle(TEXT("PipeTran™_v0.1"));
system("CLS");
system("color F1");
time_t rawtime_start;
struct tm * timeinfo_start;
time(&rawtime_start);
timeinfo_start = localtime(&rawtime_start);
printf(asctime(timeinfo_start));
cout << "\n Enter design temp. in degF: ";
float designTemp;
cin >> designTemp;
map<float, float> ferriticsteels_Y = { { 900, 0.4 },{ 950, 0.5 },{ 1000, 0.7 } };
if (ferriticsteels_Y.find(designTemp) != ferriticsteels_Y.end()) {
float coeff_Y = ferriticsteels_Y[designTemp];
cout << "\n Y: " << coeff_Y << endl;
}
else if (designTemp < ferriticsteels_Y.begin()->first) {
float coeff_Y = ferriticsteels_Y.begin()->second;
cout << "\n Y: " << coeff_Y << endl;
}
else if (designTemp > ferriticsteels_Y.rbegin()->first) {
float coeff_Y = ferriticsteels_Y.rbegin()->second;
cout << "\n Y: " << coeff_Y << endl;
}
else {
auto lower = ferriticsteels_Y.lower_bound(designTemp) == ferriticsteels_Y.begin() ? ferriticsteels_Y.begin() : --(ferriticsteels_Y.lower_bound(designTemp));
auto upper = ferriticsteels_Y.upper_bound(designTemp);
float coeff_Y = lower->second + (upper->second - lower->second) * float(designTemp - lower->first) / fabs(upper->first - lower->first);
cout << "\n Y: " << coeff_Y << endl;
}
time_t rawtime_end;
struct tm * timeinfo_end;
time(&rawtime_end);
timeinfo_end = localtime(&rawtime_end);
cout << "\n" << asctime(timeinfo_end);
cout << "\nEnter any character and hit enter to exit: ";
char ans;
cin.clear(); cin.ignore(10000, '\n'); cin >> ans;
return 0;
}

Related

ZMQ Multiple Publisher and Single Subscriber -- data loss observed

I have created 2 Publishers connecting to the same static location .
Publisher1
dummyFrontEnd::dummyFrontEnd():context(1),socket(context,ZMQ_PUB) {
}
void dummyFrontEnd::Init()
{
socket.connect("tcp://127.0.0.1:5555");
cout << "Connecting .... " << endl;
}
void dummyFrontEnd::SendTwoTables()
{
cout << "In SendTwoTables" <<endl;
while(1) {
canlogreq canLogObj = canlogreq::default_instance();
canLogObj.set_fromhours(11);
canLogObj.set_fromminutes(7);
canLogObj.set_fromseconds(2);
canLogObj.set_fromday(16);
canLogObj.set_frommonth(5);
canLogObj.set_fromyear(2020);
canLogObj.set_tohours(12);
canLogObj.set_tominutes(7);
canLogObj.set_toseconds(4);
canLogObj.set_today(17);
canLogObj.set_tomonth(5);
canLogObj.set_toyear(2020);
zmq::message_t logsnippetmsg(canLogObj.ByteSizeLong() + sizeof(uint16_t));
*((uint16_t*)logsnippetmsg.data()) = 20;
canLogObj.SerializeToArray(logsnippetmsg.data()+sizeof(uint16_t), canLogObj.ByteSizeLong());
socket.send(logsnippetmsg);
usleep(1);
canLogObj.clear_fromhours();
canLogObj.clear_fromminutes();
canLogObj.clear_fromseconds();
canLogObj.clear_fromday();
canLogObj.clear_frommonth();
canLogObj.clear_fromyear();
canLogObj.clear_tohours();
canLogObj.clear_tominutes();
canLogObj.clear_toseconds();
canLogObj.clear_today();
canLogObj.clear_tomonth();
canLogObj.clear_toyear();
}
}
Publisher2:
dummyFrontEnd::dummyFrontEnd():context(1),socket(context,ZMQ_PUB) {
}
void dummyFrontEnd::Init()
{
socket.connect("tcp://127.0.0.1:5555");
cout << "Connecting .... " << endl;
}
void dummyFrontEnd:: SendData() {
while (std::getline(file, line_str)) {
std::stringstream ss(line_str);
double tdiff;
int i;
char J;
int _1939;
int pgn;
char p;
int priority;
char _0;
int source;
char dash;
std::string direction;
char d;
int length;
int data[8];
ss >> tdiff >> i >> J >> _1939 >> pgn >> p >> priority >> _0 >> source
>> dash >> direction >> d >> length >> data[0] >> data[1] >> data[2]
>> data[3] >> data[4] >> data[5] >> data[6] >> data[7];
timestamp += tdiff;
while (gcl_get_time_ms() - start_time <
uint64_t(timestamp * 1000.0) - first_time) { usleep(1); }
if (arguments.verbose) {
std::cout << timestamp << " " << i << " " << J << " " << _1939 << " "
<< pgn << " " << p << " " << priority << " " << _0 << " " << source
<< " " << dash << " " << direction << " " << d << " " << length
<< " " << data[0] << " " << data[1] << " " << data[2] << " "
<< data[3] << " " << data[4] << " " << data[5] << " " << data[6]
<< " " << data[7] << std::endl;
}
uint64_t timestamp_ms = (uint64_t)(timestamp * 1000.0);
protoTable.add_columnvalues(uint64ToString(timestamp_ms) /* timestamp */);
protoTable.add_columnvalues(intToString(pgn) /* PGN */);
protoTable.add_columnvalues(intToString(priority) /* Priority */);
protoTable.add_columnvalues(intToString(source) /* Source */);
protoTable.add_columnvalues(direction /* Direction */);
protoTable.add_columnvalues(intToString(length) /* Length */);
protoTable.add_columnvalues(intToString(data[0]) /* data1 */);
protoTable.add_columnvalues(intToString(data[1]) /* data2 */);
protoTable.add_columnvalues(intToString(data[2]) /* data3 */);
protoTable.add_columnvalues(intToString(data[3]) /* data4 */);
protoTable.add_columnvalues(intToString(data[4]) /* data5 */);
protoTable.add_columnvalues(intToString(data[5]) /* data6 */);
protoTable.add_columnvalues(intToString(data[6]) /* data7 */);
protoTable.add_columnvalues(intToString(data[7]) /* data8 */);
zmq::message_t create_values(protoTable.ByteSizeLong()+sizeof(uint16_t));
*((uint16_t*)create_values.data()) = TABLEMSG_ID; // ID
protoTable.SerializeToArray(create_values.data()+sizeof(uint16_t), protoTable.ByteSizeLong());
socket.send(create_values);
protoTable.clear_columnvalues();
usleep(1);
}
}
Subscriber
TransportLayer::TransportLayer():context(1),socket(context,ZMQ_SUB){ }
void TransportLayer::Init()
{
socket.bind("tcp://*:5555");
socket.setsockopt(ZMQ_SUBSCRIBE, "", 0);
}
void TransportLayer::Receive()
{
cout <<"TransportLayer::Receive " << " I m in server " << endl;
static int count = 1 ;
// Producer thread.
while ( true ){
zmq::message_t request;
string protoBuf;
socket.recv(&request);
uint16_t id = *((uint16_t*)request.data());
cout <<"TransportLayer : "<<"request.data: "<< request.data() << endl;
cout << "TransportLayer: count " << count<< endl; count = count+1 ;
cout <<"TransportLayer : request.data.size "<< request.size() << endl;
protoBuf = std::string(static_cast<char*>(request.data()+sizeof(uint16_t)), request.size()-sizeof(uint16_t));
cout << "ProtoBuf : " << protoBuf << endl;
InterfaceLayer *interfaceLayObj = InterfaceLayer::getInstance();
switch(id) {
case TABLEMSG_ID: cout << "Canlyser" << endl;
interfaceLayObj->ParseProtoBufTable(protoBuf);
break;
case LOGSNIPPET_ID:cout << "LogSnip" << endl;
interfaceLayObj->ParseProtoBufLogSnippet(protoBuf);
interfaceLayObj->logsnippetSignal(); // publish the signal
break;
default:
break;
}
usleep(1);
}
}
Observation :
I)
Execution Order .
1. Started Subscriber
2. Started Publisher1 ( it sent only one data value)
Subscriber missed to receive this data.
II) modified Publisher1 to send the same data in a while loop
Execution Order
1. Started Subscriber
2. Started Publisher1
3. Started Publsiher2 .
Now I see that Subscriber is receiving the data from both publishers .
This gives me an indication that there is a possibility for data loss.
How do I ensure there is absolutely no data loss.
Thanks
Your producer may be sending its one message before the subscription handshaking has completed. As a result, the publish socket discards the message because there is no subscription registered.
If you use an XPUB socket (ZMQ_XPUB -- see http://api.zeromq.org/4-2:zmq-socket) instead of a PUB socket, your program can wait for a subscribe message, so that it knows that someone is listening, before sending its message(s).

Initialize references to matrix element in struct

I have a struct that represents a 3D position. Sometimes it's convenient to access the individual components and sometimes it's convenient to access all components as a vector (physics vector not std::vector) for which I'm using the Eigen linear algebra library. Since there are only three elements (x, y, z) and will only ever be three elements, is there anything wrong with the struct having three double& that refer to the elements of the Eigen Matrix? i.e.:
using ColumnVector3 = Eigen::Matrix<double, 3, 1>;
struct EnuPosition
{
EnuPosition(): pos(ColumnVector3::Zero()), east(pos[0]), north(pos[1]), up(pos[2]) {}
EnuPosition(double east, double north, double up): pos((ColumnVector3() << east, north, up).finished()),
east(pos[0]), north(pos[1]), up(pos[2]) {}
EnuPosition(const ColumnVector3& position): pos(position), east(pos[0]), north(pos[1]), up(pos[2]) {}
EnuPosition(const EnuPosition& enu):pos(enu.pos), east(pos[0]), north(pos[1]), up(pos[2]) {}
EnuPosition& operator=(const EnuPosition& enu)
{
this->pos = enu.pos;
return *this;
}
ColumnVector3 pos;
double& east;
double& north;
double& up;
};
It compiles fine with no warnings on g++ 5.5 with -Wall -Wextra -pedantic in the use cases I can think of:
int main ()
{
EnuPosition enu{12.5, 34.2, 99.2};
std::cout << "east: " << enu.east
<< " north: " << enu.north
<< " up: " << enu.up
<< std::endl;
ColumnVector3 x;
x << 2.0,3.0,4.0;
enu.pos = x;
std::cout << "east: " << enu.east
<< " north: " << enu.north
<< " up: " << enu.up
<< std::endl;
Eigen::MatrixXd y;
y.resize(3,1);
y << 7.6,8.7,9.8;
enu.pos = y;
std::cout << "east: " << enu.east
<< " north: " << enu.north
<< " up: " << enu.up
<< std::endl;
Eigen::Matrix<double,3,3> R;
enu.east = 1;
enu.north = 1;
enu.up = 1;
R << 1,2,3,4,5,6,7,8,9;
enu.pos = (R * enu.pos).eval();
std::cout << "east: " << enu.east
<< " north: " << enu.north
<< " up: " << enu.up
<< std::endl;
EnuPosition enu2 = enu;
std::cout << "east: " << enu2.east
<< " north: " << enu2.north
<< " up: " << enu2.up
<< std::endl;
}
Like I said, it works, I'm just curious if it's legal and not relying on undefined behavior, etc. Or are there other issues to be cognizant of?
After you added the copy-assignment your code should be safe.
However, if you are ok with writing east() instead of east in your code, then a slightly more elegant solution might be this:
using ColumnVector3 = Eigen::Matrix<double, 3, 1>;
struct EnuPosition : public ColumnVector3
{
EnuPosition(): ColumnVector3(ColumnVector3::Zero()) {}
EnuPosition(double east, double north, double up): ColumnVector3(east, north, up) {}
template<class X>
EnuPosition(const X& other): ColumnVector3(other) {}
double& east() {return this->x();}
double const& east() const {return this->x();}
double& north() {return this->y();}
double const& north() const {return this->y();}
double& up() {return this->z();}
double const& up() const {return this->z();}
};
If you intentionally don't want to inherit, you can of course still store the ColumnVector3 as a member.

Trying to add fractions while also printing LCD

I'm a beginner trying to figure out this program of adding fractions while also making their output print the result in it's lowest common denominator form. Running it in this form never runs properly...
using namespace std;
class Fraction { //Creates class Fraction
private: //Makes data members private
int num;
int denm;
};
int main()
{
int num;
int denm;
int num2;
int denm2;
int plus;
int plus2;
cout << "Please enter the numerator and denominator of the first fraction: " << endl;
cin >> num >> denm;
cout << "Please enter the numerator and denominator of the second fraction: " << endl;
cin >> num2 >> denm2;
plus = num*denm2 + denm*num2;
plus2 = denm*denm2;
cout << num << "/" << denm << " + " << num2 << "/" << denm2 << " = " << plus << "/" << plus2;
cout << "Hit 'enter' to exit..." << endl;
}
You'll need to run the program in a fashion that keeps the output window open or modify it to accomplish this. See here for examples:
How to keep the console window open in Visual C++?
One way to do this in any environment would be to cin another value before the final return 0 - this would, of course, require you to press something other than enter first, but it serves the purpose.

C++ .txt read in issues. getline reading full file

first of all, forgive my code for being ugly. The tons of ideas I've been given to try to fix this code have jumbled it up after all the potential solutions that haven't worked. Basically, I'm coding a Hearthstone rip-off that reads in two .txt files with card information and battles them to see which player wins. The issue is that when I'm trying to save the player's name (the first line in the files), it saves the whole file instead of just the first line. When I have managed to fix that, the for loop used to save the information for the card objects (format: card name, card power, card health) does not get saved properly for some reason. Any help would be appreciated, I've been trying to fix this for two days and nothing has fully solved the problem. I'll attach the read in files first before the code.
Disclaimer: It's a lot of lines and I'm sorry about that. Also I think the problem could be that my Mac is not saving the .txt in a format that has the right line endings. I'm using XCode as my IDE. Thank you so much to whomever is willing to help!
File1:
The Innkeeper
3
Tunnel Trogg
1
3
Neptulon
7
7
Fire Elemental
6
5
File2:
Malfurion
3
Leper Gnome
2
1
Aviana
5
5
Cenarius
5
8
Main:
#include "Player.h"
using namespace std;
int main()
{
cout << "Please enter file name of the first player: " << endl;
string inFile = "";
getline(cin, inFile);
Player* p1 = new Player(inFile);
cout << "Now enter the file name of the second player: " << endl;
getline(cin, inFile);
Player* p2 = new Player(inFile);
p1->battle(*p2);
delete p1;
delete p2;
return 0;
}
Player Header:
#include "Card.h"
#include <fstream>
#ifndef Player_h
#define Player_h
using namespace std;
class Player
{
private:
string playerName;
int numCards;
Card ** cards;
int wins = 0;
public:
Player(std::string inFile);
void battle(Player p2);
Card* getCard(int counter);
~Player();
};
#endif /* Player_h */
Card Header:
#include <string>
#include <iostream>
#ifndef Card_h
#define Card_h
using namespace std;
class Card
{
public:
Card();
string getName();
int getPower();
int getHealth();
void setName(string newName);
void setPower(int newPower);
void setHealth(int newHealth);
Card* duel(Card&);
friend ostream& operator<<(ostream& o, Card& c);
friend bool operator==(Card& p1Card, Card& p2Card);
private:
string name;
int power;
int health;
};
#endif /* Card_h */
Player Source:
#include "Player.h"
using namespace std;
Player::Player(string inFile)
{
ifstream in(inFile, ios::in);\
if (!in)
{
cerr << "There was a problem opening the file. Sorry, try again!" << endl;
return;
}
getline(in, playerName);
cout << playerName << endl;
in>>numCards;
playerName = "";
numCards = 0;
cards = new Card* [numCards];
string tempName = "";
int tempPower = 0;
int tempHealth = 0;
for (int i = 0; i<numCards; i++)
{
in.ignore();
cards[i] = new Card();
getline(in, tempName);
cout << "in for loop: " << endl;
cout << tempName << ",";
cards[i]->setName(tempName);
in >> tempPower;
in.ignore();
cout << tempPower << ",";
cards[i]->setPower(tempPower);
in >> tempHealth;
cout << tempHealth << " done"<< endl;
cards[i]->setHealth(tempHealth);
}
}
void Player::battle(Player p2)
{
int draws = 0;
cout << "Let the battle begin!" << endl;
cout << numCards << endl;
if (wins > p2.wins)
{
cout << playerName << " wins over " << p2.playerName << ", " << wins << " to " << p2.wins;
if (draws == 0)
{
cout << " and no ties." << endl;
}
else
{
cout << " and " << draws << " ties." << endl;
}
}
else if (p2.wins > wins)
{
cout << p2.playerName << " wins over " << playerName << ", " << p2.wins << " to " << wins;
if (draws == 0)
{
cout << " and no ties." << endl;
}
else
{
cout << " and " << draws << " ties." << endl;
}
}
else if (p2.wins == wins)
{
cout << "It is a draw between " << playerName << " and " << p2.playerName << ", with " << wins << " for each and ";
if (draws == 0)
{
cout << "no ties." << endl;
}
else
{
cout << draws << " ties." << endl;
}
}
cout << "Here are the detailed results:" << endl;
for (int i = 0; i < numCards; i++)
{
cout << *cards[i] << " vs. " << *p2.cards[i] << " - ";
if (*cards[i] == *p2.cards[i])
{
cout << "It is a draw." << endl;
}
else if (cards[i]->duel(*p2.cards[i]) == NULL)
{
cout << "It is a draw." << endl;
}
else if (*cards[i]->duel(*p2.cards[i]) == *p2.cards[i])
{
cout << p2.cards[i]->getName() << "wins for " << p2.playerName << "." << endl;
}
else if (*cards[i]->duel(*p2.cards[i]) == *cards[i])
{
cout << cards[i]->getName() << "wins for " << playerName << "." << endl;
}
}
}
Player::~Player()
{
if (cards != NULL)
{
for (int i = 0; i < numCards; i++)
{
if (cards[i] != nullptr)
{
delete cards[i];
cards[i] = NULL;
}
};
}
}
Card Source:
#include "Card.h"
using namespace std;
Card::Card()
{
name = "";
power = 0;
health = 0;
}
string Card::getName()
{
return name;
}
int Card::getPower()
{
return power;
}
int Card::getHealth()
{
return health;
}
void Card::setName(string newName)
{
name = newName;
}
void Card::setPower(int newPower)
{
power = newPower;
}
void Card::setHealth(int newHealth)
{
health = newHealth;
}
Card* Card::duel(Card& otherCard)
{
if ((otherCard.getHealth() - this->getPower() <=0) && (getHealth() - otherCard.getPower() <= 0))
{
return NULL;
}
else if ((otherCard.getHealth() - this->getPower() >0) && (getHealth() - otherCard.getPower() >0))
{
return NULL;
}
else if (otherCard.getHealth() - this->getPower() <=0)
{
return this;
}
else if (this->getHealth() - otherCard.getPower() <=0)
{
return &otherCard;
}
return NULL;
}
ostream& operator<<(ostream& o, Card& c)
{
o << c.getName() << " (" << c.power << ", " << c.health << ") " << endl;
return o;
}
bool operator==(Card& p1Card, Card& p2Card)
{
if (p1Card.health == p2Card.health &&
p1Card.power == p2Card.power &&
p1Card.name == p2Card.name)
{
return true;
}
else
{
return false;
}
}
Your code is almost right. It can read the Player's name and the card numbers, but your codes showed below:
in>>numCards;
playerName = "";
numCards = 0;
cards = new Card* [numCards];
at first, it read the num of card and store it to numCards, it is right.
next, you clear the value of the numCards, then, you lost the num of the Card, so the codes followed it are executed with numCards == 0
You can just comment the line numCards = 0, and your code is executed right.

How to use SetConsoleTextAttribute C++

I have searched countless forums and websites but I can't seem to find the answer. I'm trying to use SetConsoleTextAttribute but it only affects the text. How can I affect the whole screen like the command color 1f would? My code is:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wincon.h>
using namespace std;
int main()
{
SetConsoleTitle("C++ CALCULATOR"); // Title of window
int x; // Decision
int a; // First Number
int b; // Second Number
int c; // Answer
HANDLE Con;
Con = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(Con, BACKGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout << "CALCULATOR" << endl << endl;
cout << "1:ADDITION" << endl << "2:SUBTRACTION" << endl << "3:MULTIPLICATION";
cout << endl << "4:DIVISION" << endl << "5:EXIT" << endl;
cin >> x;
switch (x)
{
case 1: // Addition code
cout << endl << "ADDITION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a + b;
cout << endl << "ANSWER:" << c;
break;
case 2: // Subtraction code
cout << endl << "SUBTRACTION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a - b;
cout << endl << "ANSWER:" << c;
break;
case 3: // Multiplication code
cout << endl << "MULTIPLICATION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a * b;
cout << endl << "ANSWER:" << c;
break;
case 4: // Division code
cout << endl << "DIVISION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a / b;
cout << endl << "ANSWER:" << c;
break;
case 5: // Exit code
return 0;
}
}
This solution relies on these WinAPI functions and structures:
GetConsoleScreenBufferInfo to get screen dimensions
FillConsoleOutputAttribute to fill screen with an attribute
CONSOLE_SCREEN_BUFFER_INFO structure to store screen information
The code is as follows:
HANDLE hCon;
CONSOLE_SCREEN_BUFFER_INFO csbiScreenInfo;
COORD coordStart = { 0, 0 }; // Screen coordinate for upper left
DWORD dwNumWritten = 0; // Holds # of cells written to
// by FillConsoleOutputAttribute
DWORD dwScrSize;
WORD wAttributes = BACKGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
// Get the screen buffer information including size and position of window
if (!GetConsoleScreenBufferInfo(hCon, &csbiScreenInfo))
{
// Put error handling here
return 1;
}
// Calculate number of cells on screen from screen size
dwScrSize = csbiScreenInfo.dwMaximumWindowSize.X * csbiScreenInfo.dwMaximumWindowSize.Y;
// Fill the screen with the specified attribute
FillConsoleOutputAttribute(hCon, wAttributes, dwScrSize, coordStart, &dwNumWritten);
// Set attribute for newly written text
SetConsoleTextAttribute(hCon, wAttributes);
The inline comments should be enough to understand the basics of what is going with the supplied documentation links. We get the screen size with GetConsoleScreenBufferInfo and use that to determine the number of cells on the screen to update with a new attribute using FillConsoleOutputAttribute . We then use SetConsoleTextAttribute to ensure that all new text that gets printed matches the attribute we used to color the entire console screen.
For brevity I have left off the error check for the calls to FillConsoleOutputAttribute and SetConsoleTextAttribute. I put a stub for the error handling for GetConsoleScreenBufferInfo . I leave it as an exercise for the original poster to add appropriate error handling if they so choose.
SetConsoleTextAttribute changes the attribute for new characters that you write to the console, but doesn't affect existing contents of the console.
If you want to change the attributes for existing characters already being displayed on the console, use WriteConsoleOutputAttribute instead.

Resources