Function String

yas
Last edited on
How exactly does it crash?
when i run the program, it shows this output


Enter a string  : 123456789
String 1 is        : 123456789
Enter a string  : abcdefghi
String 2 is        : abcdefghi


i need to close the program for it to exit. it just stuck there. i'm not where the problem lies.
yas
Last edited on
I would guess that (say) string newstr (not in code) = msg1+msg2; ....should show up somewhere and that there should be an instruction to print newstr. Since changestr() is of type void the addition of the two strings and the printing of their sum variable should occur within this function.
Lines 28 and 33 are memory stomps. You cannot use operator[] on strings to add to a string. TempStr has length 0
per line 17.
I have changed the code .

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void changestr(string &str1, string str2)
{
int numChar1;
int numChar2;
int totalChar;
int index = 0;
int start1 = 0;
int start2 = 0;
int end = 3;
string TempStr;
char tempstr1[100];

numChar1 = str1.length();
numChar2 = str2.length();
totalChar = numChar1 + numChar2;

while ( index < totalChar )
{
for( int i = start1; i < end; start1++)
{

tempstr1[index] = str1[start1];
cout<<"\n"<<tempstr1[index];
index++;
i++;
}
for( int j = start2; j < end; start2++)
{

tempstr1[index] = str2[start2];
cout<<"\n"<<tempstr1[index];
index++;
j++;
}

end = end + 3;
}
tempstr1[index++] = '\0';
str1= tempstr1;
cout << "\n New String 1 is: \t " << str1 << "\n\n";

return;
}


int _tmain(int argc, _TCHAR* argv[])
{
string msg1;
string msg2;

cout << "\n Enter a string:\t ";
getline(cin,msg1);

cout << "\n String 1 is: \t " << msg1 << "\n\n";

cout << "\n Enter a string:\t ";
getline(cin,msg2);

cout << "\n String 2 is: \t " << msg2 << "\n\n";

changestr(msg1,msg2);

system("pause");

return 0;
}

thanks guys. thanks bluecoder. your code works fine with less characters in each string. but when i enter more than 15 characters in each string, the program just stuck halfway. i was then prompt that the program triggered a breakpoint. i'm really confused.
Uh bluecoder, why did you post the answer? You don't do that on this forum.
Topic archived. No new replies allowed.