why can't I print the copied string individually? [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
string s="1 23";
string a;
a[0]=s[2];a[1]=s[3];
cout<<a;
Here, I can't get output string a. But I can get all the individual elements by a[0].

Because a is empty, a[i] accesses it out of bounds for every possible i, causing undefined behavior.
Use a.push_back(s[i]) to add characters to a.

a is initialized as an empty string, so no memory is allocated for its characters, so when accessing it with [], you access unallocated memory, and that's undefined behavior.
One way to solve it is to create a as string with enough characters allocated. You can use the std::string fill constructor, that fills the string with a char of your choice:
std::string s = "1 23";
std::string a(s.size(), ' ');
This way you can put the characters in any index that exists in s.

Related

How memory allocated for string using new in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I am just wondering how memory handling is done for a string object in c++.
I have below code:
#include <iostream>
using namespace std;
int main() {
// your code goes here
string str = new char[30];
str = new char[60];
delete[] str;
return 0;
}
This piece of code will definitely throw an error, but can someone explain me in detail why this is an error ?
And will the above declaration assign a total of 90 bytes to str ?
Thanks in advance
std::string allocates it's memory internally. That's the whole point, it does the memory handling for you. So if you want to allocate a string of thirty characters you do it like this
string str(30, ' ');
This creates a string of 30 spaces. The memory allocated will automatically be deallocated when the string is no longer being used.
The string constructor your code used is actually meant to create C++ strings from C strings. E.g.
string str("hello");
This creates a string of length 5 and initialises it to 'hello'. Again any memory allocated will automatically be deallocated when the string is no longer used.
This is the point of the std::string class, to make things easy. Easier than you were prepared to believe apparently.

Getting initializer-string for char array is too long. Is this a compiler error? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
static_assert(0<decltype(AType::Id)::MaxIdLen);
using __type = char[decltype(AType::Id):: MaxIdLen + 10000];
[[maybe_unused]] __type aa = ""; //error initializer-string for char array is too long
I am getting this weird compiler error saying the initializer string is too long. But it is actually not. The first static_assert passed.
Has anyone seen such an issue before? I am using clang.
... compiler error saying the initializer string is too long. But it is actually not.
The compiler is not telling you that the string is too long to init aa. It's telling you that it's more data than it can handle, presumably because decltype(AType::Id)::MaxIdLen is a large value.
The compiler doesn't just store the string literal in the character array. It initialises the entire character array by using the string literal as a prefix, and padding the rest with zeros.

Can we make array of sets in c++? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Like the following way:
set<int> s[3];
I have tried it but it gives error in the line where I had tried to access its elements by writing s[i][j] where the error says
no match for 'operator[ ]'
The problem is not the array of std::set-s but rather the way you try to access elements inside your set.
std::set doesn't support operator [], that is why you are getting the error:
no match for 'operator[ ]'
Instead, access object using find() in the following way:
auto iter = s[i].find(<value>);
if (iter != s[i].end()) {
[..] // Do something with iter
}
Elements of a set are not accessed by index. s[i] is the (i-1)'th set but s[i][j] doesn't mean anything. You can check whether an element is present in the set using the find function. For eg s[i].find(3)!=s[i].end() . You can loop through the elements in sorted order using for(int x : s[i]){} (C++11 and above) or using iterators.

Error in Strings assignment. (C++) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I've looked everywhere and I've found one for php but not c++. I'm making a little console rpg for a project in my into to c++ class and I'd like to avoid functions and/or if possible--but if not I'll use them.
Sample code:
int main(){
string pName;
string pWpnName;
int pDamage=0;
int wdSwrdD=1;
if (pDamage==wdSwrdD)pWpnName="Wooden Sword";
cout<<"Please enter a name";
cin>>pName;
pDamage++:
cout<<"Name: "<<pName<<endl;
cout<<"Weapon: "<<pWpnName<<endl;
return 0}
But whenever I do this it outputs: Name: pName (like it's supposed to) and Weapon:. It just stays blank and I have a feeling it's something to do with how I'm using strings...
You do not understand basics of how imperative languages (and C++ is one of them) work. Program executed statement by statement, and your if condition checks pDamage==wdSwrdD only once - when execution flow goes through that statement. So the fact that you increase pDamage later will not magically change pWpnName (and you need to change comparison operator == to assignment operator = in that if condition in addition to that, but I assume this is a typo).
So you most probably need a loop where execution flow is repeatedly goes through your if statement (that's what loops are created for), but it is difficult to say anything more based on information you provided.
You can use the getline() function:
cout<<"Please enter a name"<<endl;
getline(cin, pName);
pDamage++;
The function can get a line from a stream, set std::cin as the steam argument, and assign a line input to a variable.
Your problem is that you've made a typo: == is equality comparison, while = is assignment. So, your section of code should be changed:
if (pDamage==wdSwrdD)
pWpnName=="Wooden Sword"; // here you're doing comparison
...to:
if (pDamage==wdSwrdD)
pWpnName="Wooden Sword"; // here you're doing assignment
Most compilers should generate a warning for this behavior because it's an easy typo to make, but can be difficult to catch.
In your program you have not initialized the strings and your are taking user input for pName and therefore it displays the name . In case pWpnName it's not initialized while declaring and the "if condition never becomes true" because you have initialized pDamage=0 and wdSwrD=1 and as we know if(0==1) is never true the string pWpnName never gets initialized to Wooden Sword so it displays blank.

If statement c++ with text [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
My code:
{
int reply;
cout<<"Am I doing something wrong: ";
cin>> reply;
if-part( reply == "yes") {
cout<<"Good";
}
}
Hi guys, I am newbie, I googled and youtubed the stuff, but i just can not find an answer.
Why is this code not running well if you text in if condition, but if you put number, everything is fine?
Thank you.
In your case you are comparing an integer with a pointer (address)
if-part( reply == "yes") {
Reply is a in value.
"yes" is a c-string, so it is roughly equivalent to
const char *yes = "yes"
where yes would be a pointer to the first byte in the string in y_e_s -- for example, 0x75243
so you are comparing an integer value to a pointer value such as 0x75243 and they are unlikely to ever be equal.
In java script this would work (better) since it converts types for you -- C++ does not, at least not like this.
reply is of type int and you are comparing it as a string.That's not possible.
You can simply use:
if (reply == 1)
cout << "Good\n";