0

The task is interchange two parts of a word, which contains the dash (i.e we have 1237-456 but should transform it into 456-1237). Here`s my code, it runs but doesnt shows results as a string is out of range and i dk why. It happens in the 1st for, the second iteration ends in the error+ it happens when strlen is 5 and more. The code:

    #include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    int u = 0, y = 0;
    string first, second;
    int i = 0;
    string word;
    cout << "Enter the text: " << endl;
    getline(cin, word);
    int l = size(word);
    int f = word.find('-');
    cout << "The word has " << l << " characters" << endl << endl;
    for (int i = 0; i < f; i++) {
        first[i] = word[i];
    }
    for (int i = f + 1; i < l; i++) {
        second[y] = word[i];
        y++;
    }
    cout << endl << second << " - " << first << endl;
}
Paw Max1
  • 17
  • 3
  • I don't see where in the code you initialize the length of `first` and `second`, so they still have their initial length of `0`. Trying to access any elements of an empty string seems wrong – PeterT Nov 19 '21 at 16:12
  • `first` and `second` are empty strings, thus `first[i]` and `second[y]` are illegal. You should use `.push_back()`. I suppose it is taught in any C++ book. – 273K Nov 19 '21 at 16:13

2 Answers2

1

first and second will not have memory allocated to them. They are initialized as strings of size 0. And for this case I would just use iterators instead of indices (though they could work too, but then you need more manual work to allocate enough room for the target strings and all). All in all I think your code is mixing 'c' and 'c++' style a bit so here is my example:

#include <algorithm> // for find
#include <iostream>

// #include <cstdlib> // <<== this is "c" not C++
// using namespace std; <<== unlearn this

int main()
{
    std::string word{ "Mississippi-delta"};

    // std::string has a lenght function use that
    std::cout << "The word has " << word.length() << " characters\n";
    
    // "it" will be an iterator to the location of '-' (if any)
    auto it = std::find(word.begin(), word.end(), '-');
    
    // it points (beyond) the end of the word if no '-' is found
    if (it == word.end())
    {
        std::cout << "no '-' found in word";
    }
    else
    {
        std::string first{ word.begin(),it };
        ++it; // skip '-'
        std::string second{ it,word.end() };

        std::cout << second << "-" << first << "\n";        
    }

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
  • Although im a newbie, you see, i have read that using std is a bad sign but havent understood why and as for now i think i should learn principles. And only then write withot std or start urgently? – Paw Max1 Nov 19 '21 at 16:33
  • @PawMax1 : [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – user4581301 Nov 19 '21 at 17:01
  • Better learn things now then later, specialy if they are easy like typing std::. It is harder to unlearn patterns then to learn them. Good things to learn early : use of std::vector/std::array (over 'C' style arrays), avoid new/delete, use references (prefer over pointers), abstract base classes (interfaces). And don't try to solve everything with inheritance, composition is OO too. (I know this is a lot just keep it in the back of your mind while learning C++). And never forget, done right programming is fun! – Pepijn Kramer Nov 20 '21 at 04:44
  • Could you pls give some books to learn for a newbie cos i dk from which to strart – Paw Max1 Nov 21 '21 at 13:26
  • To be honest I wouldn't know any good books. Its been too long ago for me that I read any C++ (beginner) books. I started coding C++ in 1995 ;) I did see this video of Jason Turner a while back that's very good though : https://www.youtube.com/watch?v=iz5Qx18H6lg. It touches many important things – Pepijn Kramer Nov 21 '21 at 16:29
0

Instead of accessing the elements of first and second, just try using .push_back() to add characters from word.

thelaw
  • 1
  • 3