Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-29-2004, 11:16 AM   #1 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
Repitition

I'm working on a program for one of my classes that goes through all of the str functions, it prints out the results of the function and then I must write a function that will do the same thing without using the librarys function. I got most of it working, but the mystrcat part is shakey, my second word that should be added to the first word appears twice, I looked at my loops and everything looks fine to me but it obviously isnt if it repeats the second word.

Here is an example of my output ....




Appending Part

Adding word 2 to word 1 with the librarys function: Hello Mr. Henry
Adding word 2 to word 1 with my function: Hello Mr. HenryMr. Henry
-----------------------------------------------


Mr. Henry is repeated for some reason.



Here is my code in main the librarys function of strcat is used, and then right underneath that my version called mystrcat is called, so the error must be in the function mystrcat.



Any ideas???


Code:
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <cstring>


int mystrlen (char []);
void mystrcpy (char one [], char two []);
void mystrncpy (char one [], char two [], int count);
void mystrcat(char *one, char *two);





int main ()
{
	int liblen;
	int mylen;
	char word1 [50] = "Hello ";
    char word2 [10] = "Mr. Henry";
	char string1 [50] =" ";
	char string2 [50] ="COPY ME";
	int num=6;
	char string3 [50]=" ";
	char string4 [50]="December";
	char string5 [50]="a";
	char string6 [50]="b";
	char string7 [50]="Its A or D";
	char string8 [50]="Is It A or D";





	//String Len

	liblen = strlen("Hello");
	mylen = mystrlen("Hello");

		cout<<"Length Part"<<endl<<endl;
		cout<<"The length of Hello with the librarys function: "<<liblen<<endl;
		cout<<"The length of Hello with my function: "<<mylen<<endl;
		 if (!(mylen==liblen))
		 {
			 cout<<"Your function doesn't work like the librarys"<<endl;
		 }
		cout<<"-----------------------------------------------"<<endl;
		cout<<""<<endl<<endl;


	//String Cat

		cout<<"Appending Part"<<endl<<endl;
		 cout<<"Adding word 2 to word 1 with the librarys function: "<<strcat(word1, word2)<<endl;
			mystrcat(word1, word2);
		 cout<<"-----------------------------------------------"<<endl;
		 cout<<""<<endl<<endl;


	//String Cpy
		cout<<"Copying Part"<<endl<<endl;
		cout<<"String 1: "<<string1<<endl;
		cout<<"String 2: "<<string2<<endl;
		cout<<"Copying string 2 to string 1 with the librarys function: "<<strcpy(string1, string2)<<endl;
      mystrcpy(string1, string2);
		cout<<"-----------------------------------------------"<<endl;
		cout<<""<<endl<<endl;


	//String Ncpy
		cout<<"Copying Part With Integer"<<endl<<endl;
		cout<<"String 1: "<<string3<<endl;
		cout<<"String 2: "<<string4<<endl;
		cout<<"Number of chars to be copyed: "<<num<<endl;
		cout<<"Copying string 2 to string 1 with the librarys function: "<<strncpy(string3, string4,num)<<endl;
      mystrncpy(string3, string4, num);
		cout<<"-----------------------------------------------"<<endl;
		cout<<""<<endl<<endl;


	//String Cmp
		cout<<"Compare Part"<<endl<<endl;
		cout<<"String 1: "<<string5<<endl;
		cout<<"String 2: "<<string6<<endl;
		cout<<"Comparing string 1 with string 2 with the librarys function: "<<strcmp(string5, string6)<<endl;
		cout<<"-----------------------------------------------"<<endl;
		cout<<""<<endl<<endl;


		/*String Search For First Occurrence
		cout<<"Search Part"<<endl<<endl;
		cout<<"String 1: "<<string7<<endl;
		cout<<"String 2: "<<string8<<endl;
		cout<<"Comparing string 1 with string 2 with the librarys function: "<<strstr(string7, string8)<<endl;
		cout<<"-----------------------------------------------"<<endl;*/
		cout<<""<<endl<<endl<<endl;

	      cout<<"Press enter to quit, scroll up for the begining problems";
         cin.get ();
	return 0;
}


















int mystrlen (char input [])
{
	int location=0;
		while (!(input[location]=='\0'))
			location++;

		return location;
}





void mystrcpy (char one [], char two [])
{
   int x=0;

	while (!(x=='\0'))
   {
    	one [x] = two [x];
   }

   cout<<"Copying string 2 to string 1 with my function: "<<one<<endl;
}



void mystrncpy (char one[], char two[], int count)
{
 	int x = 0;

   while ((!(x=='\0'))&&(x<count))
   {
   	one [x] = two [x];
   }

   cout<<"Copying string 2 to string 1 with my function: "<<one<<endl;


}


void mystrcat(char *one, char *two)
{

	char final [100];
	int lenone = strlen(one);
	int lentwo = strlen(two);
	int i;



	i = lenone;

// writes one to final
	while (i--)
	{
		final[i] = one[i];
	}

	i = lentwo;

// writes two to final
	while (i--)
	{
		final[lenone+i] = two[i];
	}

	final[lenone+lentwo] = 0;



	cout<<"Adding word 2 to word 1 with my function: " <<final<<endl;
}
keystoneman is offline   Reply With Quote
Old 09-29-2004, 02:37 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
You are appending word2 to word1:
word1+word2 =
Hello + Mr. Henry = Hello Mr. Henry = word1
Code:
cout<<"Adding word 2 to word 1 with the librarys function: "<<strcat(word1, word2)<<endl;
Then you are appending word1 AGAIN with word2:
word1+word2 =
Hello Mr. Henry + Mr. Henry = Hello Mr. HenryMr. Henry = word1
Code:
mystrcat(word1, word2);
That's it.

A few quick tips:
I didn't look well at your code at all but here are a few notes:
You don't need this:
#include <stdlib.h>
#include <math.h>
#include <cstring>

cstring is ANSI-C, that is true. But you can do it in ISO-C++ completely.

Do this:
#include <iostream>
using namespace std;

If your compiler can't handle namespace std globally then do this (even much more professional-like anyway):
#include <iostream>
std::cout<<...;
std::strcat()
etc.

Basically add std:: to all the methods as defined by the C++ standard.

Or you can add forward declarations like this:

using std::cout;
using std::strcat;
using std::strcpy;
using std::endl;

Then use it normally:
cout<<"hi"<<endl;

Just a few quick tips. See if you like it .
__________________
Valmont is offline   Reply With Quote
Old 09-29-2004, 03:59 PM   #3 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
Thanks for the help got it to work, by the way my professor said we will be getting to containers and STL later in the year, so when the time comes I will implement them.
keystoneman is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:56 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting