我的函数没有更新全局变量/main中使用的变量



我的项目是一些简单的矩阵运算,它应该可以很好地工作,但我的输入函数似乎没有更新全局变量,它们可能是我试图做的错误数据类型,但我不确定。以下是应该发生的事情的链接http://cpp.sh/22kie。这里还有一个指向Makefile的所有内容的链接https://drive.google.com/open?id=1tmIbdEWXJJ54moQKy8rkZODFkXKDU2Nz

这是我的头文件:

#ifndef project1
#define project1
static int choice, rows1, cols1, rows2, cols2, k;
static char choice2;
static int *mat1 = new int[10 * 10];
static int *mat2 = new int[10 * 10];
static int *matf = new int[10 * 10];
void output(int *matf, int rows, int cols);
void addition(int *mat1, int *mat2, int *matf, int rows, int cols);
void subtraction(int *mat1, int *mat2, int *matf, int rows, int cols);
void multiplication(int *mat1, int *mat2, int *matf, int rows1, int cols1, int rows2, int cols2);
int input();
#endif

这是:我的主菜:

#include <string>
#include "project1.h"
using namespace std;
int main()
{
input();
switch (choice)
{ //this switch block will call the correct fucntion with respect to the user's choice
case 1:
addition(mat1, mat2, matf, rows1, cols1);
break;
case 2:
subtraction(mat1, mat2, matf, rows1, cols1);
break;
case 3:
multiplication(mat1, mat2, matf, rows1, cols1, rows2, cols2);
break;
case 0:
return 0;
default:
cout << "invalid 2input";
}
//the end of the main function will repeat if the user would like
cout << "n Would you like to do another operation y/n?" << endl;
cin >> choice2;
if (choice2 == 'y')
{
main();
}
else
{
return 0;
}
}

这是输入函数:

int input()
{
//input function
cout << "Menu"
<< "n 1. Addition"
<< "n 2. Subtraction"
<< "n 3. Multiplication"
<< "n 0. Exit"
<< "n Enter the number of your choice" << endl;
cin >> choice;
if (choice == 0)
{ // this will kill the main function if "Exit" is chosen, or repeat if an invalid number is chosen
return 0;
}
else if (choice > 3)
{
cout << "invalid input" << endl;
input();
}
else
{
}
//this part of the code will prompt the user for the rows and columns for each matrix
cout << "Number of rows for the first Matrix? (max 10)" << endl;
cin >> rows1;
cout << "Number of columns for the first Matrix? (max 10)" << endl;
cin >> cols1;
cout << "Number of rows for the second Matrix? (max 10)" << endl;
cin >> rows2;
cout << "Number of columns for the second Matrix? (max 10)" << endl;
cin >> cols2;
//this if/else statement checks that the matrices are the right size to be operated
if ((choice == 1 || choice == 2) && ((cols1 != cols2) || (rows1 != rows2)))
{
cout << " error: for addition and subraction the two matrices must have the same amount of elements";
choice = 0;
return 0;
}
else if ((choice == 3) && (rows2 != cols1))
{
cout << " error: for multiplication the columns of the first must equal the rows of the second";
choice = 0;
return 0;
}
else
{
}
// this for loop allows the user to input each element for the first matrix
cout << "input the elements of the first matrix: " << endl;
for (int i = 0; i < rows1; i++)
{
for (int j = 0; j < cols1; j++)
{
cout << "Enter element "
<< "(" << i << "," << j << "): ";
cin >> k;
*(mat1 + i * cols1 + j) = k;
}
}
output(mat1, rows1, cols1); //this call to output will allow the user to see what they have imputed
// this for loop allows the user to input each element for the second matrix
cout << "input the elements of the second matrix: " << endl;
for (int i = 0; i < rows2; i++)
{
for (int j = 0; j < cols2; j++)
{
cout << "Enter element "
<< "(" << i << "," << j << "): ";
cin >> k;
*(mat2 + i * cols2 + j) = k;
}
}
output(mat2, rows2, cols2); //this call to output will allow the user to see what they have imputed
}```

编辑:将所有内容切换到本地变量后的新main:

main:

#include <iostream>
#include <string>
#include "project1.h"
using namespace std;
int choice, rows1, cols1, rows2, cols2, k;
char choice2;
int *mat1 = new int[10 * 10];
int *mat2 = new int[10 * 10];
int *matf = new int[10 * 10];
int main()
{

input( choice, rows1, cols1, rows2, cols2, k, mat1, mat2, matf);
cout<<rows1;
switch (choice)
{ //this switch block will call the correct fucntion with respect to the user's choice
case 1:
addition(mat1, mat2, matf, rows1, cols1);
break;
case 2:
subtraction(mat1, mat2, matf, rows1, cols1);
break;
case 3:
multiplication(mat1, mat2, matf, rows1, cols1, rows2, cols2);
break;
case 0:
return 0;
default:
cout << "invalid input";
}
//the end of the main fucntion will repeat if the user would like
cout << "n Would you like to do another operation y/n?" << endl;
cin >> choice2;
if (choice2 == 'y')
{
main();
}
else
{
return 0;
}
}

标题:

#ifndef project1
#define project1

int output(int *matf, int rows1, int cols1);
int addition(int *mat1, int *mat2, int *matf, int rows1, int cols1);
int subtraction(int *mat1, int *mat2, int *matf, int rows1, int cols1);
int multiplication(int *mat1, int *mat2, int *matf, int rows1, int cols1, int rows2, int cols2);
int input(int choice,int rows1,int cols1,int rows2,int cols2,int k, int *mat1, int*mat2, int*matf);
#endif

您的变量仍然是全局变量(在函数外部声明(。您应该在main中声明它们(尽管这不是导致问题的原因(。

现在的问题是,您正在按值将值传递给input。也就是说,调用input,增加堆栈,并将发送的值复制到input的堆栈空间中。当您在input中修改它们时,它们将在input的堆栈上进行修改。input然后返回,并且这些更新的值被删除。您需要将引用发送到这些变量。这意味着变量的地址将被复制到input的堆栈中,input将使用这些地址返回到main函数中的变量并直接更新这些值。

将输入的函数签名更改为

void input(int& choice, int& rows1, int& cols1, int& rows2, int& cols2, int& k, int *mat1, int* mat2, int* matf);

注意,input的返回值从int变为void。这是因为当您在main中调用它时,您没有将input的返回值分配给任何东西,所以我认为您不需要它。input中的任何return语句都需要更改为仅return;(而不是类似return 0;的语句(。

还要注意的是,在发送到输入端的前6个参数之后,现在存在与号(&(。通过这种方式,input将拥有它们的地址,并且可以直接更改它们。mat1mat2matf已经作为指针传递,因此它们不需要更改。

什么';通过引用传递和通过值传递之间的区别是什么?

最新更新