Function String

Feb 24, 2010 at 4:23pm
yas
Last edited on Mar 3, 2010 at 2:23pm
Feb 24, 2010 at 4:38pm
How exactly does it crash?
Feb 24, 2010 at 4:44pm
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.
Feb 24, 2010 at 4:48pm
yas
Last edited on Mar 3, 2010 at 2:23pm
Feb 25, 2010 at 3:38am
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.
Feb 25, 2010 at 1:20pm
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.
Feb 25, 2010 at 2:21pm
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;
}

Feb 25, 2010 at 3:45pm
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.
Feb 26, 2010 at 12:45am
Uh bluecoder, why did you post the answer? You don't do that on this forum.
Topic archived. No new replies allowed.