c++ string.length() out of range as string.at() parameters












2















So I've been working on a program that invert it's characters. And I've doing it by string.length() and string.at(). But, it occur error "out of range" even though it's not out of range (I've checked it by print the pos of at variabele) I suspected it's caused of data types mismatch. Correct me if I'm wrong. This is my code.



 #include "pch.h"
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

string compose(string temp) {
size_t temp1 = 0, temp2 = temp.length() - 1;
string temporary, temporary1;
cout << temp2;
while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
temporary1 = temp.at(temp1);
temporary = temp.at(temp2);

temp.replace(temp1, 1, temporary);
temp.replace(temp2, 1, temporary1);
temp1++;
temp2--;
}
return temp;
}

int main()
{

cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjjnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
_getch();
return 0;
}









share|improve this question

























  • what errors do you get?

    – user463035818
    Nov 21 '18 at 12:33











  • Out of Range error

    – Harry76
    Nov 21 '18 at 12:34











  • sidenote: if you want to invert, why not simply std::copy(temp.rbegin(), temp.rend(), destination) ? or you are not allowed to use additional memory ?

    – Andrew Kashpur
    Nov 21 '18 at 12:35








  • 1





    length returns number of elements, at takes an index as parameter. When length is 3 then the max valid index is 2. I find it extremely hard to spot the error in your code as all variables have almost the same name, though as you already know it is an out-of-range, thats most likely the problem

    – user463035818
    Nov 21 '18 at 12:35













  • If you get out of range, you're out of range. Why would that have anything to do with "data types mismatch"?

    – Lightness Races in Orbit
    Nov 21 '18 at 12:48
















2















So I've been working on a program that invert it's characters. And I've doing it by string.length() and string.at(). But, it occur error "out of range" even though it's not out of range (I've checked it by print the pos of at variabele) I suspected it's caused of data types mismatch. Correct me if I'm wrong. This is my code.



 #include "pch.h"
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

string compose(string temp) {
size_t temp1 = 0, temp2 = temp.length() - 1;
string temporary, temporary1;
cout << temp2;
while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
temporary1 = temp.at(temp1);
temporary = temp.at(temp2);

temp.replace(temp1, 1, temporary);
temp.replace(temp2, 1, temporary1);
temp1++;
temp2--;
}
return temp;
}

int main()
{

cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjjnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
_getch();
return 0;
}









share|improve this question

























  • what errors do you get?

    – user463035818
    Nov 21 '18 at 12:33











  • Out of Range error

    – Harry76
    Nov 21 '18 at 12:34











  • sidenote: if you want to invert, why not simply std::copy(temp.rbegin(), temp.rend(), destination) ? or you are not allowed to use additional memory ?

    – Andrew Kashpur
    Nov 21 '18 at 12:35








  • 1





    length returns number of elements, at takes an index as parameter. When length is 3 then the max valid index is 2. I find it extremely hard to spot the error in your code as all variables have almost the same name, though as you already know it is an out-of-range, thats most likely the problem

    – user463035818
    Nov 21 '18 at 12:35













  • If you get out of range, you're out of range. Why would that have anything to do with "data types mismatch"?

    – Lightness Races in Orbit
    Nov 21 '18 at 12:48














2












2








2








So I've been working on a program that invert it's characters. And I've doing it by string.length() and string.at(). But, it occur error "out of range" even though it's not out of range (I've checked it by print the pos of at variabele) I suspected it's caused of data types mismatch. Correct me if I'm wrong. This is my code.



 #include "pch.h"
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

string compose(string temp) {
size_t temp1 = 0, temp2 = temp.length() - 1;
string temporary, temporary1;
cout << temp2;
while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
temporary1 = temp.at(temp1);
temporary = temp.at(temp2);

temp.replace(temp1, 1, temporary);
temp.replace(temp2, 1, temporary1);
temp1++;
temp2--;
}
return temp;
}

int main()
{

cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjjnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
_getch();
return 0;
}









share|improve this question
















So I've been working on a program that invert it's characters. And I've doing it by string.length() and string.at(). But, it occur error "out of range" even though it's not out of range (I've checked it by print the pos of at variabele) I suspected it's caused of data types mismatch. Correct me if I'm wrong. This is my code.



 #include "pch.h"
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

string compose(string temp) {
size_t temp1 = 0, temp2 = temp.length() - 1;
string temporary, temporary1;
cout << temp2;
while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
temporary1 = temp.at(temp1);
temporary = temp.at(temp2);

temp.replace(temp1, 1, temporary);
temp.replace(temp2, 1, temporary1);
temp1++;
temp2--;
}
return temp;
}

int main()
{

cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjjnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
_getch();
return 0;
}






c++ string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 12:33







Harry76

















asked Nov 21 '18 at 12:30









Harry76Harry76

264




264













  • what errors do you get?

    – user463035818
    Nov 21 '18 at 12:33











  • Out of Range error

    – Harry76
    Nov 21 '18 at 12:34











  • sidenote: if you want to invert, why not simply std::copy(temp.rbegin(), temp.rend(), destination) ? or you are not allowed to use additional memory ?

    – Andrew Kashpur
    Nov 21 '18 at 12:35








  • 1





    length returns number of elements, at takes an index as parameter. When length is 3 then the max valid index is 2. I find it extremely hard to spot the error in your code as all variables have almost the same name, though as you already know it is an out-of-range, thats most likely the problem

    – user463035818
    Nov 21 '18 at 12:35













  • If you get out of range, you're out of range. Why would that have anything to do with "data types mismatch"?

    – Lightness Races in Orbit
    Nov 21 '18 at 12:48



















  • what errors do you get?

    – user463035818
    Nov 21 '18 at 12:33











  • Out of Range error

    – Harry76
    Nov 21 '18 at 12:34











  • sidenote: if you want to invert, why not simply std::copy(temp.rbegin(), temp.rend(), destination) ? or you are not allowed to use additional memory ?

    – Andrew Kashpur
    Nov 21 '18 at 12:35








  • 1





    length returns number of elements, at takes an index as parameter. When length is 3 then the max valid index is 2. I find it extremely hard to spot the error in your code as all variables have almost the same name, though as you already know it is an out-of-range, thats most likely the problem

    – user463035818
    Nov 21 '18 at 12:35













  • If you get out of range, you're out of range. Why would that have anything to do with "data types mismatch"?

    – Lightness Races in Orbit
    Nov 21 '18 at 12:48

















what errors do you get?

– user463035818
Nov 21 '18 at 12:33





what errors do you get?

– user463035818
Nov 21 '18 at 12:33













Out of Range error

– Harry76
Nov 21 '18 at 12:34





Out of Range error

– Harry76
Nov 21 '18 at 12:34













sidenote: if you want to invert, why not simply std::copy(temp.rbegin(), temp.rend(), destination) ? or you are not allowed to use additional memory ?

– Andrew Kashpur
Nov 21 '18 at 12:35







sidenote: if you want to invert, why not simply std::copy(temp.rbegin(), temp.rend(), destination) ? or you are not allowed to use additional memory ?

– Andrew Kashpur
Nov 21 '18 at 12:35






1




1





length returns number of elements, at takes an index as parameter. When length is 3 then the max valid index is 2. I find it extremely hard to spot the error in your code as all variables have almost the same name, though as you already know it is an out-of-range, thats most likely the problem

– user463035818
Nov 21 '18 at 12:35







length returns number of elements, at takes an index as parameter. When length is 3 then the max valid index is 2. I find it extremely hard to spot the error in your code as all variables have almost the same name, though as you already know it is an out-of-range, thats most likely the problem

– user463035818
Nov 21 '18 at 12:35















If you get out of range, you're out of range. Why would that have anything to do with "data types mismatch"?

– Lightness Races in Orbit
Nov 21 '18 at 12:48





If you get out of range, you're out of range. Why would that have anything to do with "data types mismatch"?

– Lightness Races in Orbit
Nov 21 '18 at 12:48












3 Answers
3






active

oldest

votes


















2














Change this:



while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1)


to:



while (temp2 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1)




Explanation:




I've checked it by print the pos of at variabele




Your check was insufficient. I did a:



cout << temp1 << " " << temp2 << endl;


at the first line of the body of the while loop, and it was obvious that temp2 was underflowing, which explains the out of range error.



What you did was to force a size_t (imagine it as an unsigned integer) variable go below 0. Read more in Question about C behaviour for unsigned integer underflow.



In order to fix this, you need to understand what allows your temp2 to underflow. At the end of your loop, you do temp2--;,, which decrements temp2 by 1.



You should control how many times this gets executed, by having a stop condition in your while loop. Thats approach allows us to focus on the stop condition in the while loop.



With an input like "bar", and not that big string you use there, you can easily see, that by the time it gets inverted into "rab", temp1 is equal to 2 and temp2 is equal to 0.



If you allow the body of the loop of to get executed once more, you will then allow temp2 to underflow, invoking Undefined Behavior (UB), when you try to use temp2.



Checking the stop condition and the values of your counters, you can see that you need to change the first operand of the OR condition, as explained above.





Homework: Think how you can simplify your stop condition. Take a small string as an example input and see how counters temp1 and temp2 update themselves.



Hint: One condition will suffice (you do not need two conditions paired with a logical OR).





PS: Since we discussed about underflow, it should be natural now for someone to think that temp must be ensured that is not empty. If it is, then this temp2 = temp.length() - 1; would result in temp2 to underflow!



So, modify your function to do in its first line if(temp.length() == 0) return "";.






share|improve this answer





















  • 1





    Thanks! Managed to fix it! I thought it's error is on the string operation.

    – Harry76
    Nov 21 '18 at 12:52











  • @Harry76 you are welcome! Nice question, you got an upvote for providing a complete and minimal example.

    – gsamaras
    Nov 21 '18 at 12:54





















2














The error is here:



while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
^-- should be -1


But actually the check itself is wrong. In theory correct would be:



while (temp1 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1) {
^-- change here


But even this is too much. Just checking



while (temp1 < temp.length() / 2 - 1)


Should be enough. The reason is you have temp1 and temp2 where one starts from 0 and the other one from the end of the string. Each step you increase temp1 and decrease temp2. You have to distinguish 2 cases:




  • string length is even

  • string length is odd


But for both cases it's enough to just check one of the variables if they are about to pass halfway through the string (even length string) or about to hit the middle character (odd length string) where a switch is pointless.



And as Lightness Races in Orbit mentions in the comments you have to make sure the passed string is not empty. But in that case you can just return.






share|improve this answer





















  • 1





    You'll need to check that temp is not empty otherwise this will explode

    – Lightness Races in Orbit
    Nov 21 '18 at 12:49











  • Thanks! Managed to fix it! I thought it's error is on the string operation.

    – Harry76
    Nov 21 '18 at 12:52











  • @LightnessRacesinOrbit thanks, added it to the answer

    – Philipp
    Nov 21 '18 at 12:54



















0














This one should be enough (little bit KISS):



#include <iostream>
#include <string>

using namespace std;

string compose(string temp) {
size_t temp1 = 0, temp2 = temp.length() - 1;
string temporary, temporary1;

size_t size = temp.length() /2;
size = size %2 == 0? size: size -1;
while (temp1 <= size) {
temporary1 = temp.at(temp1);
temporary = temp.at(temp2);

temp.replace(temp1, 1, temporary);
temp.replace(temp2, 1, temporary1);
temp1++;
temp2--;
}
return temp;
}

int main()
{

cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjj123456789");
cout << compose("1234");
cout << compose("12345");
return 0;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412090%2fc-string-length-out-of-range-as-string-at-parameters%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Change this:



    while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1)


    to:



    while (temp2 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1)




    Explanation:




    I've checked it by print the pos of at variabele




    Your check was insufficient. I did a:



    cout << temp1 << " " << temp2 << endl;


    at the first line of the body of the while loop, and it was obvious that temp2 was underflowing, which explains the out of range error.



    What you did was to force a size_t (imagine it as an unsigned integer) variable go below 0. Read more in Question about C behaviour for unsigned integer underflow.



    In order to fix this, you need to understand what allows your temp2 to underflow. At the end of your loop, you do temp2--;,, which decrements temp2 by 1.



    You should control how many times this gets executed, by having a stop condition in your while loop. Thats approach allows us to focus on the stop condition in the while loop.



    With an input like "bar", and not that big string you use there, you can easily see, that by the time it gets inverted into "rab", temp1 is equal to 2 and temp2 is equal to 0.



    If you allow the body of the loop of to get executed once more, you will then allow temp2 to underflow, invoking Undefined Behavior (UB), when you try to use temp2.



    Checking the stop condition and the values of your counters, you can see that you need to change the first operand of the OR condition, as explained above.





    Homework: Think how you can simplify your stop condition. Take a small string as an example input and see how counters temp1 and temp2 update themselves.



    Hint: One condition will suffice (you do not need two conditions paired with a logical OR).





    PS: Since we discussed about underflow, it should be natural now for someone to think that temp must be ensured that is not empty. If it is, then this temp2 = temp.length() - 1; would result in temp2 to underflow!



    So, modify your function to do in its first line if(temp.length() == 0) return "";.






    share|improve this answer





















    • 1





      Thanks! Managed to fix it! I thought it's error is on the string operation.

      – Harry76
      Nov 21 '18 at 12:52











    • @Harry76 you are welcome! Nice question, you got an upvote for providing a complete and minimal example.

      – gsamaras
      Nov 21 '18 at 12:54


















    2














    Change this:



    while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1)


    to:



    while (temp2 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1)




    Explanation:




    I've checked it by print the pos of at variabele




    Your check was insufficient. I did a:



    cout << temp1 << " " << temp2 << endl;


    at the first line of the body of the while loop, and it was obvious that temp2 was underflowing, which explains the out of range error.



    What you did was to force a size_t (imagine it as an unsigned integer) variable go below 0. Read more in Question about C behaviour for unsigned integer underflow.



    In order to fix this, you need to understand what allows your temp2 to underflow. At the end of your loop, you do temp2--;,, which decrements temp2 by 1.



    You should control how many times this gets executed, by having a stop condition in your while loop. Thats approach allows us to focus on the stop condition in the while loop.



    With an input like "bar", and not that big string you use there, you can easily see, that by the time it gets inverted into "rab", temp1 is equal to 2 and temp2 is equal to 0.



    If you allow the body of the loop of to get executed once more, you will then allow temp2 to underflow, invoking Undefined Behavior (UB), when you try to use temp2.



    Checking the stop condition and the values of your counters, you can see that you need to change the first operand of the OR condition, as explained above.





    Homework: Think how you can simplify your stop condition. Take a small string as an example input and see how counters temp1 and temp2 update themselves.



    Hint: One condition will suffice (you do not need two conditions paired with a logical OR).





    PS: Since we discussed about underflow, it should be natural now for someone to think that temp must be ensured that is not empty. If it is, then this temp2 = temp.length() - 1; would result in temp2 to underflow!



    So, modify your function to do in its first line if(temp.length() == 0) return "";.






    share|improve this answer





















    • 1





      Thanks! Managed to fix it! I thought it's error is on the string operation.

      – Harry76
      Nov 21 '18 at 12:52











    • @Harry76 you are welcome! Nice question, you got an upvote for providing a complete and minimal example.

      – gsamaras
      Nov 21 '18 at 12:54
















    2












    2








    2







    Change this:



    while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1)


    to:



    while (temp2 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1)




    Explanation:




    I've checked it by print the pos of at variabele




    Your check was insufficient. I did a:



    cout << temp1 << " " << temp2 << endl;


    at the first line of the body of the while loop, and it was obvious that temp2 was underflowing, which explains the out of range error.



    What you did was to force a size_t (imagine it as an unsigned integer) variable go below 0. Read more in Question about C behaviour for unsigned integer underflow.



    In order to fix this, you need to understand what allows your temp2 to underflow. At the end of your loop, you do temp2--;,, which decrements temp2 by 1.



    You should control how many times this gets executed, by having a stop condition in your while loop. Thats approach allows us to focus on the stop condition in the while loop.



    With an input like "bar", and not that big string you use there, you can easily see, that by the time it gets inverted into "rab", temp1 is equal to 2 and temp2 is equal to 0.



    If you allow the body of the loop of to get executed once more, you will then allow temp2 to underflow, invoking Undefined Behavior (UB), when you try to use temp2.



    Checking the stop condition and the values of your counters, you can see that you need to change the first operand of the OR condition, as explained above.





    Homework: Think how you can simplify your stop condition. Take a small string as an example input and see how counters temp1 and temp2 update themselves.



    Hint: One condition will suffice (you do not need two conditions paired with a logical OR).





    PS: Since we discussed about underflow, it should be natural now for someone to think that temp must be ensured that is not empty. If it is, then this temp2 = temp.length() - 1; would result in temp2 to underflow!



    So, modify your function to do in its first line if(temp.length() == 0) return "";.






    share|improve this answer















    Change this:



    while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1)


    to:



    while (temp2 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1)




    Explanation:




    I've checked it by print the pos of at variabele




    Your check was insufficient. I did a:



    cout << temp1 << " " << temp2 << endl;


    at the first line of the body of the while loop, and it was obvious that temp2 was underflowing, which explains the out of range error.



    What you did was to force a size_t (imagine it as an unsigned integer) variable go below 0. Read more in Question about C behaviour for unsigned integer underflow.



    In order to fix this, you need to understand what allows your temp2 to underflow. At the end of your loop, you do temp2--;,, which decrements temp2 by 1.



    You should control how many times this gets executed, by having a stop condition in your while loop. Thats approach allows us to focus on the stop condition in the while loop.



    With an input like "bar", and not that big string you use there, you can easily see, that by the time it gets inverted into "rab", temp1 is equal to 2 and temp2 is equal to 0.



    If you allow the body of the loop of to get executed once more, you will then allow temp2 to underflow, invoking Undefined Behavior (UB), when you try to use temp2.



    Checking the stop condition and the values of your counters, you can see that you need to change the first operand of the OR condition, as explained above.





    Homework: Think how you can simplify your stop condition. Take a small string as an example input and see how counters temp1 and temp2 update themselves.



    Hint: One condition will suffice (you do not need two conditions paired with a logical OR).





    PS: Since we discussed about underflow, it should be natural now for someone to think that temp must be ensured that is not empty. If it is, then this temp2 = temp.length() - 1; would result in temp2 to underflow!



    So, modify your function to do in its first line if(temp.length() == 0) return "";.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 13:07

























    answered Nov 21 '18 at 12:43









    gsamarasgsamaras

    51.2k24100187




    51.2k24100187








    • 1





      Thanks! Managed to fix it! I thought it's error is on the string operation.

      – Harry76
      Nov 21 '18 at 12:52











    • @Harry76 you are welcome! Nice question, you got an upvote for providing a complete and minimal example.

      – gsamaras
      Nov 21 '18 at 12:54
















    • 1





      Thanks! Managed to fix it! I thought it's error is on the string operation.

      – Harry76
      Nov 21 '18 at 12:52











    • @Harry76 you are welcome! Nice question, you got an upvote for providing a complete and minimal example.

      – gsamaras
      Nov 21 '18 at 12:54










    1




    1





    Thanks! Managed to fix it! I thought it's error is on the string operation.

    – Harry76
    Nov 21 '18 at 12:52





    Thanks! Managed to fix it! I thought it's error is on the string operation.

    – Harry76
    Nov 21 '18 at 12:52













    @Harry76 you are welcome! Nice question, you got an upvote for providing a complete and minimal example.

    – gsamaras
    Nov 21 '18 at 12:54







    @Harry76 you are welcome! Nice question, you got an upvote for providing a complete and minimal example.

    – gsamaras
    Nov 21 '18 at 12:54















    2














    The error is here:



    while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
    ^-- should be -1


    But actually the check itself is wrong. In theory correct would be:



    while (temp1 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1) {
    ^-- change here


    But even this is too much. Just checking



    while (temp1 < temp.length() / 2 - 1)


    Should be enough. The reason is you have temp1 and temp2 where one starts from 0 and the other one from the end of the string. Each step you increase temp1 and decrease temp2. You have to distinguish 2 cases:




    • string length is even

    • string length is odd


    But for both cases it's enough to just check one of the variables if they are about to pass halfway through the string (even length string) or about to hit the middle character (odd length string) where a switch is pointless.



    And as Lightness Races in Orbit mentions in the comments you have to make sure the passed string is not empty. But in that case you can just return.






    share|improve this answer





















    • 1





      You'll need to check that temp is not empty otherwise this will explode

      – Lightness Races in Orbit
      Nov 21 '18 at 12:49











    • Thanks! Managed to fix it! I thought it's error is on the string operation.

      – Harry76
      Nov 21 '18 at 12:52











    • @LightnessRacesinOrbit thanks, added it to the answer

      – Philipp
      Nov 21 '18 at 12:54
















    2














    The error is here:



    while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
    ^-- should be -1


    But actually the check itself is wrong. In theory correct would be:



    while (temp1 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1) {
    ^-- change here


    But even this is too much. Just checking



    while (temp1 < temp.length() / 2 - 1)


    Should be enough. The reason is you have temp1 and temp2 where one starts from 0 and the other one from the end of the string. Each step you increase temp1 and decrease temp2. You have to distinguish 2 cases:




    • string length is even

    • string length is odd


    But for both cases it's enough to just check one of the variables if they are about to pass halfway through the string (even length string) or about to hit the middle character (odd length string) where a switch is pointless.



    And as Lightness Races in Orbit mentions in the comments you have to make sure the passed string is not empty. But in that case you can just return.






    share|improve this answer





















    • 1





      You'll need to check that temp is not empty otherwise this will explode

      – Lightness Races in Orbit
      Nov 21 '18 at 12:49











    • Thanks! Managed to fix it! I thought it's error is on the string operation.

      – Harry76
      Nov 21 '18 at 12:52











    • @LightnessRacesinOrbit thanks, added it to the answer

      – Philipp
      Nov 21 '18 at 12:54














    2












    2








    2







    The error is here:



    while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
    ^-- should be -1


    But actually the check itself is wrong. In theory correct would be:



    while (temp1 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1) {
    ^-- change here


    But even this is too much. Just checking



    while (temp1 < temp.length() / 2 - 1)


    Should be enough. The reason is you have temp1 and temp2 where one starts from 0 and the other one from the end of the string. Each step you increase temp1 and decrease temp2. You have to distinguish 2 cases:




    • string length is even

    • string length is odd


    But for both cases it's enough to just check one of the variables if they are about to pass halfway through the string (even length string) or about to hit the middle character (odd length string) where a switch is pointless.



    And as Lightness Races in Orbit mentions in the comments you have to make sure the passed string is not empty. But in that case you can just return.






    share|improve this answer















    The error is here:



    while (temp2 < temp.length() / 2 + 1 || temp2 > temp.length() / 2 - 1) {
    ^-- should be -1


    But actually the check itself is wrong. In theory correct would be:



    while (temp1 < temp.length() / 2 - 1 || temp2 > temp.length() / 2 - 1) {
    ^-- change here


    But even this is too much. Just checking



    while (temp1 < temp.length() / 2 - 1)


    Should be enough. The reason is you have temp1 and temp2 where one starts from 0 and the other one from the end of the string. Each step you increase temp1 and decrease temp2. You have to distinguish 2 cases:




    • string length is even

    • string length is odd


    But for both cases it's enough to just check one of the variables if they are about to pass halfway through the string (even length string) or about to hit the middle character (odd length string) where a switch is pointless.



    And as Lightness Races in Orbit mentions in the comments you have to make sure the passed string is not empty. But in that case you can just return.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 12:49

























    answered Nov 21 '18 at 12:41









    PhilippPhilipp

    86631434




    86631434








    • 1





      You'll need to check that temp is not empty otherwise this will explode

      – Lightness Races in Orbit
      Nov 21 '18 at 12:49











    • Thanks! Managed to fix it! I thought it's error is on the string operation.

      – Harry76
      Nov 21 '18 at 12:52











    • @LightnessRacesinOrbit thanks, added it to the answer

      – Philipp
      Nov 21 '18 at 12:54














    • 1





      You'll need to check that temp is not empty otherwise this will explode

      – Lightness Races in Orbit
      Nov 21 '18 at 12:49











    • Thanks! Managed to fix it! I thought it's error is on the string operation.

      – Harry76
      Nov 21 '18 at 12:52











    • @LightnessRacesinOrbit thanks, added it to the answer

      – Philipp
      Nov 21 '18 at 12:54








    1




    1





    You'll need to check that temp is not empty otherwise this will explode

    – Lightness Races in Orbit
    Nov 21 '18 at 12:49





    You'll need to check that temp is not empty otherwise this will explode

    – Lightness Races in Orbit
    Nov 21 '18 at 12:49













    Thanks! Managed to fix it! I thought it's error is on the string operation.

    – Harry76
    Nov 21 '18 at 12:52





    Thanks! Managed to fix it! I thought it's error is on the string operation.

    – Harry76
    Nov 21 '18 at 12:52













    @LightnessRacesinOrbit thanks, added it to the answer

    – Philipp
    Nov 21 '18 at 12:54





    @LightnessRacesinOrbit thanks, added it to the answer

    – Philipp
    Nov 21 '18 at 12:54











    0














    This one should be enough (little bit KISS):



    #include <iostream>
    #include <string>

    using namespace std;

    string compose(string temp) {
    size_t temp1 = 0, temp2 = temp.length() - 1;
    string temporary, temporary1;

    size_t size = temp.length() /2;
    size = size %2 == 0? size: size -1;
    while (temp1 <= size) {
    temporary1 = temp.at(temp1);
    temporary = temp.at(temp2);

    temp.replace(temp1, 1, temporary);
    temp.replace(temp2, 1, temporary1);
    temp1++;
    temp2--;
    }
    return temp;
    }

    int main()
    {

    cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjj123456789");
    cout << compose("1234");
    cout << compose("12345");
    return 0;
    }





    share|improve this answer




























      0














      This one should be enough (little bit KISS):



      #include <iostream>
      #include <string>

      using namespace std;

      string compose(string temp) {
      size_t temp1 = 0, temp2 = temp.length() - 1;
      string temporary, temporary1;

      size_t size = temp.length() /2;
      size = size %2 == 0? size: size -1;
      while (temp1 <= size) {
      temporary1 = temp.at(temp1);
      temporary = temp.at(temp2);

      temp.replace(temp1, 1, temporary);
      temp.replace(temp2, 1, temporary1);
      temp1++;
      temp2--;
      }
      return temp;
      }

      int main()
      {

      cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjj123456789");
      cout << compose("1234");
      cout << compose("12345");
      return 0;
      }





      share|improve this answer


























        0












        0








        0







        This one should be enough (little bit KISS):



        #include <iostream>
        #include <string>

        using namespace std;

        string compose(string temp) {
        size_t temp1 = 0, temp2 = temp.length() - 1;
        string temporary, temporary1;

        size_t size = temp.length() /2;
        size = size %2 == 0? size: size -1;
        while (temp1 <= size) {
        temporary1 = temp.at(temp1);
        temporary = temp.at(temp2);

        temp.replace(temp1, 1, temporary);
        temp.replace(temp2, 1, temporary1);
        temp1++;
        temp2--;
        }
        return temp;
        }

        int main()
        {

        cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjj123456789");
        cout << compose("1234");
        cout << compose("12345");
        return 0;
        }





        share|improve this answer













        This one should be enough (little bit KISS):



        #include <iostream>
        #include <string>

        using namespace std;

        string compose(string temp) {
        size_t temp1 = 0, temp2 = temp.length() - 1;
        string temporary, temporary1;

        size_t size = temp.length() /2;
        size = size %2 == 0? size: size -1;
        while (temp1 <= size) {
        temporary1 = temp.at(temp1);
        temporary = temp.at(temp2);

        temp.replace(temp1, 1, temporary);
        temp.replace(temp2, 1, temporary1);
        temp1++;
        temp2--;
        }
        return temp;
        }

        int main()
        {

        cout << compose("RPL X-1okewphevoiwrwrejfnjjjjjjjjjjjjjjjjj123456789");
        cout << compose("1234");
        cout << compose("12345");
        return 0;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 12:55









        Alexander CherninAlexander Chernin

        307210




        307210






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412090%2fc-string-length-out-of-range-as-string-at-parameters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]