GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Oct-2004, 12:56
nirvana4lf nirvana4lf is offline
New Member
 
Join Date: Oct 2004
Posts: 8
nirvana4lf is on a distinguished road
Question

strcat(); function help


i know that this is probably a noob question but wutever. i am trying to figure out how to use the strcat(); function by testing it in a program, but it doesn't work. here is my code (fyi i am using windows xp not linux although i want to use linux...):

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;



int main()
{

  string var1 = "this is string 1.";
  string var2 = "this is string 2.";

  strcat(var1, string var2);
  system("pause");

  return 0;
}

please help me. i would really appreciate it!
Last edited by JdS : 02-Oct-2004 at 11:44. Reason: Please insert your example C/C++ codes between [c] and [/c] tags
  #2  
Old 01-Oct-2004, 14:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nirvana4lf
i know that this is probably a noob question but wutever. i am trying to figure out how to use the strcat(); function by testing it in a program, but it doesn't work. here is my code (fyi i am using windows xp not linux although i want to use linux...):


please help me. i would really appreciate it!

strcat() is used with c-style strings (null-terminated array of chars).

Here's an example:

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
  char var1[100] = "This is string 1.";
  char var2[100] = "This is string 2.";

  cout << "var1: <" << var1 << ">" << endl;
  cout << "var2: <" << var2 << ">" << endl;

  strcat(var1, " ");
  strcat(var1, var2); // for c-style strings

  cout << "After strcat, var1: <" << var1 << ">" << endl;

  return 0;
}
  

With the C++ string class, concatenation of strings is performed with the "+" operator. Here's an example:
CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;



int main()
{

  string var1 = "this is string 1.";
  string var2 = "this is string 2.";

  cout << "Initially var1: <" << var1 << ">" << endl;
  cout << "Initially var2: <" << var2 << ">" << endl;

  var1 = var1 + " " + var2;

  cout << "After the operation, var1: <" << var1 << ">" << endl;

return 0;
}


With c-style strings, you must allocate enough storage to hold the largest string that you will ever store in that array.

With the C++ string class, strings are automatically re-sized as you perform operations.

Regards,

Dave
  #3  
Old 01-Oct-2004, 16:05
nirvana4lf nirvana4lf is offline
New Member
 
Join Date: Oct 2004
Posts: 8
nirvana4lf is on a distinguished road
Quote:
Originally Posted by davekw7x
strcat() is used with c-style strings (null-terminated array of chars).

Here's an example:

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
  char var1[100] = "This is string 1.";
  char var2[100] = "This is string 2.";

  cout << "var1: <" << var1 << ">" << endl;
  cout << "var2: <" << var2 << ">" << endl;

  strcat(var1, " ");
  strcat(var1, var2); // for c-style strings

  cout << "After strcat, var1: <" << var1 << ">" << endl;

  return 0;
}
  

With the C++ string class, concatenation of strings is performed with the "+" operator. Here's an example:
CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;



int main()
{

  string var1 = "this is string 1.";
  string var2 = "this is string 2.";

  cout << "Initially var1: <" << var1 << ">" << endl;
  cout << "Initially var2: <" << var2 << ">" << endl;

  var1 = var1 + " " + var2;

  cout << "After the operation, var1: <" << var1 << ">" << endl;

return 0;
}


With c-style strings, you must allocate enough storage to hold the largest string that you will ever store in that array.

With the C++ string class, strings are automatically re-sized as you perform operations.

Regards,

Dave

thank you dave!
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 19:46
Calling functions within a function spudtheimpaler C Programming Language 5 02-Mar-2004 08:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.