Error searching row inside matrix

Asked

Viewed 106 times

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.

  • I’m just programming with c++, I just put the #c to see if anyone can help...

  • The else of the line else { cout << erro; } doesn’t have a if correspondent.

  • 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?

  • 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

  • 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?

  • 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

  • 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.

  • You are right, I understand that I have to improve . Excuse my rudeness.

  • I saw no rudeness.

  • @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

Show 6 more comments

1 answer

1


As there are many mistakes I will list them instead of trying to fix a specific point.

  • Indenting and naming variables that have meaning is no joke, it helps the most advanced programmers to understand what they are doing and find possible problems, imagine for those who are starting. Nobody should program without having this very clear and do in all the codes, even the simplest ones that exist. It costs nothing to do it the right way and so you train your brain to do it better and better. If you do it anyway you train your brain to do bad.
  • I have already said in the comments that I do not agree with the idea of mixing C with C++, I should choose a language and follow it. If you’re gonna do it in C, you can’t use it cin, cout, string, Sleep(), etc. If you are going to do it in C++, you should use vector or at least array that are structures ready better than the array C that is even a array for real.
  • In any case should not have one array two-dimensional there, this is wrong concept. Even if it works it is not right and then you will learn to do wrong. You should create a struct or even a class in C++, but I’ll stick with C and avoid talking in resources like this. Then you would have only one array of these structures. Maybe you haven’t learned this yet, but then you shouldn’t be trying to solve this kind of problem. First learn all the necessary mechanisms for the problem. If there is a teacher demanding something without having passed the appropriate mechanisms I can only regret and indicate to see with him if he can not follow the class otherwise, is hurting you.
  • Try to declare the variables closest to where it is used, don’t declare everything before, this is an old way of doing that decreases readability.
  • Note that the menu is repeated, simply initialize the variable with the appropriate value and do not need to create it before the loop.
  • The ideal is that each option was made in a function. You may not have learned this yet, but again this is too complex a problem for those who do not know how to work with functions.
  • Even if you do so the selection would be better with switch, If you made a mistake with him, you should try to figure out what it is and fix it, not throw it all away and do it another way. You need to understand what you’re doing. The goal of learning programming is not to make it work, it’s to make it right, it’s to understand what you’re doing and what’s happening.
  • There shouldn’t be an option for the main menu, or there shouldn’t be an option to quit, only an output option is useful and an option that does something that would happen anyway doesn’t have to exist.
  • Has variable that is declared twice, this should not even compile.
  • There’s no reason to have a nested loop to research, actually with the use of struct the error becomes more obvious and would not even happen.
  • Ask for the size of array does not make sense, this is information you have and should not expose to the user, this is detail of your code, maybe the error is there.
  • I’d have to clean this up first to make it easier to see the error. You can try to find it that way, but it takes a lot of work on something that shouldn’t be this way.
  • There are other little details I don’t even think are important right now.
  • My intention and really learn, I will analyze the points that you signaled, I believe that many people can have difficulty at the beginning, and that through effort and persistence and possible learn, so I will leave this question here so that I can help another person in the future. About the exercise, the teacher asked us to do it using matrix, he teaches with the C language, but as I had already learned a little about c++, maybe this is getting in the way.

  • I’m sorry I have to learn wrong, I would refuse.

  • The statement says the following: Create an algorithm that requires the user to register airplanes, where the code, name, number of armchairs, origin and destination will be requested. The program will ask the user how many registrations he wants to make, at the end, must be shown all the registered data. In this case, what is the best way to do this in C? you don’t think it would be efficient to use matrix ?

  • Well, you stated that the teacher had it done with matrix, now you show a statement that doesn’t say that. For all possible reasons the correct thing is to do with struct, as I have already stated in the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.