I tried to write a code which gets an input from the user and concatenate with another string but it doesn't work well. The code is down below,
#include<iostream>
using namespace std;
int main() {
string s1="Hi ";
string s2;
cin>>s2;
s1=s1+s2
cout<<s1;
return 0;
}
Input:
this is how it works
Expected Output:
Hi this is how it works
But it didn't work as I expected. The output was:
Hi this
Can anybody help me?
'>>' reads space-delimited strings.
Now I found getline is used to read lines.
#include<iostream>
using namespace std;
int main() {
string s1="Hi ";
string s2;
getline(cin,s2);
s1=s1+s2;
cout<<s1;
return 0;
}
Now I get the desired output.
#include <iostream>
using namespace std;
int main()
{
string s1="hi ";
string s2;
cout << "Enter string s2: ";
getline (cin,s2);
s1 = s1 + s2;
cout << "concating both "<< s1;
return 0;
}
here use this! this should help!
Related
I created a program to pass the values from the file to the string "op", using the getline function and then print the values to the screen.
`
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string op;
while(getline(cin, op))
{
cout << op;
}
return 0;
}
`
But, cout does not print all values. The cout only prints all values if there is an "endl".
FILE:
`
1+2
2+3\5
`
INPUT WITHOUT endl:
2+3\5
INPUT WITH endl:
1+2
2+3\5
I'm using vs2019 and I have a problem the compiler give me an error however I cant solving that
please help me.
the error is : argument of type"const char*" is incompatible with parameter of type"char*"
Blockquote
*** #include <iostream>
#include<cstring>
using namespace std;
class part
{
public:
char partname[30];
int partnumber;
double cost;
void setpart(char pname[], int pn, double c)
{
strcpy_s(partname, pname);
partnumber = pn;
cost = c;
}
void showpart()const
{
cout << "\npartname : " << partname;
cout << "\npartnumber : " << partnumber;
cout << "\ncost($) : " << cost;
}
};
int main()
{
part part1, part2;
part1.setpart("handle bolt", 467, 4500);
part2.setpart("start lever", 549, 2300);
cout << "\nFirst part : "; part1.showpart();
cout << "\nSecond part : "; part2.showpart();
}***
The strings you are giving to setpart are const strings ( const char *) . But set part take a char * as parameter. Since pname will not be modified in your example you can replace void setpart(char pname[], int pn, double c) by void setpart(const char pname[], int pn, double c)
This code is giving error on 7th line?
why is *it=*rit not working?
string temp;
string::reverse_iterator rit;
rit = demo.rbegin();
string::iterator it;
it = temp.begin();
for (; rit != demo.rend(); rit++, it++)
{
*it = *rit;
}
To answer why your version doesn't work.
( I don't see a definition for demo - I assume its a non-empty string. )
temp is defined though, as an empty string.
Your loop checks "rit" but not "it" this could be OK if the 2 strings had the same length.
"it" is trying to iterate along an empty string, and that's why dereferencing it fails.
If temp was already a string with the correct length, then it could be iterated and edited successfully.
You could achieve this with
temp.resize(demo.length());
or just
temp = demo;
before the loop.
Try it like this:
#include <iostream>
#include <string>
#include <iterator>
int main()
{
std::string s = "Hello, world";
std::reverse_iterator<std::string::iterator> r = s.rbegin();
std::string rev(r, s.rend());
std::cout << rev << '\n';
}
Example
For example:
string MyString = "Normal\tString";
cout << MyString << endl;
produces the following: "Normal String"
Appending the raw string modifier to the string like so:
string MyString = R"(Normal\tString)";
cout << MyString << endl;
produces the following: "Normal\tString"
The Question
Is there a way to append the raw string modifier to a variable containing a string in order to print the raw form of the string contained within the variable?
string TestString = "Test\tString";
cout << R(TestString) << endl;
So you get: "Test\tString"
Is there a way to append the raw string modifier to a variable containing a string in order to print the raw form of the string contained within the variable?
No.
However, you can write a function that substitutes the characters that are defined by escape sequences by an appropriate string, i.e. replace the character '\t' by the string "\\t".
Sample program:
#include <iostream>
#include <string>
// Performs only one substitution of \t.
// Needs to be updated to do it for all occurrences of \t and
// all other escape sequences that can be found in raw strings.
std::string toRawString(std::string const& in)
{
std::string ret = in;
auto p = ret.find('\t');
if ( p != ret.npos )
{
ret.replace(p, 1, "\\t");
}
return ret;
}
int main()
{
std::string TestString = "Test\tString";
std::cout << toRawString(TestString) << std::endl;
}
Output:
Test\tString
This question is tagged as C++11, in which case rolling your own conversion function is probably the best call.
However, if you have a C++14 compiler, you can use the std::quoted stream manipulator:
#include <iostream>
#include <iomanip>
int main() {
string s = "Hello\tWorld!";
std::cout << std::quoted(s) << std::endl; // Prints "Hello\tWorld!"
}
Kind of. This works but it's not a pretty solution:
std::string strToInsert = " insert this "
std::string myRawString = R"(
raw string raw string raw string
raw string raw string raw string
)"; myRawString += strToInsert; myRawString += R"(raw string
raw string raw string raw string
)";
I have problem converting String^ containing 8 bytes as characters (as ascii) to double.
I want to take those 8 characters and convert them binary to double.
What would you recommend to do this conversion in C++/cli?
I was trying to use Marshal::Copy, Double::TryParse, etc.
Maybe I use wrong specifications of parameters, but I really lost my last hopes.
There must be something easy to do this conversion.
Thanks.
Well, the bad news is that the System.String class uses only Unicode encoding internally.
So if you give it bytes it will map them to its internal encoding hiding the original value.
The good news is that you can play with the System.Text.Encoding class to retrieve 8bits values corresponding to the unicode characters.
Here is a sample :
#include <iostream>
using namespace System;
using namespace System::Text;
int main()
{
int n = 123456;
double d = 123.456;
std::cout << n << std::endl;
std::cout << d << std::endl;
char* n_as_bytes = (char*)&n;
char* d_as_bytes = (char*)&d;
String^ n_as_string = gcnew String(n_as_bytes, 0, sizeof(n));
String^ d_as_string = gcnew String(d_as_bytes, 0, sizeof(d));
Encoding^ ascii = Encoding::GetEncoding("iso-8859-1");
array<Byte>^ n_as_array = ascii->GetBytes(n_as_string);
array<Byte>^ d_as_array = ascii->GetBytes(d_as_string);
cli::pin_ptr<unsigned char> pin_ptr_n = &n_as_array[0];
cli::pin_ptr<unsigned char> pin_ptr_d = &d_as_array[0];
unsigned char* ptr_n = pin_ptr_n;
unsigned char* ptr_d = pin_ptr_d;
int n_out = *(int*)ptr_n;
double d_out = *(double*)ptr_d;
std::cout << n_out << std::endl;
std::cout << d_out << std::endl;
return 0;
}
This should give you :
123456
123.456
123456
123.456
Not sure it is completely safe, but trying it in your context should be a good start to ensure it is viable. :)