Efficient C++ String Manipulation: Tips and Tricks from Experts

  • C/C++
  • Thread starter yungman
  • Start date
  • Tags
    C++ Strings
In summary: St1;The name stored in memory is: TestThe name stored in memory is: TesterThe name stored in memory is: Tester123
  • #106
yungman said:
Yes, I was running debug by hitting F5 first and stop at the break point before I check. This is the picture. I don't see it.
Possibly Step Backward is no longer a feature in VS 2019. I'm running VS 2017, and it is there, but only if the debugger is running.
Here's what I see when the debugger is not running.
DbgNotRun.png

Here are the options when the debugger is running.
DbgRunning.png
 
Technology news on Phys.org
  • #107
Mark44 said:
Possibly Step Backward is no longer a feature in VS 2019. I'm running VS 2017, and it is there, but only if the debugger is running.
Here's what I see when the debugger is not running.
View attachment 271067
Here are the options when the debugger is running.
View attachment 271068
Thanks

Too bad, ha ha, maybe I should try to get the 2017! Believe me, I tried the alt-[ where it shows or not. It just gave me a "bing" sound.

I don't know how your 2017 is on this, my VS does not allow strcpy() and even strncpy(). I have to change the code in the book to strncpy_s() on all of them and I have look to get the length to put in the extra two parameters.

Thanks
 
  • #108
I might have an English problem here. I read this CAREFULLY like 10 times, I cannot make heads and tails out of this. The question is written in the copy. I have no idea what the book refer all the 1st, 2nd, 3rd constructors etc. I copy the two pages in the book. They are continuous from one page to the next.
Constructor.jpg


Constructor1.jpg


I labeled question (1) as 1st, 2nd and 3rd. I labeled questions(2) and (3) as 0, 1 and 2. Please help clarify (1) to (4) for me.

Thanks
 

Attachments

  • Constructors.jpg
    Constructors.jpg
    97.2 KB · Views: 115
Last edited:
  • #109
For question (1), see Contents of InventoryItem.h (Version 3) on page 759. The "third constructor" is labeled "Constructor #3". It receives three arguments.

The statement that you asked about, is on page 761. It calls that constructor three times, once for each of the three elements of the array named inventory, supplying three arguments each time.

For questions (2) and (3):

The compiler chooses which constructor to use, by comparing the number and types of arguments that you give when calling the constructor, with the number and types of arguments in the constructors in the class definition. inventory[0] and inventory[2] are each constructed with one argument: a C-string. Therefore "Constructor #2" is used (because it receives one argument, a C-string). inventory[1] is constructed with three arguments: a C-string, a double and an int. Therefore "Constructor #3" is used.

For question (4), the "default constructor" is the one that has no arguments, in this case "Constructor #1".
 
Last edited:
  • Like
Likes yungman
  • #110
yungman said:
Too bad, ha ha, maybe I should try to get the 2017!
Besides the different versions (2015, 2017, 2019, and earlier), there are different editions, such as Community Edition, Enterprise Edition, and one or two more that I don't remember. If you have the (free) Community Edition, maybe its features are limited. I have version 2017, but it's the Enterprise Edition.
Otherwise, I don't know why your version doesn't support Step Backward.
yungman said:
I have no idea what the book refer all the 1st, 2nd, 3rd constructors etc.
@jtbell explained this pretty well, but just in case, the constructors are numbered in the code you showed.
// Constructor #1
InventoryItem() - no parameters

// Constructor #2
InventoryItem(char *description) - a single C-string parameter

// Constructor #3
InventoryItem(char *description, double cost, int units) - three parameters
#2 and #3 use the parameter names that I suggested.
 
  • Like
Likes yungman
  • #111
Thanks guys, I did NOT know it's referring back to the old stuffs. This is a new section Array of Objects, the book should say very specifically it refers back to P759.

Thanks jtbell, it really helps you have the book and gave me the page number. I have to go back and read over with the new understanding. I was pulling hair since last night on this. I was thinking my English cannot be that bad!

Hi Mark, my middle name is "CHEAP", I ain't paying no money for the version of VS that cost money! No walking back it is! 😊

Thanks both jtbell and Mark.
 
  • #112
yungman said:
This is a new section Array of Objects, the book should say very specifically it refers back to P759.
No, this isn't reasonable. The topics in this book and most other programming books build on each other, and aren't standalone. What you're asking for is like saying that every use of int should refer back to where this keyword was defined. Eventually the chapters would be mostly references back to previous material, with very few parts devoted to new topics.
 
  • Like
Likes Vanadium 50
  • #113
I am running into problem with VS again. If you look at the invItem.h, if I use line11 to line27. It will FAIL and the error file is shown below. BUT if you block out line 11 to line27, the USE line 29 to line 44. It will work. Look at the two blocks of codes. THEY ARE EXACTLY THE SAME. I compare the two blocks a few times. They are exactly the same.

This is the invItem.h.

C++:
#ifndef invItem_H
#define invItem_H
#include <cstring>   // Needed for strlen and strcpy
#include <iostream>

class invItem
{
  private:
    char* description; const int L = 21;  double cost; int units;
  public:
//This part of the code will give error
    invItem item1()
    { char cA1[] = "Alan";
      description = new char[L];//0 
      strncpy_s(description, L, cA1, L);
      cost = 0.0;   units = 0;}//2

   invItem item2(char *desc)//12
    { description = new char[strlen(desc)+1];//13
      strncpy_s(description, strlen(desc)+1, desc, strlen(desc)+1);//14
      cost = 0.0;   units = 0; }//15

    invItem item3(char* desc, double c, int u)//18
    { description = new char[strlen(desc) + 1];//19
      strncpy_s(description, strlen(desc)+1, desc, strlen(desc)+1);//20
      cost = c; units = u;}//21/*This is the exact code as above. If you use this part, it will work.
    invItem()   // Constructor #1
    {   char cA1[] = "Alan";
        description = new char[L];
        strncpy_s(description, L, cA1, L);
        cost = 0.0; units = 0;}// 2--Default values

    invItem(char* desc)   //12   Constructor#2 
    {   description = new char[strlen(desc)+1];
        strncpy_s(description, strlen(desc)+1, desc, strlen(desc)+1);
        cost = 0.0; units = 0; }//15---Default value
    invItem(char* desc, double c, int u)//18-Constructor#3,No default value
    {   description = new char[strlen(desc) + 1];
        strncpy_s(description, strlen(desc) + 1, desc, strlen(desc) + 1);
        cost = c; units = u;}//21 --From main()   
This is the end of the working code*/
    ~invItem()  { delete [] description; }//42, 43, 44
//Mutator function only for Constructor#1
    void setDesc(char *d) {strncpy_s(description, L, d, L);};//5--d=cAr1
    void setCost(double c) { cost = c; }//7
    void setUnits(int u) { units = u; }//9
// Accessor functions
    const char *getDesc() const { return description; }    //25, 31, 37  
    double getCost() const { return cost; }//27, 33, 39
    int getUnits() const { return units; }//29, 35, 41
};
#endif
This is the source.cpp
C++:
#include <iostream>
#include <iomanip>
#include <cstring>
#include "invItem.h"
using namespace std;
int main()
{//Create invItem object item1, to Constructor#1, set default values
   invItem item1;
   char cAr1[] = "Hammer";//3
   item1.setDesc(cAr1);//4--to mutator
   item1.setCost(6.95);//6--to mutator
   item1.setUnits(12);//8---to mutator
//Create invItem object item2 with one parameter
   char cAr2[] = "Pliers";//10--input parameter to item2
   invItem item2(cAr2);//11--cost and units by default
//Create invItem object item3 with 3 parameters
   char cAr3[] = "Wrench";//16
   invItem item3(cAr3, 8.75, 20);//17 parameters for item3
   cout << "The following items are in inventory:\n";//22
   cout << setw(20) << setprecision(2) << fixed << showpoint;//23
//Display item1 use item1. to access member functions
   cout << "Description: " << item1.getDesc() << endl;//24
   cout << "Cost: $" << item1.getCost() << endl;//26
   cout << "Units on Hand: " << item1.getUnits() << "\n\n";//28
//Display item2
   cout << "Description: " << item2.getDesc() << endl;//30
   cout << "Cost: $" << item2.getCost() << endl;//32
   cout << "Units on Hand: " << item2.getUnits() << "\n\n";//34
//Display item3
   cout << "Description: " << item3.getDesc() << endl;//36
   cout << "Cost: $" << item3.getCost() << endl;//38
   cout << "Units on Hand: " << item3.getUnits() << "\n\n";//40
   return 0;
}
This is the error message for the block of line11 to line27.
Error 10_15.jpg


Anyone can explain this?
 
  • #114
yungman said:
THEY ARE EXACTLY THE SAME

No they are not.
 
  • #115
  • Like
Likes Vanadium 50
  • #116
Or he could look line by line.

"invItem item1()" is not the same as "invItem()"
 
  • #117
Vanadium 50 said:
Or he could look line by line.

"invItem item1()" is not the same as "invItem()"
And "invItem2(char *desc)" is not the same as "invItem(char *desc)"
And "invItem3(char* desc, double c, int u)" is not the same as "invItem(char* desc, double c, int u)"

@yungman, you have forgotten the most significant thing about overloaded functions.
 
  • #118
Thanks guys, I found it. I deleted the program accidentally and I had to retype the program from my notes and somehow I typed invItem item1(), invItem item2() and invItem item3() in the .h file. When I check, I check the lines after this declaration line and totally missed it.

I was actually working on the next problem, but with this one opened, I accidentally went to the wrong window and deleted the codes and closed the program and lost everything. So I just typed the whole thing back from reading the notes I posed in post #91 really fast without thinking and wrote the declaration of the object item1,2 and 3 in it.

Thanks
 
Last edited:
  • Sad
Likes Vanadium 50
  • #119
Since this is no longer about strings, I'm going to close it. If you have a question about classes, please start a new thread.
 
  • Love
Likes Tom.G

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
5
Views
885
  • Programming and Computer Science
Replies
1
Views
878
  • Programming and Computer Science
Replies
3
Views
732
  • Programming and Computer Science
Replies
18
Views
2K
Back
Top