Thread: Assistance needed --Plz

  1. #1
    Newbie96
    Guest

    Assistance needed --Plz

    This program is suppose to do 3 things:

    1 - copy a string using: strncpy (without Null) and strncpy (with null)
    2- Input a string and reverse it (i.e. enter First Name Last and output Last, First)
    3- Use a substring to that will read a portion of a string

    The following is the code I have, but it just doesn't want to work and I can't figure out why.
    Any help would be greatly appreciated.

    Thank you

    Code:
    #include<iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::flush;
    
    
    #include <cstring>
    using std::string;
    
    void mystrncpy (char *, char *, int);
    void LastNameFirst (char *, char *);
    void substr (char *, char*, int, int);
    
    const int MAX_STRING = 200;
    int main()
    {
    	char Source[MAX_STRING];
    	char Destination[MAX_STRING];
    	int MaxLength;
    	char Name[MAX_STRING];
    	char NewName[MAX_STRING];
    	int StartIndex;
    	int SubMaxLength;
    	
    	cout <<"Enter string to be copied: ";
    	cin.getline(Source, MAX_STRING, '\n');
    	cout <<"Enter number of characters to be copied: ";
    	cin>>MaxLength;
    	cout<<"\nThe string you entered is: ";
    	cout<<Source;
    	mystrncpy (Destination, Source, MaxLength);
    	cout<<endl;
    	
    
    	cout<<"\nEnter name (in the form First name, Space, Last Name): ";
    	cin.getline(Name, MAX_STRING, '\n');
    	for (int i=0; i<3; i++){
    	LastNameFirst (Name, NewName);
    	cout<<"Name after conversion is: "<<NewName<<endl;
    	
    	}
    	
    
    	cout<<"\nEnter a string: ";
    	cin.getline(Source, MAX_STRING, '\n');
    	cout<<"Enter a starting index number: ";
    	cin>>StartIndex;
    	cout<<"Enter a length to copy: ";
    	cin>>SubMaxLength;
    	cout<<"\nThe string you entered was: "<<Source;
    	cout<<"\nThe converted string is: ";
    	substr (Destination, Source, StartIndex, SubMaxLength);
    
    
    	return 0;
    }
    
    
    void mystrncpy (char *Destination, char *Source, int MaxLength)
    {
    	
    	strncpy(Destination, Source, MaxLength);
    	cout<<"\nThe string you entered using strncpy is: "<<Destination[MaxLength];
    	strncpy(Destination, Source, MaxLength-1);
    	Destination[(MaxLength - 1)] = '\0';
    	cout<<"\nThe string you entered using mystrncpy is: "<<Destination<<endl;
    	
    }
    
    
    void LastNameFirst (char *Name, char *NewName)
    {
    	char *p;
    	p = strchr(Name, ' ');
    	*p = 0;
    
    	//copy last name to NewName, append ", " and 
    	//concatenate first name
    
    	strcpy(NewName, p+1);
    	strcat(NewName, ", ");
    	strcat(NewName, Name);
    	
    	//replace the NULL char with the original blank
    	*p = ' ' ;
    
    }
    
    void substr(char *Destination, char *Source, int StartIndex, int SubMaxLength)
    {
    	
    	string (Destination, StartIndex, SubMaxLength);
    	cout<<Destination[StartIndex, SubMaxLength+1]<<endl;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this (old code commented).
    Code:
    int main()
    {
    	char Source[MAX_STRING];
    	char Destination[MAX_STRING];
    	int MaxLength;
    	char Name[MAX_STRING];
    	char NewName[MAX_STRING];
    	int StartIndex;
    	int SubMaxLength;
    	
    	cout <<"Enter string to be copied: ";
    	cin.getline(Source, MAX_STRING, '\n');
    	cout <<"Enter number of characters to be copied: ";
    	cin>>MaxLength;
    	cin.ignore();
    	cout<<"\nThe string you entered is: ";
    	cout<<Source;
    	mystrncpy (Destination, Source, MaxLength);
    	cout<<endl;
    	
    
    	cout<<"\nEnter name (in the form First name, Space, Last Name): ";
    	cin.getline(Name, MAX_STRING, '\n');
    	for (int i=0; i<3; i++){
    	  LastNameFirst (Name, NewName);
    	  cout<<"Name after conversion is: "<<NewName<<endl;
    	
    	}
    
    	cout<<"\nEnter a string: ";
    	cin.getline(Source, MAX_STRING, '\n');
    	cout<<"Enter a starting index number: ";
    	cin>>StartIndex;
    	cout<<"Enter a length to copy: ";
    	cin>>SubMaxLength;
    	cout<<"\nThe string you entered was: "<<Source;
    	cout<<"\nThe converted string is: ";
    	substr (Destination, Source, StartIndex, SubMaxLength);
    
    
    	return 0;
    }
    
    
    void mystrncpy (char *Destination, char *Source, int MaxLength)
    {
    	
    	strncpy(Destination, Source, MaxLength);
    	//cout<<"\nThe string you entered using strncpy is: "<<Destination[MaxLength];
    	cout<<"\nThe string you entered using strncpy is: "<<Destination << endl;
    	strncpy(Destination, Source, MaxLength);
    	//Destination[(MaxLength - 1)] = '\0';
    	Destination[MaxLength] = '\0';
    	cout<<"\nThe string you entered using mystrncpy is: "<<Destination<<endl;
    	
    }

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I wish somebody would do my CS homework!

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assistance greatly needed to do LCD Display
    By HELPMEPLEASE!!! in forum C Programming
    Replies: 6
    Last Post: 03-28-2009, 05:07 PM
  2. C++ program assistance needed
    By Velocity in forum C++ Programming
    Replies: 31
    Last Post: 10-05-2008, 09:08 PM
  3. Assistance Needed
    By Sir_McIntyre in forum Projects and Job Recruitment
    Replies: 81
    Last Post: 09-19-2008, 06:43 AM
  4. Newbie c programmer need assistance plz...
    By Nexus-ZERO in forum C Programming
    Replies: 17
    Last Post: 12-27-2007, 04:05 PM
  5. Web Bot Needed ( will pay ) Plz Look
    By tribalflames69 in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 05-04-2005, 11:37 AM