c++ - 有没有办法使用strcpy将一个字符串数组复制到另一个字符串或另一个数组中?

标签 c++ arrays loops menu copy

又是我!
我对C++还是很陌生,昨天刚交了一份作业,创建了一个菜单,其中有7个选项可供选择。我能够完成所需的所有任务,但选项7除外,后者是将一个阵列复制到另一个阵列并退出新阵列。
我知道我的代码在情况7中不会显示它,但是是否有人可以让我知道如何使用当前的代码完成任务?它不再对我的任务有所帮助,但是我觉得这是我应该能够做的。也许我在屏幕前花了太长时间,无法寻找答案,但是在任何情况下,我都会为我将来的程序获得任何帮助。

#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;

}

最佳答案

方法1:最简单的方法
如对此StackOverflow question的第一个答案所述,如果您使用的是C++ 11,则可以使用std::array将一个数组复制到另一个数组。您完全不必担心使用strcpy

std::array<int,4> A = {1,2};
std::array<int,4> B = A; // This copies array A into array B.
因此,对于您的特定代码,您可以简单地创建一个新的字符串数组并将其设置为master
Herestd::array的相关文档。
方法2:使用strcpy
另外,如果您确实想使用strcpy,,请改用strcpy_s,因为strcpy现在被认为是不安全且过时的功能。您可以这样称呼它:
strcpy_s(FirstThing, SecondThing);
它将把SecondThing的内容复制到FirstThing中。

关于c++ - 有没有办法使用strcpy将一个字符串数组复制到另一个字符串或另一个数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62617734/

相关文章:

java - 为什么我的 Scan.nextDouble 给出 java 错误?

c - C 字符串中的十六进制值

javascript - 从 json 文件中提取包含数组中值的项目

Swift:暂停无限滚动背景(即无限循环)

c++ - 试图在C++中的子类中调用父类的 protected 函数

c++ - 从函数模板错误返回修改后的值

c++ - 在 Linux 中运行的 Eclipse Juno CDT - 它没有给我任何工具链

c++ - 减少 operator= 和复制构造函数之间的代码重复

loops - 在 Go 中使用范围循环

R - 使用循环搜索一个变量与另一个变量并创建新的合并变量