#include <iostream.h>
#include <string.h>
int const MaxSize = 50; 
 
Class CString 
{
public:
CString ( ); //Constructor initializes array to NULL.
int GetStrLen( ); //Gets the length of the string.
void ClearStr( ); //Clears the array to NULL.
void DisplayStr( ); //Displays the string as written.
void SetStrVal(char*); //Assigns the array a new string value.
char GetStrMid( ); //Returns the "middle" character of the 
//string. If the string length is even
//then the right-most character of the
//"left" half is returned.
char* GetString( ); //Returns the object's private data.
private:
char data[30]; //Accessible ONLY thru member functions
};
/*** Write the implementations of the class here. ***/
 
 
/*** End of class implementations *******************/
 
 
int main(void)
{
 
CString first_string; // First object of type CString
CString second_string; // Second object of type CString
char string[MaxSize]; // The string that user enters
 
cout << "Please enter the first string < " << MaxSize 
<< " characters in length: ";
 
// Insert code to get the first string from the user (use cin.getline).
 
cin.getline(string,MaxSize);

HERE IS MY QUESTION
// Insert code to initialize first_string to the inputted string value.

how may I do that?

thanks

Recommended Answers

All 5 Replies

I assume you've been given all this code by your tutor..? Where is the implementation code for each of the member functions? (It looks like you've been told to write that)


You will need to call a member function which accepts a char* (You have got one of those.. )

yes i try to do step by step, but my question is how may I initialize first_string to the inputted string value:

I have got:
cin.getline(string,MaxSize);

// Insert code to initialize first_string to the inputted string value.

?????????????????????
what i sthe call to initialize or something

how may I do that?


thanks

You're jumping ahead of yourself.. at the moment you can't do that, because your class member functions haven't got any body to them.

Write the body for your member function void SetStrVal(char*); .. then you can use that

I don'y got it, looks like it is to hard for me.
What I did was :

I used that to take the input

char* CString::GetString()
{
cin.getline(data,30);
 
}
 
in int main() i did something like that :
cout << "Please enter the first string < " << MaxSize<< " characters in length: "; 
first_string.GetString();
 
and that works but the only problem is that my teacher want to use an array :
char string[MaxSize];
to store a string, what he sad is: 
// Insert code to get the first string from the user (use cin.getline).
 
and then 
// Insert code to initialize first_string to the inputted string value.
 
and here i am lost
 
the problem lloks like that step by step:
 
// Use this template to do your lab.
 
#include <iostream.h>
#include <string.h>
int const MaxSize = 50; 
 
/*** Write your class specification here. ***/
Class CString 
{
public:
CString ( ); //Constructor initializes array to NULL.
int GetStrLen( ); //Gets the length of the string.
void ClearStr( ); //Clears the array to NULL.
void DisplayStr( ); //Displays the string as written.
void SetStrVal(char*); //Assigns the array a new string value.
char GetStrMid( ); //Returns the "middle" character of the 
//string. If the string length is even
//then the right-most character of the
//"left" half is returned.
char* GetString( ); //Returns the object's private data.
private:
char data[30]; //Accessible ONLY thru member functions
};
/*** End of class specification. ************/
 
/*** Write the implementations of the class here. ***/
 
/*** End of class implementations *******************/
 
// Write your code where indicated.
int main(void)
{
/**** These are the ONLY variables that are allowed in main! ****/
CString first_string; // First object of type CString
CString second_string; // Second object of type CString
char string[MaxSize]; // The string that user enters
/****************************************************************/
cout << "Please enter the first string < " << MaxSize 
<< " characters in length: ";
// Insert code to get the first string from the user (use cin.getline).
// Insert code to initialize first_string to the inputted string value.
 
cout << "The first string contains: ";
// Insert code to display the first string.
cout << endl;
cout << "\nThe length of the first string is " 
<< // Insert code to find the length of the first string.
<< endl;
cout << "The middle character of the first string is: " 
<< // Insert code to find the middle character of the first string.
<< endl;
cout << "Enter a second string < " << MaxSize 
<< " characters in length: ";
// Insert code to get the second string from the user (use cin.getline).
 
 
 
// Insert code to initialize second_string to the inputted string value.
cout << "The second string contains: ";
// Insert code to display the second string.
cout << endl;
cout << "\nThe length of the second string is " 
<< // Insert code to find the length of the second string.
<< endl;
cout << "The middle character of the second string is: " 
<< // Insert code to find the middle character of the first string.
<< endl;
// Insert code to clear the second string's data member
cout << "After clearing the second string it contains " 
<< // Insert code to find the new length of the second string.
<< " characters." << endl;
 
cout << "After reassigning the second string it contains: ";
// Insert code to assign the value of the first string to the second.
// Insert code to display the newly assigned second string.
cout << endl;
return 0;
}

if you know anything more then me which i'm sure you do just let me know
thanks

Initialise is the wrong word here. What he seems to be asking for is actually for you to use the SetStrVal function to copy the input string into the array. you can do that in one line - strcpy(data, user_input); Also be wary of the difference in the size of your CString array, and the user input string - it would be easy for the CString array to overflow. (Your best bet is to make them both the same size)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.