Skip to main content
GameDev.net gamedev.net
🔒 Locked

(char*) Problem

Started by Leiarchy9 Jan 31, 2004 at 4:42 PM 4 replies 900+ views
Original Post
Leiarchy9
Leiarchy9
Header
#ifndef _Main_h
#define _Main_h


int StrLen(char*);
void SetString(char*, char*);


#endif
Source
#include <iostream>
#include <cstdlib>

#include "Main.h"

   using std::cout;
   using std::cin;
   using std::endl;

   
   
int main(void)
{

     char strBuffer[50];
     char strMessage[50] = "Hello World!";
     
     SetString( strBuffer, strMessage );
     
     cout << strBuffer << endl;

     cin.get();
     return EXIT_SUCCESS;
}


int StrLen( char * paramString )
{

     int iCount = 0;
     
     while( paramString[iCount] != ''\0'' )
     {
     
        iCount++;
     }
     
     return iCount;
}


void SetString( char * strEmpty, char * strSource )
{

     for( int i = 0; i < StrLen(strSource); i++ )
     {
     
        strEmpty[i] = strSource[i];
     }

}
No compiler errors but when it actual outputs the string........it also outputs *excessive* or unneeded/unwanted/unknown characters. If there anyway to cut the excessive off? -------- "Do you believe in Ghost?"
--------"Do you believe in Ghost?"
Pipo DeClown
Pipo DeClown
void SetString( char * strEmpty, char * strSource )
{

for( int i = 0; i < StrLen(strSource); i++ )
{

strEmpty[i] = strSource[i];
}
strEmpty[i+1] = 0; // same as ''\0''


}


--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..
danielf
danielf
Just one correction, use

strEmpty = 0; // same as ''\0''

instead of:

strEmpty[i+1] = 0; // same as ''\0''
SiCrane
SiCrane
If you insist on using C style strings, why not just use strcpy()?

If there''s a reason you''re not using strcpy(), the C idiom for using a loop to copy strings is:

while (*dest++ = *src++);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.