Hello Ragueky,
Welcome to the forum.
Sorry I just happened upon your question today.
I had to load up the program to find the less obvious problems. What I did find:
You did good initializing most of the variables, but the global variables at the beginning of the file are better put in main. I moved them and they worked just fine. Should it be necessary you can pass any variable you need to the function that needs it. It is best to avoid global variables unless they start with "constexpr" or "const" to make it a constant variable.
The variable
int x[0][0];
will not work because you can not have a zero length array. Whatever number that is there must be greater than zero. I made it 10 for now although I do not have any idea what it should be.
For line 30. You are not using" argc" and "argv" in the program, so they do not have to be there.
The first do/while loop. I do not understand why you are doing it twice in two different places.
I suggest putting the do/while in the "menu" function and only return a valid result. I translated the "cout" statements so I could understand better. It helped, but not as much as I thought it would. It did not help me understand the program any better for now. Not that is a problem. This is what I did with the menu function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
int menu()
{
int option{};
do
{
cout << "\n\n\n";
cout << "MENU\n\n";
//cout << "1-Introduzir Matriz ou Matrizes\n";
std::cout << "1 - Enter Matrix or Matrix\n";
//cout << "2-Somar Matrizes ou Subtrair Matrizes\n";
std::cout << "2 - Add Matrices or Subtract Matrices\n";
//cout << "3-Multiplicar Matrizes\n";
std::cout << "3 - Multiply Arrays\n";
//cout << "4-Multiplicar por uma constante\n";
std::cout << "4 - Multiply by a constant\n";
//cout << "5-Matriz Transposta\n";
std::cout << "5 - Transposed Matrix\n";
//cout << "6-Alterar Valores das Matrizes ou Matriz\n\n";
std::cout << "6 - Change Matrix or Matrix Values\n";
//cout << "7-Sair\n";
std::cout << "7 - Get out\n";
cout << "Opcao:";
cin >> option;
if (option < 1 || option > 7)
//std::cout << "\n Escolha inválida tente novamente." << std::endl;
std::cout << "\n Invalid choice try again." << std::endl;
} while (option < 1 || option > 7);
return option;
}
|
This way when you call the "menu" function it verifies that a valid number is chosen before returning that number. I used the variable "option" because it was there I normally us a variable called "choice". Mostly a personal preference. The function call for this would be:
menuChoice = menu();
.
In your first switch you have a case for "1", but you should also have a case for "7" and a "default" case.
The next do/while loop looks good for now although I would change lines 59 - 61 to just the above mention new "menu" call.
The only other suggestion I have is this:
Your original posted code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
do{
menu();
cout<<"Opcao:";
cin>>option;
switch(option){
/*case 1:cout<<"Introduzir Matriz ou Matrizes\n";
cout<<"Numero de Matrizes que pretende introduzir:\n";
cin>>qnt;
for(i=0;i<qnt;i++){
cout<<"Inserir a quantidade de linhas da Matriz:"<<i+1<<endl;
cin>> qntmatriz[i][0]; //linhas
cout<<"Inserir a quantidade de colunas da Matriz:"<<i+1<<endl;
cin>> qntmatriz[i][1]; //colunas
int x[qntmatriz[i][0]][qntmatriz[i][1]];
for(linha=0;linha<qntmatriz[i][0];linha++){
for(coluna=0;coluna<qntmatriz[i][1];coluna++){
cout<<"Introduza o valor da Matriz:"<<i+1<<endl;
cout<<"["<<linha<<"]"<<"["<<coluna<<"]\n";
cin>>x[linha][coluna];
}
}
}
break;*/
case 2:{cout<<"Somar ou Subtrair Matrizes\n";
cout<<"Que Operacao pretende efeturar?\n";
int Option=1;
//do{ //SOMA
cout<<"Qual e o numero da Matriz A para somar a B\n";
cout<<"A:\n";
int z=i+1;
cin>>z;
for(linha=0;linha<aux;linha++){
for(coluna=0;coluna<aux2;coluna++){
cout<<x[linha][coluna]<<"\t";
}
}
}
/*cout<<"1-Soma\n";
cout<<"2-Subtracao\n";*/
break;
case 3:cout<<"Multiplicar Matrizes\n";
break;
case 4:cout<<"Multiplicar por uma constante\n";
break;
case 5:cout<<"Matriz Transposta\n";
break;
case 6:cout<<"Alterar Valores das Matrizes ou Matriz\n";
break;
case 7:cout<<"...Sair...";
sair=true;
break;
default:cout<<"Escolha uma opcao das anteriores\n";
}
}
while(sair!=true);
|
Notice the } brace on line 53 and keep in mind that the indenting here is more than it should be, but the point is what does it go to and where is the { brace? It is hard to find.
There is no real standard on how to write code and what you did works as far as the compiler is concerned.
My rearrange d code makes it easier to read and find all the {}s:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
do
{
menuChoice = menu();
//cout << "Opcao:";
//std::cout << "Option\n";
//cin >> option;
switch (menuChoice)
{
/*case 1:cout<<"Introduzir Matriz ou Matrizes\n";
cout<<"Numero de Matrizes que pretende introduzir:\n";
cin>>qnt;
for(i=0;i<qnt;i++){
cout<<"Inserir a quantidade de linhas da Matriz:"<<i+1<<endl;
cin>> qntmatriz[i][0]; //linhas
cout<<"Inserir a quantidade de colunas da Matriz:"<<i+1<<endl;
cin>> qntmatriz[i][1]; //colunas
int x[qntmatriz[i][0]][qntmatriz[i][1]];
for(linha=0;linha<qntmatriz[i][0];linha++){
for(coluna=0;coluna<qntmatriz[i][1];coluna++){
cout<<"Introduza o valor da Matriz:"<<i+1<<endl;
cout<<"["<<linha<<"]"<<"["<<coluna<<"]\n";
cin>>x[linha][coluna];
}
}
}
break;*/
case 2:
//cout << "Somar ou Subtrair Matrizes\n";
std::cout << "Add or subtract arrays\n";
//cout << "Que Operacao pretende efeturar?\n";
std::cout << "What operation do you intend to do?\n";
int Option = 1;
//do{ //SOMA
//cout << "Qual e o numero da Matriz A para somar a B\n";
std::cout << "What is the number of Matrix A to add to B?\n";
cout << "A:\n";
int z = i + 1;
cin >> z;
for (linha = 0; linha<aux; linha++)
{
for (coluna = 0; coluna<aux2; coluna++)
{
cout << x[linha][coluna] << "\t";
}
}
/*cout<<"1-Soma\n";
cout<<"2-Subtracao\n";*/
break;
case 3:
//cout << "Multiplicar Matrizes\n";
std::cout << "Multiply Arrays\n";
break;
case 4:
//cout << "Multiplicar por uma constante\n";
std::cout << "Multiply by a constant\n";
break;
case 5:
//cout << "Matriz Transposta\n";
std::cout << "Transposed Matrix\n";
break;
case 6:
//cout << "Alterar Valores das Matrizes ou Matriz\n";
std::cout << "Change Matrix or Matrix Values\n";
break;
case 7:
//cout << "...Sair...";
std::cout << "...Get out...";
sair = true;
break;
default:
//cout << "Escolha uma opcao das anteriores\n";
std::cout << "Choose an option from previous\n";
}
} while (sair != true);
|
The {}s after "case 2:" are not needed, but it is not a problem if they are there. What follows the ':' and comes before the next "case" is considered to be one case. Pressing "Enter" after the ':' putting the next statement on the next line makes it easier to read.
Lastly notice how the {}s line up making it easier to find the pair. The only thing missing is a comment on the closing } to say what block of code it is for.
I think I covered everything.
Hope that helps,
Andy