Hey I'm trying to save text entered in textbox as string
#pragma once
#include <iostream>
#include <string>
namespace Project1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
int IloscBD1=0, IloscBD2=0, IloscPD1, IloscPD2, Suma, I, J, Punkty, P1, P2, P3, P4, P5, P6, Druzyna;
int TabPunkt[10][6];
std::string TabOdpowiedzi[10][6];
...
...
private: System::Void buttonZ_Click(System::Object^ sender, System::EventArgs^ e) {
TabOdpowiedzi[1][1] = (Convert::ToString(textPO->Text));
this->textPN->Text = (Convert::ToString(TabOdpowiedzi[1][1]));
}
But I am getting those errors, what's wrong? How can I keep input from text box as a string for future or is there a better way to keeping text input for future usage ?
Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
Error 2 error C2665: 'System::Convert::ToString' : none of the 37 overloads could convert all the argument types
3 IntelliSense: no operator "=" matches these operands
operand types are: std::string = System::String ^
4 IntelliSense: no instance of overloaded function "System::Convert::ToString" matches the argument list
argument types are: (std::string)
You are mixing up your types. Declare
std::string TabOdpowiedzi[10][6];
As
array<System::String^,2>^ TabOdpowiedzi = gcnew array<System::String^,2>(10, 6);
And your problems here vanish. Maybe you will have problems in other parts of your code then, but you have not posted those...
Related
For a vector of strings, return the sum of each string's size.
I tried to use accumulate, together with a lambda function (Is it the best way of calculating what I want in 1-line?)
Codes are written in wandbox (https://wandbox.org/permlink/YAqXGiwxuGVZkDPT)
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v = {"abc", "def", "ghi"};
size_t totalSize = accumulate(v.begin(), v.end(), [](string s){return s.size();});
cout << totalSize << endl;
return 0;
}
I expect to get a number (9), however, errors are returned:
/opt/wandbox/gcc-head/include/c++/10.0.0/bits/stl_numeric.h:135:39: note: 'std::__cxx11::basic_string' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
135 | __init = _GLIBCXX_MOVE_IF_20(__init) + *__first;
I want to know how to fix my codes? Thanks.
That's because you do not use std::accumulate properly. Namely, you 1) did not specify the initial value and 2) provided unary predicate instead of a binary. Please check the docs.
The proper way to write what you want would be:
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v = {"abc", "def", "ghi"};
size_t totalSize = accumulate(v.begin(), v.end(), 0,
[](size_t sum, const std::string& str){ return sum + str.size(); });
cout << totalSize << endl;
return 0;
}
Both issues are fixed in this code:
0 is specified as initial value, because std::accumulate needs to know where to start, and
The lambda now accepts two parameters: accumulated value, and the next element.
Also note how std::string is passed by const ref into the lambda, while you passed it by value, which was leading to string copy on each invocation, which is not cool
I'm trying to use to_string to append an integer value to a string like this:
#include <iostream>
#include <string>
using namespace std;
int main(){
int number = 1;
string p = "Player";
p += to_string(number);
return 0;
}
But when I try to compile it I get an error stating to_string was not declared in this scope, so I removed the using namespace std; and replaced to_string with std::to_string, but now I get a new error saying that to_string is not a member of std.
This problem only occurs when I compile from the command line but it works perfectly in XCode.
Any ideas on why this is? (I need to be able to compile it from the command line for a hw assignment)
to_string is a feature of C++11, did you use the flag --std=c++11?
I want to be able to append a constant string to the end of another string in the form of a char*, and then use the resulting string as an argument for open(). Here's what it looks like:
file1.cpp
#include "string.h"
file2 foo;
char* word = "some";
foo.firstWord = word; //I want the file2 class to be able to see "some"
file2.h
#include <fstream>
#include <iostream>
#define SECONDWORD "file.txt"
class file2{
public:
file2();
static char* firstWord;
static char* fullWord;
private:
ofstream stream;
}
file2.cpp
#include "file2.h"
char* file2::firstWord;
char* file2::fullWord;
fullWord = firstWord + SECONDWORD; //so fullWord is now "somefile.txt" ,I know this doesn't work, but basically I am trying to figure out this part
file2::file2(){
stream.open(fullWord);
}
So I am not very well versed in C++, so any help would be appreciated!
C++-style solution might be the following.
#include <string>
char* a = "file";
char* b = ".txt";
...
stream.open((std::string(a) + b).c_str());
What happens here? First, std::string(a) creates a temporary std::string object. Than b value is added to it. At last, c_str() method returns a c-style string which contains a + b.
I'm trying to copy characters from a System::String ^ to a rectangular char array.
First I tried: (along with some other code. not relevant to the question)
char name[25][21];
...
void savedata(int x, System::String ^ a){ //x is the student #, a is the name
int b;
using namespace System::Runtime::InteropServices; // for class Marshal
char* buffer((char*)(void*)Marshal::StringToHGlobalAnsi(a));
x--; //So we write buffer[b] at data[0][b] when int x is 1
for(b = 0; b < 21; b++){
data[x][b] = buffer[b];
};
}
and when I tried to run and debug it, "An unhandled exception of type 'System.AccessViolationException'" occurred
Is there some easier/better way to put a String^ into a (2 dimensional) char array, and if not, what am I doing wrong here?
You should be calling .ToPointer() to convert the result of StringToHGlobalAnsi to something that you can then cast to char*.
You should also call FreeHGlobal on the result of StringToHGlobalAnsi (or you can recreate an IntPtr from your char*).
I'm trying to compile VC6 project with VC10...
I obtain an error C2678 with set_intersection: I wrote some example to understand. Can anybody explain how to compile this snippets ?
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
int main( )
{
using namespace std;
typedef set<string> MyType;
MyType in1, in2, out;
MyType::iterator out_iter(out.begin());
set_intersection(in1.begin(),in1.end(), in2.begin(), in2.end(), out_iter);
}
The output :
c:\program files\microsoft visual\studio 10.0\vc\include\algorithm(4494): error C2678: '=' binary : no operator defined which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
If I use a std::vector instead of std::set the compilation succeeded.
acceptable)
Try
set_intersection(in1.begin(),in1.end(), in2.begin(), in2.end(), inserter(out, out.begin()) );
This is because set_intersection wants to write to the output iterator, which causes the output container to grow in size. However this couldn't be done with just an iterator alone (it could be used to overwrite existing elements but not grow in size)
Edit: fixed the typo. Use inserter for adding to a set. A back_inserter only works for vectors and such.
Edit 2: fixed another typo. STL inserter requires a second argument which is a hint iterator to the likely insert position. Thanks chepseskaf.