简体   繁体   中英

Is there a way to use strcpy to copy a string array into another string or different array?

It's me again!

I am still way to new at C++, and just turned in an assignment yesterday creating a menu with 7 options to pick from. I was able to complete all the tasks required except for option 7, which was to copy an array to another array and cout the new array.

I know my code does not show it in case 7, but is there anyone who can let me know how I would complete the task with the current code I have? It will not help me with the assignment anymore, but I feel it is something that I should be able to do. Maybe I spent too long in front of the screen and searching for answers that it just eluded me.... in any circumstance, I would appreciate any help I can get for my future programs.

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main(void)
{

    // Delcarations
    string MasterFile[6]; // MasterFile Array
    MasterFile[0] = "I want to thank all the C++ students who has helped me this semester. You have inspired me to work harder and to be able to design small C++ programs using a gamming approach.";
    string master = MasterFile[0];
    string str1 = "my Instructor, Professor Penn";
    string str2 = "I want to thank all the C++ students who has helped me this semester. You have inspired me to work harder and to be able to design efficient C++ programs using a gamming approach.";

    char masterfile[] = { "I want to thank all the C++ students who has helped me this semester. You have inspired me to work harder and to be able to design small C++ programs using a gamming approach." };
    char StudentFile[1] = {masterfile[1]}; // StudentFile Array

    char selection;

    // Defines the width of the menu
    const int menuWidth = 84;

    // This is a loop structure for the menu box using ASCII
    // This prints the top left corner of the menu box (ASCII)
    cout << char(201);

    // This prints the top border of the menu box (ASCII)
    for (int column = 0; column < menuWidth; ++column) {
        cout << char(205);
    }

    // This prints the top right corner of the menu box (ASCII)
    cout << char(187);
    cout << "\n";

    // array with menu options
    string menu[20];
    menu[0] = "                                 **************";
    menu[1] = "                              ***  MAIN  MENU  ***";
    menu[2] = "                                 **************";
    menu[3] = "                    Please Choose From The Following Options:";
    menu[6] = "  1. Master File Array Statement:";
    menu[8] = "  2. Length of Master File Array:";
    menu[10] = "  3. Replacing Phrase 'all the C++ students' with 'my Instructor, Professor Penn':";
    menu[12] = "  4. Swaping 'small' with 'efficient':";
    menu[14] = "  5. Find and Display the Position of 'th': ";
    menu[16] = "  6. Erase the Phrase 'using a gaming approach':";
    menu[18] = "  7. Copy MasterFile Array to Student Array:";

    for (string option : menu)
    {
        cout << char(186) // print left border
            << setw(menuWidth) // set next item width
            << left // set next item aligment
            << option // print menu option string with width and aligment
            << char(186) << "\n"; // print right border
    }

    // This will print the bottom left corner of the menu box (ASCII)
    cout << char(200);

    // This prints the bottom border of the menu box (ASCII)
    for (int column = 0; column < menuWidth; ++column) {
        cout << char(205);
    }

    // This prints the bottom right corner of the menu box (ASCII)
    cout << char(188);
    cout << "\n\n";

    /*


    END OF MENU


     */

    cout << "\t\t\t     Enter Your Selection: ", cin >> selection, cout << endl;

    do
    {

        switch (selection)

        {

        case '1':

            cout << "You have chosen to create Masterfile Array:\n\n";
            cout << master; cout << endl;
            cout << "\n\n";

            break;

        case '2':

            cout << "You have chosen to display the length:\n\n";
            cout << "The number of characters in MasterFile Array is: ";
            cout << master.length(), cout << endl;
            cout << "\n\n";

            break;

        case '3':

            cout << "You have chosen to replace 'all the C++ students' with 'my Intructor, Professor Penn':\n\n";
            cout << master.replace(16, 20, str1), cout << endl;
            cout << "\n\n";

            break;

        case '4':

            master.swap(str2);
            cout << "You have to swap the word 'small' with 'efficient':\n\n";
            cout << master; cout << "\n\n";

            break;

        case '5':

            cout << "You have chosen to find and display the 'th' position:\n\n";
            cout << "'th' starts in position: ";
            cout << master.find("th"); cout << " of the array."; cout << endl;
            cout << "\n\n";

            break;

        case '6':

            cout << "You have chosen to erase the phrase 'using a gaming approach:\n\n";
            cout << master.erase(149) << ".", cout << "\n\n";

            break;

        case '7':

            cout << "\nYou have chosen to copy StudentFile Array into MasterFile Array:\n\n";
            cout << master;
            
        default: cout << "\n Invalid selection\n\n";

        }

    } while ((selection = main()) != 7);

    return 0;

}

Method 1: The simplest way

As explained in the first answer to this StackOverflow question , if you are using C++11, you can copy one array to another array using std::array . You don't have to worry about using strcpy at all.

std::array<int,4> A = {1,2};
std::array<int,4> B = A; // This copies array A into array B.

So for your specific code, you can simply create a new string array and set it equal to master .

Here is the relevant documentation for std::array .

Method 2: Using strcpy

Alternatively, if you do want to use strcpy, use strcpy_s instead, as strcpy is now considered an unsafe and obsolete function. You can call it like this:

strcpy_s(FirstThing, SecondThing);

which will copy the contents of SecondThing into FirstThing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM