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;
}
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];
}
Code: Select all
if(k == i-1)
{
for(l; l <= textlaenge-1; l++)
{
ptr1[k] = ptr2[l];
k++;
}
}
I dont know what to do maybe someone could help?