【问题标题】:Is there a way to use strcpy to copy a string array into another string or different array?有没有办法使用 strcpy 将字符串数组复制到另一个字符串或不同的数组中?
【发布时间】:2020-10-18 09:54:11
【问题描述】:

又是我!

我还是 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;

}

【问题讨论】:

  • 嗯,是的,strcpy() 可用于复制字符数组,因为它所做的只是复制元素,直到找到终止 '\0' 字符(数值为零的 char )。但是,在您的代码中,这不是复制StudentFile 的明智方法,它是一个由一个字符组成的数组,初始化为非零值 - 因此使用strcpy() 复制它会导致未定义的行为。由于您的代码已经使用std::string 的数组,我建议用std::string 替换所有char 数组,并且根本不使用strcpy()

标签: c++ arrays loops menu copy


【解决方案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 的相关文档。

方法二:使用strcpy

或者,如果您确实想使用 strcpy,,请改用 strcpy_s,因为 strcpy 现在被认为是不安全且过时的函数。你可以这样称呼它:

strcpy_s(FirstThing, SecondThing);

这会将 SecondThing 的内容复制到 FirstThing。

【讨论】:

    猜你喜欢
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2016-04-30
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    相关资源
    最近更新 更多