C++

57K

Solutions

24K

Contributors

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

Share tech news, updates, or what's on your mind.

Sign up to Post

I am trying to add icon to a context menu in a MFC app.

this code adds a textual menu item, but how do I add the icon to it?
CMenu l_menu;
l_menu.CreatePopupMenu();
l_menu.AppendMenu(MF_STRING, ID_REPORT_COLUMN_NEW, _T("some string"));

Open in new window

Thank you
Vaclav
0
Learn SQL Server Core 2016
LVL 13
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Im trying to make a small battery tester.

It is charging up battery fine up to 2kv while reading analog values, then goes into discharging loop and dies there.  I need help with the second loop.

Can you please help?

I want to charge and discharge while reading analog values over and over again in endless loop.

void loop(){
  
  // read the raw data coming in on analog pin 0:
  int val = analogRead(analogPin);  // read the input pin
  // Convert the raw data value (0 - 1023) to voltage (0.0V - 5.0V):
  float voltage = val * (5.0 / 1024.0); 
  //convert to actual voltage
  int volts = voltage * 10000; 

  
  //if the voltage is less than 2,000v keep charging
  if (voltage < 0.19){
  lcd.setCursor(1,1);
  lcd.print("Charging     ");
  digitalWrite(chargePin, HIGH); 
  
  //print to lcd
  lcd.setCursor(1,0);  
  lcd.print("Volts: ");
  lcd.print(volts); 
  lcd.print("     ");
  delay(100); 
  }

  else{  
    //we reached 2000v, discharge untill fully discharged
    while (voltage > 0.01){  
    lcd.setCursor(1,1);
    lcd.print("Discharging    ");
    digitalWrite(chargePin, LOW);
    
    //print to lcd
    lcd.setCursor(1,0);  
    lcd.print("Volts: ");
    lcd.print(volts); 
    lcd.print("     ");
    delay(100); 
    }
  }

  
}

Open in new window

0
Need help to convert below C++ code to C#. Thanks for the help.

In C++:
double dValue = 14 / 1000;
ostringstream oss;
oss.precision(10);
oss<<fixed<<dValue;
string sValue = oss.str();
1
Tried to use VC++ 2008 command line utility cl to compile the code that contains an array with variable size. A lot of errors were generated. In Linux all needs to do is to include the option -std=C99 when compiling.  Is there any workaround for it? Can any gurus shed some light to it? Thanks a lot.
0
I have an electronics board which has 16 relays and it works over TCP/IP.
The IP address of the board is 192.168.1.4 and the port is 3000.
I would like to control it with C/C++ under Ubuntu.

There is a list of HEX commands that it is possible to use in order to remotely switch ON and OFF each relay on the board.
This is the list:
"580112000000016C",  // switch on the relay 1

"580111000000016B",  // switch off the relay 1
"580112000000026D", // switch on the relay 2
"580111000000026C", // switch off the relay 2
"580112000000036E", // so on..
"580111000000036D",
"580112000000046F",
"580111000000046E",
"5801120000000570",
"580111000000056F",
"5801120000000671",
"5801110000000670",
"5801120000000772",
"5801110000000771",
"5801120000000873",
"5801110000000872",
"5801120000000974",
"5801110000000973",
"5801120000000A75",
"5801110000000A74",
"5801120000000B76",
"5801110000000B75",
"5801120000000C77",
"5801110000000C76",
"5801120000000D78",
"5801110000000D77",
"5801120000000E79",
"5801110000000E78",
"5801120000000F7A",
"5801110000000F79",
"580112000000107B",
"580111000000107A",
"5801130000FFFF77",
"580113000000007B",
"5801100000000069"

I'm correctly able to switch on and off each relay by sending command line commands under Ubuntu:

echo '580112000000016C' | xxd -r -p | nc 192.168.1.4 3000

Open in new window

The above code correctly turns on the relay.

I would like to do the same with C/C++ code since I want to control the …
1
I'm developing a WxWidgets application under Ubuntu and I'm using CodeBlocks as IDE.

I need to:

  • read the data stored in a magnetic card band
  • check if they are OK
  • parse the string in order to get the code, the name and the surname

I'm using a USB magnetic card reader and I set a TextCtrl as "focused" so it immediately gets the data from the reader automatically.

The format of the string is like this:

%URSMCC84R04E815LURSO  MARCO_ò8038001605011036724_

Open in new window


The control characters are always the same and I need to read everything between % and _.
I need to obtain three strings:
  • the first one containing the first 16 chars
  • the second contains the surname which goes from the 17th till the first space
  • the third string contains the name and goes from the first char after the space till the _

What is the best way to do this in Wxwidgets?
Moreover, since the reader emulates a keyboard, the output string is read a char at a time, so how can I design the parsing code?

This is the code I'm using to read the string.

void ProjectFrame::OntesseraText(wxCommandEvent& event)
{
    wxString stringa_tessera = tessera->GetValue(); // I read the string coming from the USB reader
    TextCtrl2->ChangeValue( stringa_tessera ); // I load the string in the second TextCtrl just for test
}

void ProjectFrame::OnTextCtrl2Text(wxCommandEvent& event)
{
// TextCtrl used to display the parsed string
}

Open in new window


I see that there is a class wxStringTokenizer, should I use it?
I'm sorry but I'm just a beginner :(

Thank you!
0
I am writing a Win32 Service application using embarcadero XE 10.1 Berlin C++ builder on a 64 bit Windows 7 PC.

I am starting with a trivial project prior to putting "flesh on the bones", but can't get the simple example to work ?
when I run the application with /install as a command line parameter (which usually installs the service app), I get the following error instead.

    Exception EOSError in module KERNELBASE.dll at 0000C5AF.
    System Error. code 5
Access is denied.


I get this error regardless of whether or not the Beep function is called.
I have set the StartType to stDisabled so I would expect the service application to be installed but it isn't.
 The code is as follows, any help would be appreciated. I have written service applications before using Borland C++ builder but no Embarcadero XE C++ builder.


C++ file:
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TTestServiceName *TestServiceName;
//---------------------------------------------------------------------------
__fastcall TTestServiceName::TTestServiceName(TComponent* Owner)
	: TService(Owner)
{
}

void __stdcall ServiceController(unsigned CtrlCode)
{
	TestServiceName->Controller(CtrlCode);
}

TServiceController __fastcall TTestServiceName::GetServiceController(void)
{
	return (TServiceController) ServiceController;
}

Open in new window

0
Hi, I am trying to see the actual address that a C/C++ pointer points to.  I can easily see the data that it points to but I'm trying to see the actual address of those data, i.e., the contents of the pointer itself.

I am using cout for the attempted display of this which will be an address in hex (along with other data that displays just fine in my cout statement).

The environment is VS2015.

Any help will be appreciated.
0
I am trying to catch an exception that is generated when a TCP client connection is attempted when the connection is already connected. The code that I am using us shown below.
All I get is a message on the screen  as also shown below.
How do I suppress this message and catch the exception in my code ?
I a using Embarcadero XE 10.1 berlin C++ Builder with an Indy 10 TCP client component.

void __fastcall TForm1::Button2Click(TObject *Sender)
{
	try
	{
		IdTCPClient1->Connect();
	}
	catch(...)
	{
        Memo1->Lines->Add(u"Failed to connect");
    }
}

Open in new window


Already connected message
0
I am trying to set up a binding on an Indy 10 TIdUDPServer component with Embarcadero XE 10.1 Berlin  C++ builder

I As soon as the Bind() call is made, I get an error message as follows:

"Exception class EIdNotAsocket with message 'socket Error #10038' Socket Operation on non-socket."

How can I set up the binding ?

the following is my code:

	TIdSocketHandle *h;
	udpserver = new TIdUDPServer(NULL);
	udpserver->OnUDPRead = IdUDPServer1UDPRead;
	h = udpserver->Bindings->Add();
	h->Port = 791;
	h->IP   = "127.0.0.1";
	try
	{
		h->Bind();
	}
	catch(...)
	{
	};

Open in new window

0
Announcing the Winners!
LVL 13
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

I am coding a C++ class which has a member function void Start(list<int> lst);

I wish to assign a default  value to the lst parameter but I haven't worked out how to declare and implement this in the declaration of the member function .

How would I declare this with a default list of integers 1,3,5,7 for example ?

class MyClass
{
        public:
        MyClass(){};
       ~MyClass(){};
void Start(list<int> lst = ???);
}

Open in new window

0
Is there some functionality already available in C++14 to convert a number from decimal representation into binary?

Thanks.
0
I need to generate an XML document based on the result of a database query (ADOQuery).
Is it appropriate to use a TXMLDocument component and if so how would I do this ?
I am writing a VCL application for windows using Embarcadero C++ builder XE 10.1 Berlin
0
I am unable to get any reply from the Getit Package manager in Embarcadero RAD Studio 10.2 or Embarcadero C++ Builder  XE 10.1 Berlin.
Does anyone know when this will be fixed by Embarcadero ?
0
What is the simplest way to the conversion below?
kconn.cpp:35:15: error: non-const lvalue reference to type 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') cannot bind to a value of unrelated type 'const char [4]'
            addRoute("JFK", "ATL", 150);
                     ^~~~~
kconn.cpp:27:31: note: passing argument to parameter 's' here
        void addRoute(std::string &  s,

Open in new window

1
My IDE is visual studio community 2019.  I'm  unable to link GLUT library with my C++ project.  I get following link errors:

link errors
Do I need to specify some windows libraries in "Additional Dependencies" field?


Additional Dependencies
0
I'm using Visual Studio Community 2019.
I found 'OpenGL C++ Tutorial : Getting Started' on youtube.   I get error when I try to link project.  Maybe I'm not specifying 'Additional Dependencies' correctly in my project setting.  Please see attached.

Also, attached is the link error.
Additional_dependencies.PNG
lib_in_proj_folder.PNG
link_error.PNG
0
Hi,

  I  tried to find the free space on mounted volume which is aws elastic filesystem. used statvfs api to find, but statvfs_obj.f_bavail and statvfs_obj.f_blocks are 13digit numbers, statvfs_obj.f_files and statvfs_obj.f_ffree are zeros.

  How to retrieve correct details from mounted aws efs on linux by using statvfs( mounted_filesys_path, &statvfs_struct_obj).  Th path looks like /mnt/efs_mounted_from_xx_city.

  OS: RHEL7.4,

  mount details:  nfs4, rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,noresvport,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=xxxx,local_lock=none,addr=xxxxx 0 0


Thanks,
Bvm
0
I have just purchased QuickReport 6.0 Professional and can't get it to compile. to illustrate this I have created a trivial application as follows:
The missing file that appears to be missing is QuickRp.h  (NOT QuickRp.hpp)
I have contacted QBS software for support and they are experiencing the same problem and haven't come up with a solution so far after three days.,
any help would be appreciated.

I have a simple Form (Windows VCL 32bit application) with a TQuickRep control placed on the form and a TQRBand.
The header file of the project is as follows:
class TForm1 : public TForm
{
__published:     // IDE-managed Components
                TQuickRep *QuickRep1;
                TQRBand *QRBand1;
private:                // User declarations
public:                   // User declarations
                __fastcall TForm1(TComponent* Owner);
};
The first attempt to use it with Embarcadero C++ builder XE 10.1 Berlin gives an error messages when I attempt to save the project prior to compilation:
“Unable to find header file for component: QuickRpt”
Pressing cancel results in the same error message being displayed 6 times in total.
 
The code doesn’t compile as the header files are missing and the compiler error “Type name expected” is given (naturally)
 
The installation ran without any errors using the downloaded file QuickReportv6QR6DXE10_1_Berlin_W64_installer.exe
The PC is a Dell Precision laptop running 64 bit windows 7 Professional …
0
OWASP: Forgery and Phishing
LVL 13
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

I have just installed Embarcadero 10.2 RAD and every project I try to build or compile returns the following error without referencing any line in the project. Even new projects that I create with nothing in them are giving this error.

[Fatal Error] Object reference not set to an instance of an object
0
I am using GetPrivateProfileString to retrieve some data for a program I am working on. Can anyone tell me why the code below does not work as well as which it often breaks at runtime.

LPCTSTR lpAppName2;
LPCTSTR lpKeyName2;
LPCTSTR lpDefault;
LPTSTR  lpReturnedString;
DWORD   nSize;
LPCTSTR lpFileName2;

	GetPrivateProfileString(
		lpAppName2 = TEXT ("Section11"),
		lpKeyName2 = TEXT ("FirstKey1"),
		lpDefault  = TEXT ("String Not Found") ,
		lpReturnedString ,
		nSize ,
		lpFileName2 = TEXT ("c:\\aa\\test2.ini")
		);
	Application->ProcessMessages();
	ShowMessage(lpReturnedString);

Open in new window

0
Is it possible to hide the main form in a Windows application written in Embarcadero C++ builder XE 10.1  ?
0
I'm trying to declare an array for a graph problem in class.  When I try and print the arrays I get an "expression must have class type" error.

#pragma once
#ifndef GRAPH_H
#define GRAPH_H
#include <list>
#include <vector>
#include <string>
#include <set>

class Graph {
private:
	static const int MAXIMUM = 20;
	int mileageGraph[MAXIMUM][MAXIMUM];  //compiles so long as I comment out my print function
	int costGraph[MAXIMUM][MAXIMUM];
	std::vector<std::string> vertex;
	//int labels[MAXIMUM];
	int manyVertices;

public:  //this is where the functions go

	Graph();
	void addVertex(const std::string value);
	int getMileage(int source, int destination);
	int getCost(int source, int destination);
	void addEdge(int source, int destination, int mileage, int cost);
	bool isEdge(int source, int destination) const;
	//void shortestPath(int source);
	int getID(std::string vertex);
	std::string getName(int vertexId);
	int size();
	void printDirectFlights();
};

#endif // !GRAPH_H

void Graph::printDirectFlights()
{
	cout << "Direct Flights" << endl;
	cout << left <<
		setw(10) << "Source" <<
		setw(15) << "Destination" <<
		setw(10) << "Mileage" <<
		setw(10) << "Price" << '\n';
	for (int i = 1; i < MAXIMUM; i++)
	{
		for (int j = 1; j < MAXIMUM; j++)
		{
			if (costGraph.isEdge(i, j))  //breaks
			{
				cout << left <<
					setw(10) << getName(i) <<
					setw(15) << getName(j) <<
					setw(10) << mileageGraph.getMileage(i, j) <<  //breaks
					setw(10) << costGraph.getCost(i, j)

Open in new window

0
I am having a problem with an old function I used to use about 20 years ago.
I am using Embarcadero RAD 8.3

I have added windows.h to my include list
AnsiString name;
GetPrivateProfileString(_T("Section 1"), _T("Key 1"), _T("") , name, 32, _T("C:\\test.ini"));

Open in new window

This is the error I am getting when I try to compile

[bcc32c Error] Unit2.cpp(53): no matching function for call to 'GetPrivateProfileStringW'

I find this most confusing.
0
Hi,

I am having a problem with Indy in Embarcadero RAD 10.3. I am using the version of Indy that RAD 10.3 shipped with. I am getting a timeout error with MyPoP->Connect();

MyPop3->Host = "host";
MyPop3->Username = "username";
MyPop3->Password = "password";
MyPop3->Port = 995;
MyPop3->Connect();

This is the error I am getting.
Capture_13.jpg
0

C++

57K

Solutions

24K

Contributors

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.