C++ Function Problem

Everything todo with programming goes HERE.
Post Reply
niin
Average Program
Posts: 86
Joined: Fri Dec 28, 2012 4:36 pm
Location: Germany

C++ Function Problem

Post by niin »

Code: Select all

#include <iostream>

using namespace std;

int TextEinfueg(char *Einfueg1, char *Einfueg2, int i)
{
    char *ptr1 = Einfueg1;
    char *ptr2 = Einfueg2;
    //int *Posi = i;
    int k = 0;
    int l = 0;

    char temp[256];

    for(int j = 0; j <= 255; j++)
    {
        temp[j] = ptr1[j];
    }

    int textlaenge = 0;
    for(int j = 0; ptr2[j] != '\0'; j++)
    {
        textlaenge++;
    }

    int textlaenge2 = 0;
    for(int j = 0; ptr1[j] != '\0'; j++)
    {
        textlaenge2++;
    }

    for(k; k <= textlaenge+textlaenge2; k++)
    {
        if(k == i-1)
        {
            for(l; l <= textlaenge-1; l++)
            {
            ptr1[k] = ptr2[l];
            k++;
            }
        }
        else
        {
        ptr1[k] = temp[k];
        }

    }
       cout << ptr1 << endl;
}

int main()
{
    char Einfueg1[256];
    char Einfueg2[256];
    int i = 0;

    cout << endl << "Bitte Text zum ausgeben eingeben: ";
    cin.getline(Einfueg1, 255);
    cout << "Bitte Text zum einfuegen eingeben: ";
    cin.getline(Einfueg2, 255);
    cout << "Bitte Position zum einfuegen angeben: ";
    cin >> i;

    TextEinfueg(Einfueg1, Einfueg2, i);

    return 0;
}
Heyhey guys,
I'm doing some C++ programming in school and our task was is it to insert a text inside of an other field with a position we give.
For example:
A: The weather is nice outside
B: not
Position: 15
The output should be "The weather is not nice outside"
At the moment I have the problem that the text B is getting inserted but it is overwriting Text A so the result is "The weather is note outside"
In my opinion the mistake is in

Code: Select all

else
        {
        ptr1[k] = temp[k];
        }	
temp[k] needs to be smth else so it starts there where it ended before going in the

Code: Select all

if(k == i-1)
        {
            for(l; l <= textlaenge-1; l++)
            {
            ptr1[k] = ptr2[l];
            k++;
            }
        }
But I dont get it done.. I tried it using an other variable then k in temp but it didnt work at all. And subtracting from k didnt help too..
I dont know what to do maybe someone could help?
niin
Average Program
Posts: 86
Joined: Fri Dec 28, 2012 4:36 pm
Location: Germany

Re: C++ Function Problem

Post by niin »

Sorry if this is hard to read, first time for me posting on this thread
User avatar
vov
Match Winner
Posts: 568
Joined: Thu Feb 17, 2011 8:40 pm

Re: C++ Function Problem

Post by vov »

Try moving the right side out of the way first before inserting so you have space, like this:

Code: Select all

The weather is nice outside.
The weather is nice outside    .
The weather is nice outsid    e.
The weather is nice outsi    de.
...
The weather is ni    ce outside.
The weather is n    ice outside.
The weather is     nice outside.
The weather is not nice outside.
It's just one possible way but I hope it gives you somewhere to start. :D
chrisd
Round Winner
Posts: 315
Joined: Sat May 29, 2010 1:13 pm

Re: C++ Function Problem

Post by chrisd »

What I am wondering about when I see code like that is why you are taught to use char* to represent strings. In this century we use std::string from the standard library for that purpose. In fact, using char* like you do here is only a good idea if you don't mind seeing lots of crashes in your programs. In the character arrays that you are using only 255 characters fit. Whenever a string gets larger than that size your program is most likely going to crash...... I think it is more or less criminal negligence to teach beginning programmers to use char* and also not tell them of the dangers.

If you insist on using char*, and don't mind a dirty solution you could also write

int TextEinfueg(char *Einfueg1, char *Einfueg2, int i)
{
char save_char = Einfueg1;
Einfueg1 = 0;
cout << Einfueg1 << Einfueg2;
Einfueg1 = save_char;
cout << Einfueg1 + i;
}

Personally I would use std::string and I would return an std::string instead of letting the function print. It is bad style to mix printing and operations that change the string. Instead have one function for the string operation and you main function for input and output. The reason is that splitting concerns makes the chance that you can reuse your function someplace much greater.

string TextEinfueg(const string &Einfueg1, const string &Einfueg2, int i)
{
string result;
result.reserve(Einfueg1.size() + Einfueg2.size());
result.append(Einfueg1, 0, i);
result.append(Einfueg2);
result.append(Einfueg1, i, string::npos);
return result;
}
niin
Average Program
Posts: 86
Joined: Fri Dec 28, 2012 4:36 pm
Location: Germany

Re: C++ Function Problem

Post by niin »

Thank you both for the answers. I think I'm going to try vov's first because chrisd's seems like I need to find out a little bit more about strings first ^.^'
niin
Average Program
Posts: 86
Joined: Fri Dec 28, 2012 4:36 pm
Location: Germany

Re: C++ Function Problem

Post by niin »

Code: Select all

#include <iostream>

using namespace std;

int main()
{
    char Einfueg1[255];
    char Einfueg2[255];
    int i = 0;
    int h = 0;
    int k = 0;

    cout << "Bitte Text zum ausgeben eingeben: " << endl;
    cin.getline(Einfueg1, 255);

    cout << "Bitte Text zum einfuegen eingeben: " << endl;
    cin.getline(Einfueg2, 255);

    cout << "Bitte Position zum einfuegen eingeben: " << endl;
    cin >> i;    
 
//Basic stuff around here nothing really happens just giving the sentence and the word I want to put in it as well as the position to put it in

    for(h; Einfueg2[h] != '\0'; h++)
    {
        h++; 

//this is to find out how long the word I want to put in is

    }

    for(k; Einfueg1[k] != '\0'; k++)
    {
     k++;                                    //this is to find out how long the sentence I want to put the word in is, everything fine till here
    }

    for(int j = k; j >= i; j--)         //The struggle starts here
    {
        if(Einfueg1[j] == '\0')        //The idea is to start at the last position of the sentence I typed at start
        {
            Einfueg1[j] = ' ';           //This if is here to put the \0 away cause I had some struggles getting it away without this if
            Einfueg1[j+h] = '\0';    //j+h because I want to put it the length of the word I put in behind. As example "not" is 3 types                                
                                              //so everything needs to be moved 3 steps
        }
        else
        {
        Einfueg1[j+h] = Einfueg1[j];      //so if it's not the \0 just move the other stuff of the sentence 
        }
    }

    for(int j = 0; Einfueg2[j] != '\0'; j++)
    {
        Einfueg1[i] = Einfueg2[j];          //this is to fill in the word I want in the sentence i is the position where it should be and j is 0 so I get the whole word
        i++;                                        //this is working fine the only problems are in the other for
    }

    cout << endl << "Ergebnis: " << Einfueg1;


    return 0;
}
So this time there are some comments in the code so you guys have it easier to understand what I was thinking while doing it. I tried vov's methode with moving it step by step. There is a problem in the function where I want to move it but I don't get it...
User avatar
vov
Match Winner
Posts: 568
Joined: Thu Feb 17, 2011 8:40 pm

Re: C++ Function Problem

Post by vov »

Code: Select all

    for(h; Einfueg2[h] != '\0'; h++)
    {
        h++; 
    }
Here, h is already being counted up in the loop control part itself. You don't have to then count it up in the loop again - that's doubled so now it goes 2 4 6 8 10. Which is a problem if your word is actually 9 letters long. Just delete the h++ in the loop so it's empty, same with the other loop with the k. With those two changed, it works for me. :-)

Chrisd mentioned strings, which in C++ can do lots of stuff like that already with already existing functions - I'd bet there's probably some insert function. I do recommend those if you want to do some bigger projects because you know they work and you don't have to write em every time. For starting though I think it's helpful to program some stuff like this to get an idea how it works behind the scenes.
niin
Average Program
Posts: 86
Joined: Fri Dec 28, 2012 4:36 pm
Location: Germany

Re: C++ Function Problem

Post by niin »

Oh yes I didn't realise it being doubled. Thanks for that advice.

I'm going to see what I can find out about strings and try it. Even tho they're probably just useful for the programming outside of school because our teacher doesn't like if people don't do it the way he teaches @.@
User avatar
Lucifer
Project Developer
Posts: 8640
Joined: Sun Aug 15, 2004 3:32 pm
Location: Republic of Texas
Contact:

Re: C++ Function Problem

Post by Lucifer »

Ok, here's how you do this algorithm.

First, use sizeof() to figure out how long the base string is and how long the insertion string is. Add the two sizes together and malloc your char* array to be that size.

Then you iterate over the original string, copying the values to the new char* array you made. When you get to the insertion point, you copy the values from the new string in. When you're done with that (make sure you ignore CR/LF characters), continue copying characters from the old string.

Your function will then return a pointer to the new char* array you made. Make sure to free everything else that you don't need so that you don't leak memory.

Make sure you do bounds checking, as well, because if you accidentally malloc your new string to be not big enough, you want to stop iterating so you don't have problems. Also, I didn't say much about how to handle CR/LF characters.

Chrisd: Do they still require you to learn math before you can use a calculator? ;)

Edit: I didn't read the newest code you posted, btw. So if you've already figured out this is how the algorithm should work, you can disregard this post.
Image

Be the devil's own, Lucifer's my name.
- Iron Maiden
niin
Average Program
Posts: 86
Joined: Fri Dec 28, 2012 4:36 pm
Location: Germany

Re: C++ Function Problem

Post by niin »

@Lucifer thanks for that really detailed answer. It's working now after vov told me where the mistakes are. It isn't as nice as your idea of doing it but it works ^.^ I'm going to use sizeof() and maybe try out what chrisd said.
The other stuff you talked about like malloc seem a little bit to complicated at the knowledge level I have atm :wink:
Post Reply