No operator matches these operands: visual studio c++ window forms error [duplicate] - c++-cli

This question already exists:
operand types are: System::String ^ <= int error
Closed 2 years ago.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
long mb = 15001;
long sb = 1500001;
long msb = 25000001;
if (tc < mb)
{
}
}
Im new to programming and im currently trying to develop an app for a school project that takes 2 numerical inputs and 1 string input in c++ windows form via a textbox to give a required output. Here im having an issue where i can't use the less than in my condition. i get the error "no operator "<" matches these operands". How do i fix this.
Thank you

Related

using "query user /server:XXX" command in c++ windows form application

I'm developing a small app to find out the users logged into the server( ip provided) using VS2008 windows form application.
i have wrote the below code which seems to be working fine only when i'm logged into any one of the remote servers, if i install the same application in my local machine and try to run it, i get below error :
'Query' is not recognized as internal or external command, operable program or batch file.
press any key to continue...
Note : if i just directly run 'system user /server:XXX' command via command prompt of my local machine, i will get the required details, without facing any error.
My doubt is, why am i facing error if i run the same command with my application?
PFB code which i have written :
static std::string toStandardString(System::String^ string)
{
using System::Runtime::InteropServices::Marshal;
if (string->Length == 0 || string->Length < 0)
{
MessageBox::Show("No field can be empty");
}
System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string);
char* charPointer = reinterpret_cast<char*>(pointer.ToPointer());
std::string returnString(charPointer, string->Length);
Marshal::FreeHGlobal(pointer);
return returnString;
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ com = "query user /server:";
String^ texStr= textBox1->Text;
String^ full = com + texStr + " & pause";
std::string str1 = toStandardString(full);
char str2[100];
strcpy(str2,str1.c_str());
system(str2);
getch();
}
TIA.

(C++ / CLI) Set contents of textbox to content of a .txt

I am looking to set the contents of a textbox to that of a .txt file,
however I cannot get it to work. I have a button which would "refresh" the
content of the textbox to that of the .txt file, here is the code I am using:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
std::ifstream dat1("DataStore.txt");
String^ textHolder;
if (dat1.is_open())
{
while ( getline(dat1, line) )
{
textHolder += line.c_str;
}
textBox1->Text = textHolder;
dat1.close();
}
else textBox1->Text = "Unable to open file";
}
I get these 2 errors when compiling the program:
error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': function call missing argument list; use '&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str' to create a pointer to member
error C2297: '+=' : illegal, right operand has type 'const char *(__thiscall std::basic_string<char,std::char_traits<char>,std::allocator<char>>::* )(void) throw() const'
How do I make this work?
NOTE: I am trying to display the whole .txt file's contents in the textbox
If you are going to use C++/CLI you may as well take advantage of .net. You can use the File class to read the file for you:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
textBox1->Text = System::IO::File::ReadAllText("DataStore.txt");
}
You'll need to hope that the process working directory contains that file. In a GUI app you are better served by specifying full paths to files, since the working directory is typically ill-defined.
If you are trying to learn C++, then perhaps a .net C++/CLI WinForms application is not the place to start.
You forgot to end method c_str, change it to c_str()

C++ will change it last input

I don't know if anyone had the same problem.
I use Microsoft visual C++ 2010 I am new to it but I know a lot of Java program but for me trying to go from one program to another is confusing.
Anyway I will make a basic program "Hello World" and run it it works fine but when I go and try to type something else or add something to the text it remains the same code when I run it. It does not happen all the time but it will do it a lot and it seam like it is becoming more common.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
label1 -> Text = "hello world";
}
This is what I put.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
label1 -> Text = "This is a test";
}
when I run the first code it works, but when I change to the code and put in the 2nd code it will remain the first code when I run it, but I change what the code say so it should change as well but it does not.
There is a setting that controls whether Visual Studio forces a build before you run, likely you set it to false.
Here is a similar question
Tools + Options, Projects and Solutions, Build and Run. "On Run, when projects are out of date" = Always build.

Brother Label Printer c# program with visual studio 2012 for windows8

I want to make a simple program which prints sth (Just wnt it to write sth )
I ve added Interop.bpac.dll (Found it from samples bin folder)
I ve wrote this code
private void buttonTry_Tapped_1(object sender, TappedRoutedEventArgs e)
{
bpac.DocumentClass doc = new DocumentClass();
if(doc.Open("templateFile.lbx"))
{
doc.GetObject("field1").Text = "Hello";
doc.GetObject("field2").Text = "World";
doc.StartPrint("", PrintOptionConstants.bpoDefault);
doc.PrintOut(1, PrintOptionConstants.bpoDefault);
doc.EndPrint();
doc.Close();
}
}
And it gives an error "Interop type 'bpac.DocumentClass' can not be embedded.Use the applicable interface instead." For now Im trying to print QL700 I ll try some other thermal receipt printers later
And also I couldnt get the templateFile.lbx whats that and where does the program search this file?
Thanks :)
Change Embed Interop Types to False

Trying to write a textfield value to file in c++

I am tearing my hair out over this error.
------ Build started: Project: shotfactorybatchgen, Configuration: Debug Win32 ------
shotfactorybatchgen.cpp
c:\documents and settings\administrator\my documents\visual studio 2010\projects\shotfactorybatchgen\shotfactorybatchgen\Form1.h(307): error C2664: 'fprintf' : cannot convert parameter 2 from 'System::String ^' to 'const char *'
No user-defined-conversion operator available, or
Cannot convert a managed type to an unmanaged type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have looked all around the interwebs but I can not find an answer. Here is the code that the error is happening in.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Decimal value;
if(!Decimal::TryParse(textBox4->Text, value)) {
MessageBox::Show("Non-numeric characters detected in 'Wait Time' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
} else {
if(!Decimal::TryParse(textBox3->Text, value)) {
MessageBox::Show("Non-numeric characters detected in 'Max Upload' filed", "Oops", MessageBoxButtons::OK, MessageBoxIcon::Warning);
} else {
FILE *OutFile = fopen("run.bat","w");
fprintf(OutFile,"#ECHO OFF\r\nC:\\Python26\\python.exe factory.py");
if(factoryname->Text != ""){
fprintf(OutFile," -f ");
fprintf(OutFile,factoryname->Text);
}
fclose(OutFile);
}
}
}
Any ideas? It is a simple windows form application. I am using Visual Studio C++ 2010
Thanks
Colum
If factoryname->Text identifies a property, then its getter may throw a CLR exception, in which case you will leak the file handle OutFile. Consider not using fopen, etc. It's a C library function and there are better alternatives such as std::ofstream.
Alternatively, as you have .NET available, you could use StreamWriter which will let you pass System::String and thus avoid your conversion problem as well:
StreamWriter writer(someFilePath);
writer.WriteLine(someString);
C++/CLI will take care of closing the writer when the scope is exited.
It's simply a conversion error:
cannot convert parameter 2 from 'System::String ^' to 'const char *'
Possible solution is posted here:
What is the best way to convert between char* and System::String in C++/CLI
Opening Files
Convert System::String to const char*