View Single Post
  #2 (permalink)  
Old October 24th, 2004, 08:15 PM
C@uark C@uark is offline
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

In the GetInput function the line of code name = "John";
will not assign the string literal "john" into an array (unless you are initializing a declaration of a char array).
the '=' is for assignment of values.

you can use the c style string library for string manipulation:

# include <string.h>

and use the strcpy function in the following formats strcpy(string 1,string 2);, strcpy(string 1,"string literal");

the function copies the contents of string 2 to string 1;


Sample code:


# include <iostream.h>
# include <string.h> //C-style string library


void main()
{

    // Variable definition
    char string1[50]="something";
    char string2[50]="nothing";

    strcpy(string1,string2);
    cout<<string1<<endl;
    strcpy(string2,"New String");
    cout<<string2<<endl;

    return;
}

OUTPUT 1: nothing
OUTPUT 2: New string.







Mike
Reply With Quote