2
The exercise asks me to create matrices to store aircraft registration with some variables, done this, the program asks me to search all the information by entering the code. The problem is that when I search for the flight inside menu 3, the program stops.
main(){
int menu;
string codigo, nome, qtpol, ori, dest;
int qtcad = 0, linha;
string mat[1][5];
cout<<"Cadastro de Avioes";
cout<<"1 - Menu Principal\n";
cout<<"2 - Cadastrar Avioes\n";
cout<<"3 - Pesquisar Voos\n";
cout<<"4 - Sair\n";
cin>>menu;
while(menu!=4){
if(menu==1){
cout<<"Cadastro de Avioes";
cout<<"1 - Menu Principal\n";
cout<<"2 - Cadastrar Avioes\n";
cout<<"3 - Pesquisar Voos\n";
cout<<"4 - Sair\n";
cin>>menu;
}
else if(menu==2){
cout<<"Quantos Cadastros Deseja Fazer:";
cin>>qtcad;
string mat[qtcad][5];
for(int l=0;l<qtcad;l++){
cout<<"Digite o Codigo do Voo:";
cin>>mat[l][0];
cout<<"Digite o Nome da CIA:";
cin>>mat[l][1];
cout<<"Digite a Quantidade de Poltronas:";
cin>>mat[l][2];
cout<<"Informe a Origem:";
cin>>mat[l][3];
cout<<"Informe o Destino:";
cin>>mat[l][4];
}
cout<<"Cadastro Realizado com Sucesso.";
Sleep(2000);
system("cls");
menu=1;
}
else if(menu==3){
cout<<"Digite um Codigo para pesquisar um voo:";
cin>>codigo;
cout<<qtcad;
for(int l=0;l<qtcad;l++){
for(int c=0;c<5;c++){
if(codigo==mat[l][c]){
linha=l;
}
}
}
for(int c=0;c<5;c++){
cout<<mat[linha][c];
}
}
else{
cout<<"erro";
}
}
return 0;
}
First question I have is why mix C with C++? It would not be better to code using only C++. If you do this you may not even have an error. Don’t you want to learn how to make a typical C++ code instead of making one that uses C mechanisms and is more susceptible to errors? Hitting one eye actually has some problematic things, and the fact that the code is not well organized with good names helps to be difficult to identify the problem. Don’t you think that improving this part can also help if you don’t eliminate the problem, at least find the error more easily? Part of your difficulty you created.
– Maniero
I’m just programming with c++, I just put the #c to see if anyone can help...
– Vitor Lima
The
else
of the lineelse { cout << erro; }
doesn’t have aif
correspondent.– enzo
Besides not being nice spam to tag, is actually using resources that are C should not be used in C++, has no interest in following my advice and use only C++ resources to see if the code gets cleaner?
– Maniero
In this period I’m having my first contact with algorithms in c, but I use Cin and Cout, and that’s it. the resources I am using and due to other exercises done in this matter. The code may not be properly indented or clean, precisely because I’m starting it. The Program works, the problem happens after I put the menu, I tried to do with switch case but appeared another error that I also do not know how to solve
– Vitor Lima
But what’s most basic and easy to do in a code is this. If you think you don’t need to take care of that basic aspect will you be prepared to go for something more advanced? First you said you were doing it in C++, now you’re in C even though there’s a lot of things that aren’t C, not just the
cout
. I think the code is too confusing to try to fix, you’ll make it work, but you won’t learn to do something right, on the contrary, you’ll probably think it’s good because it worked. Wouldn’t it be better to go solving one problem at a time?– Maniero
Well, I guess I wasn’t clear, I agree that I have to improve on code identation, and of course, what I meant is that I’m initiating programming. About C and C++, the teacher is running algorithms in C and maybe that’s why I’m using resources in c, although I don’t know exactly what the difference is, the one thing I do differently is C and Cin, that’s what I meant. If the forum and only for advanced programmers I will close to ask, thank you very much
– Vitor Lima
But agreeing is different than doing it. If it is to do in C this code has too many elements in C++, will get lost more, by being starting should not do this, you do not even realize that you are using other C++ resources beyond what you are saying. The site of Q&A (not a forum) It is not for advanced programmers, on the contrary, I am saying to take care of the basics and you are wanting to do the advanced without this first base. I am just doing what is necessary for those who are starting that is to show the way and not let go ahead without understanding what you are doing.
– Maniero
You are right, I understand that I have to improve . Excuse my rudeness.
– Vitor Lima
I saw no rudeness.
– Maniero
@Vitorlima Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site
– Maniero