473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

A few questions

9
Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. class myString
  4. {
  5.         private:
  6.             char value[20]; // Assume no more than 20 characters for simplicity.
  7.         public:
  8.             myString (char *str) { strcpy (value, str); } // Constructor
  9.             void printString ( ) { cout << value << endl; }
  10.             myString& operator+= (const myString& s1)
  11.             {
  12.             char concString[50];
  13.             strncpy(concString,value,20);
  14.             strncpy(concString,s1.value,20);
  15.             myString finalString(concString);
  16.             *value=*finalString.value;
  17.  
  18.             }
  19. };
  20.  
  21.  
  22.  
Expand|Select|Wrap|Line Numbers
  1.  *  myStringMain.cpp
  2.  *
  3.  *  Created by Reuben Doetsch on 10/18/07.
  4.  *  Copyright 2007 __MyCompanyName__. All rights reserved.
  5.  *
  6.  */
  7.  
  8. #include "myString.h"
  9.  
  10. main()
  11. {
  12. char firstString[20];
  13. cout << "Enter String @1:";
  14. cin >> firstString;
  15.  
  16. myString first(firstString);
  17.  
  18. char secondString[20];
  19. cout << "enter string 2:";
  20. cin >> secondString;
  21.  
  22. myString second(secondString);
  23.  
  24. first+=second;
  25.  
  26. first.printString();
  27. }
  28.  
I don't understand why this doesn't work as intended. This is supposed to concate the strings.
Oct 21 '07 #1
1 1060
weaknessforcats
9,208 Expert Mod 8TB
[qute=hjast]
class myString
{
private:
char value[20]; // Assume no more than 20 characters for simplicity.
public:
myString (char *str) { strcpy (value, str); } // Constructor
void printString ( ) { cout << value << endl; }
myString& operator+= (const myString& s1)
{
char concString[50];
strncpy(concString,value,20);
strncpy(concString,s1.value,20);
myString finalString(concString);
*value=*finalString.value;

}
};
[/quote]

I presume this for a class or something since C++ already hjas a string class.

However, your += operator just needs to concatenate the value in s1 to the value in the the this obect. You need ot allow for a null terminator also.
Expand|Select|Wrap|Line Numbers
  1. myString& operator+= (const myString& s1)
  2. {
  3.             int len = strlen(this->value);
  4.             strncat(this->value, s1.value, 20 - len -1);
  5.             strcat(this->value, '\0'); ;
  6.  }
  7.  
Your code:
myString finalString(concString);
*value=*finalString.value;
just creates a local variable named finalString followed by an assignment of finalString.value[0] to this->value[0]. This is not a copy of the string. Rather, it's just the assignment of a single character.
Oct 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.