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.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Annotation:
In my code I have edited the string at an adneren position, this I have not had in my example.
Since here the actual error cause/problem lay I have adapted the example, see example below.
The Answer should be:
std::string.resize increases the memory size of the string to the maximum expected size.
replace inserts the entire size into str0.
A solution is to execute resize only if str1.lengh > max_len.
Original question:
I would like to insert a string (str1) into another (str0), at any position (n).
If str1 is shorter than the rest of string str0 (n -> str0.end()), the rest of str0 should remain.
For this I wanted to use the std::string::replace() function.
#include <string>
#include <stdint.h>
#include <iostream>
int main()
{
std::string str0 = "ABCDEFGHIJ";
std::string str1 = "000";
uint16_t n = 3;
str0.replace(n, str1.length(), str1);
std::cout << "TEST:" << str0 << std::endl;
return 0;
}
In my example, I expect to output "TEST: ABC000GHIJ" via cout.
However, "TEST: ABC000" is output.
Where is the problem that the string will be cutted?
Correct code example with the error cause:
#include <string>
#include <stdint.h>
#include <iostream>
#define max_len 20
int main()
{
std::string str0 = "ABDCDEF";
std::string str1 = "";
uint16_t n = 3;
std::getline(std::cin, str1);
if(!str1.empty() && str1[str1.size()-1] == '\n') {
str1.erase(str1.size()-1);
}
str1.resize(max_len);
str0.replace(n, str1.length(), str1);
std::cout << "TEST:" << str0 << std::endl;
return 0;
}
Related
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'm trying to append some characters from one string to another, but I couldn't do it. I tried something like this:
std::string fooz = "fooz";
std::string foo;
int i = 0;
while(i< fooz.length()){
if(fooz[i] != 'z'){
foo.push_back(fooz[i]);
}
i++;
}
foo after the while its empty.
You're taking length from the target string, which is still empty and the while loop won't be executed at all.
Change
while(i< foo.length()){
to
while(i< fooz.length()){
The STL can help you in this kind of scenarios.
This one uses the remove algorithm which provide a range of element to erase.
#include <string>
#include <iostream>
#include <algorithm>
int main()
{
std::string str("aaazbbb");
std::cout << str << std::endl;
str.erase(std::remove(str.begin(), str.end(), 'z'), str.end());
std::cout << str << std::endl;
}
std::string fooz = "fooz";
std::string foo;
int i = 0;
int len=fooz.size();
while(i< len){
if(fooz[i] != 'z'){
foo.push_back(fooz[i]);
}
i++;
}
Don not call std::string.size() or length() in while loop.
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
#include <iostream>
#include <string>
#include <sstream>
#include <string>
int coins = 0;
std::stringstream ss;
ss << 100 << ' ' << 200;
When I hover over ss I get the error "declaration has no storage class or type specifier" and when I hover over << I get the error expected a ;".
C++ does not allow executable statements outside functions.
The first two lines are declarations; they are allowed, although I doubt that you made them global on purpose. The last line, however, must be placed inside a function, e.g. main:
int main() {
int coins = 0;
std::stringstream ss;
ss << 100 << ' ' << 200;
}
These can be outside of a function:
int coins = 0;
std::stringstream ss;
However this can't:
ss << 100 << ' ' << 200;
So, put it inside main() or any other function.
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
I made a function that will reverse the string, but the output of the reversed string always shifts towards the right by one character.
#include <iostream>
#include <string>
using namespace std;
void reverse(string string1)
{
cout << endl;
for (int i = string1.size(); i >= 0; i--)
{
cout << string1[i];
}
cout << endl;
}
int main()
{
string string1;
getline(cin, string1);
reverse(string1);
system("pause");
return 0;
}
Your first output is of a character that does not exist.
std::string's leaky abstraction means that your first iteration is printing '\0', which apparently looks like a space in your configuration.
Begin at string1.size() - 1.
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 8 years ago.
Improve this question
I have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.
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 8 years ago.
Improve this question
Whenever I try to run this program it returns an error saying:
no operator "<<" matches these operands
Also note that the program only runs into this problem in the getChoice() function.
#include <iostream>
#include "utilities.h"
using namespace std;
int getChoice(string inChoices[]){
int numOfChoices = sizeof(inChoices) / sizeof(inChoices[0]);
string x = inChoices[0];
string y = inChoices[1];
cout << x << endl << y << endl;
return numOfChoices;
}
int main()
{
string choices[2] = { "Happy Day", "Even Better Day" };
cout << utilities::getChoice(choices) << endl;
cout << endl << sizeof(choices) / sizeof(choices[0]) << endl;
}
You need also to include the string header:
#include <string>
You need to #include <string>
And your calculation of numOfChoices in getChoice() is wrong, since the parameter inChoices is actually a "pointer to string" instead of "array of strings".