Question

C++ Programming I have finished the code but there's one error which shows that "strcpy" might...

C++ Programming

I have finished the code but there's one error which shows that "strcpy" might be unsafe. Consider using strcpy_s instead. I've tried that but it's not working probably because I didn't implement it correctly. I am posting my code below. It'd be really helpful if you could fix all the strcpy errors and post it below. Thank you.

Text.cpp:

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include "Text.h"

Text::Text ( const char *charSeq )
{
   bufferSize = strlen(charSeq) + 1;
   buffer = new char[bufferSize];
   strcpy(buffer, charSeq);
}

Text::Text ( const Text &other )
{
   bufferSize = other.bufferSize;
   buffer = new char[bufferSize];
   strcpy(buffer, other.buffer);
}

void Text::operator = ( const Text &other )
{
   int len = other.bufferSize;
   if (len > bufferSize)
   {
       delete[] buffer;
       bufferSize = other.bufferSize;
       buffer = new char[bufferSize];
   }
   strcpy(buffer, other.buffer);
}

Text::~Text ()
{
   delete[] buffer;
}

int Text::getLength () const
{
   return bufferSize - 1;
}

char Text::operator [] ( int n ) const
{
   // check if n is out of range first
   return 0;
}

void Text::clear ()
{
   buffer[0] = '\0';
}

void Text::showStructure() const

// Outputs the characters in a string. This operation is intended for
// testing/debugging purposes only.

{
   int j; // Loop counter

   for (j = 0; j < bufferSize; j++)
       cout << j << "\t";
   cout << endl;
   for (j = 0; buffer[j] != '\0'; j++)
       cout << buffer[j] << "\t";
   cout << "\\0" << endl;
}

Text Text::toUpper( ) const
{
   Text temp;
   for (int i = 0; i < bufferSize; i++)
   {
       if ((buffer[i] > 96 && (buffer[i] < 123)))
       {
           temp.buffer[i] = char(buffer[i] - 32);
       }
   }
   return temp;
}

Text Text::toLower( ) const
{
   Text temp;
   for (int i = 0; i < bufferSize; i++)
   {
       if ((buffer[i] > 64 && (buffer[i] < 91)))
       {
           temp.buffer[i] = char(buffer[i] + 32);
       }
   }
   return temp;
}

bool Text::operator == ( const Text& other ) const
{
   {
       if (bufferSize == other.getLength())
       {
           if (bufferSize == 0)
           {
               return true;
           }
           else
           {
               for (int i = 0; i < bufferSize; i++)
               {
                   if (buffer[i] != other.buffer[i])
                   {
                       return false;
                   }
               }
               return true;
           }
       }
       else
       {
           return false;
       }
   }
}

bool Text::operator < ( const Text& other ) const
{
   if (bufferSize < other.getLength())
   {
       return true;
   }
   else
   {
       return false;
   }
}

bool Text::operator > ( const Text& other ) const
{
   if (bufferSize > other.getLength())
   {
       return true;
   }
   else
   {
       return false;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you are working in a visual studio IDE, try including the _CRT_SECURE_NO_WARNINGS in the pre processor definitions of your project.

Please rate it if the above solution helps you in any way or if you have any concerns comment it, I will help you through again.

Add a comment
Know the answer?
Add Answer to:
C++ Programming I have finished the code but there's one error which shows that "strcpy" might...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is c++ programming and here is my code. I am getting an exception thrown on...

    This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception? #include <iostream> #pragma warning(disable:4996) class PermanentWorker { private:    char *name;    int salary; public:    PermanentWorker(const char* nam, int money) : salary(money)    {        name = new char[strlen(nam) + 1];        strcpy(name, nam);    }    int getPay() const    {        return salary;   ...

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • I need to complete the C++ program for hotel reservation. In the main cpp you have...

    I need to complete the C++ program for hotel reservation. In the main cpp you have to display the hotel information from the hotel.txt based on customer preferences(which can be taken from the User.txt) For example, if the customer's budget is 200$, then only the hotels with prices below that amount should be displayed. Then the user has an option to choose which one to reserve for the particular time(which also can be found in the user.txt) The last two...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT