当前位置: 首页 > 知识库问答 >
问题:

将void中的值打印到其他void

谭建章
2023-03-14

我正在尝试打印在“show”void中的“getvalue”void中找到的值。我得到一个错误:

错误:“学生”未在此范围中声明

#include <iostream>

using namespace std;

int studentNumber=0;
int testNumber=0;
struct Student
{
string name,grade;
int studentNo,*testResults;
double average;
};

void getValue()
{
    cout << "Enter the number of students: ";
    cin >> studentNumber;
    cout << "Enter the number of tests: ";
    cin >> testNumber;

    Student* Students = new Student[studentNumber];

    for(int i=0; i< studentNumber; i++)
    {
        cout<< "\n" << "Enter the name of the " << i+1 << ". student: ";
        cin >> Students[i].name;
        cout<< "\n" << "Enter the number of the " <<  i+1 << ". student: ";
        cin >> Students[i].studentNo;

        Students[i].testResults = new int[testNumber];

            for(int z=0; z<testNumber; z++)
            {
                cout<< "\n" << "Enter the " << z+1 << ". exam grade of the " << i+1 << ". student: " ;
                cin >> Students[i].testResults[z];
            }
    }
}

void show()
{
    for(int i=0; i < studentNumber; i++)
    {
        cout<< "\n" << Students[i].name;
        cout<< "\n" << Students[i].studentNo;
        for(int z=0; z<testNumber; z++)
            {
                cout<< "\n" <<Students[i].testResults[z];
            }
    }
}

int main()
{
    getValue();
    show();
}

我正试图在另一个名为“show”的虚空中显示获得的值,但失败了。(必须在一个不同的虚空中的代码的一般结构必须是我的作业中另一个名为“show”的虚空)

共有1个答案

黎震博
2023-03-14

您必须传递值。

可以通过引用来完成:

// omit

void getValue(Student*& Students) // add reference argument
{
    cout << "Enter the number of students: ";
    cin >> studentNumber;
    cout << "Enter the number of tests: ";
    cin >> testNumber;

    // don't declare here and use the arugment
    Students = new Student[studentNumber];

    // omit
}

void show(Student* Students) // add argument (need not be reference)
{
    // omit
}

int main()
{
    // add variable for arguments and use that
    Student* s;
    getValue(s);
    show(s);
}

或通过全局变量

// omit

// declare variable here (as global)
static Student* Students;

void getValue()
{
    cout << "Enter the number of students: ";
    cin >> studentNumber;
    cout << "Enter the number of tests: ";
    cin >> testNumber;

    // don't declare here and use the global variable
    Students = new Student[studentNumber];

    // omit
}

void show()
{
    // omit
}

int main()
{
    getValue();
    show();
}
 类似资料:
  • 我有一个csv文件结构如下: 并且我必须使用awk脚本打印单个计算机部件的价格总和(第3行到第6行)与组装的计算机价格(第7行)之间的比较,同时显示每个品牌的国家。终端中的打印结果应该类似于: 事先非常感谢。

  • 我想打印真值表到一个表在一个adt文件,没有一个程序,但我不知道如何得到淡水河或值打印到odt文件,这个程序只是打印结果在屏幕上!

  • 为什么我的代码要打印这个,而不是打印保存在数据库中的学生姓名? 下面是我的代码:

  • 我之所以给出这个问题的源代码,是因为我不能在TrainsTest类中做一个简单的系统输出打印,这让我感到很沮丧。正如您所看到的,我在TrainsTest类中为以下方法打印vaule时遇到了问题; public void testDistanceBetween_ABC public void testDistanceBetween_AD public void testDistanceBetween

  • 是否有任何缓存或某种设置在jasper或类似的东西?

  • 我希望下面显示一个绘图,但我没有看到绘图,解释器只是挂起(我的后端报告自己为)。 我如何让情节显示? 我正在运行一个有很多迭代的模拟,并希望每1000次迭代更新我的图,以便我可以监控我的模拟是如何发展的。 下面的伪代码: 在主线程中使用绘图可能会导致绘图GUI挂起/崩溃,因为我还有其他工作要做。所以这个想法是在一个单独的线程中进行绘图。 我看到过使用进程而不是线程的建议(例如这里)。但是当我的模拟