代码

#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>

using namespace std;
void case1();
void case2();


struct Students
{
    char first_name[10];
    char last_name[10];
    char country[20];


};



int n, i;
int main()
{
    char selection;
    do                                  //MENU
    {
        cout << "\n\nMENU\n";
        cout << "1. Case 1: \n";
        cout << "2. Case 2: \n";
        cout << "3. Exit";
        cin >> selection;

        switch (selection)
        {
        case '1': {
            system("cls");
            case1();
        } break;
        case '2': {
            //system("cls");
            case2();
        } break;
        }

    } while (selection != '3');

    return 0;

}


//*Function 1
void case1()
{

    Students array[10];
    int i;
    cin >> n;


    for (i = 0; i < n; i++)
    {
        cout << "Name:";
        cin >> array[i].first_name;
        cout << "Last Name:";
        cin >> array[i].last_name;
        cout << "Country:";
        cin >> array[i].country;

    }

    for (i = 0; i < n; i++)
    {
        cout << array[i].first_name << " ";
        cout << array[i].last_name << " ";
        cout << array[i].country << endl;


    }

}

//Function 2
void case2()
{
    Students array[10];
    char choose[2];
    //* I must use n and Students array [10] that I enter for the keyboard in function 1
    do {


        cout << "New check? Y/N \n";
        cin >> choose;
        if (_stricmp("N", choose) == 0)
        {
            break;
        }
        char name_for_check[10];
        cout << "\n Name for check:";
        cin >> name_for_check;

        for (i = 0; i < n; i++)
        {
            if (strcmp(array[i].first_name, name_for_check) == 0 || strcmp(array[i].country, name_for_check) == 0 || strcmp(array[i].last_name, name_for_check) == 0)
            {
                cout << array[i].first_name << " ";
                cout << array[i].last_name << " ";
                cout << array[i].country << endl;
            }
        }




    } while (_stricmp("Y", choose) == 0);
}

我想要做的是在第一个函数中输入n和array,在第二个函数中使用与我在第一个函数中输入的值相同的值。

还有其他。到目前为止,我已将这些结构用作全局结构。如何在main中定义我的结构并在case1()case2()中使用它?

最佳答案

首先声明struct Students,然后再声明函数原型(prototype),如下所示:

struct Students
{
    char first_name[10];
    char last_name[10];
    char country[20];
};

void case1( int &n, int maxN, Students array[] ); // int &n becaus n is output (reference to int)
void case1( int n, Students array[] );  // int n; because n is input

函数case1看起来像这样
void case1( int &n, int maxN, Students array[] )
{
    cin >> n;
    if ( n <= 0 )
        return;
    if ( n > maxN )
        n = maxN;

    for (int i = 0; i < n; i++)
    {
        cout << "Name:";
        cin >> array[i].first_name;
        ...
    }
    ...
}

这样称呼它:
 int main()
 {
    ....
    Students array[10];
    int n;

    case1( n, 10, array );
    case2( n, array );

    ...

我建议使用std::vector而不是数组。另请参阅您自己的问题Structures export data for specific name
#include <vector>

void case1( std::vector<Students> &array );

std::vector<Students> array;
case1( array );

关于c++ - C++传递值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34574558/

10-10 06:14