Can you help me with this c++ code?

I did a code that allow to register students 2-show users registered 3-order users registered by code and 4-search for an user by code. My problem is in the step 4, i did the code, and it prints, but it don't show what it should show. What i did wrong? the other parts of the code work well. The code is in spanish, but the mistake is in function #4 "buscarRegistro.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>



using namespace std;

int nr=0, opc, i, j, cod;
char ban='F';

 typedef struct{
 

	int codigo;
	string nombre;
	string apellido;
	float calificacion;
 }Estudiante;
 Estudiante nm, alumno[100];

void ingresarRegistro(){
	cout<<"Ingresar codigo: ";
	cin>>alumno[nr].codigo;
	cout<<"Ingresar nombre: ";
	cin>>alumno[nr].nombre;
	cout<<"Ingresar apellido: ";
	cin>>alumno[nr].apellido;
	cout<<"Ingresar calificacion: ";
	cin>>alumno[nr].calificacion;
	nr++;
	

}
void mostrarRegistro(){
    for(int i=0;i<nr;i++){
    	cout<<alumno[i].codigo<<"\t"
		    <<alumno[i].nombre<<"\t"
		    <<alumno[i].apellido<<"\t"
		    <<alumno[i].calificacion<<endl;
		    system("pause");
    	
	}
	system("pause");
	
}

void ordenarRegistros(){
	Estudiante nm;
	for(int i=0;i<nr;i++){
 	for(int j=0;j<nr-1;j++){
 		if(alumno[j].calificacion<alumno[j+1].calificacion){
 			nm=alumno[j];
 			alumno[j]=alumno[j+1];
 			alumno[j+1]=nm;
		 }
	 }
 	
 }  
 mostrarRegistro();
}

void buscarRegistro(){
	alumno;
	cout<<"ingresa codigo a buscar: "<<endl;
	cin>>cod;
	int i=0;
	
	 while ((ban=='F')&&(i<100)){
		if(alumno[i].codigo==cod)
		ban= 'V';
		i++;
			
		
	}for(int i=0;i<100;i++){
	if(ban=='V'){
	
	cout<<"codigo encontrado"<<endl;
		cout<<alumno[i].codigo<<"\t"
		    <<alumno[i].nombre<<"\t"
		    <<alumno[i].apellido<<"\t"
		    <<alumno[i].calificacion<<endl;  }
	else
	for(int i=0;i<100;i++)
	cout<<"codigo no encontrado"<<endl;
	getch();
}}
	
	   




int main(){
  do{
  	system("cls");

	cout<<"MENU"<<endl;
	cout<<"1.-Ingresa nuevo alumno"<<endl;
	cout<<"2.-Mostrar registros"<<endl;
	cout<<"3.-Ordenar registros"<<endl;
	cout<<"4.-Buscar registros por codigo"<<endl;
	cout<<"5.- Salir"<<endl;
	cin>>opc;
	
	switch(opc){
		    case 1:
			   ingresarRegistro();
	        break;
			case 2:
			   mostrarRegistro();
			break; 
			case 3:
			   ordenarRegistros();  
			break;	
			case 4:
				buscarRegistro();
			break;
	}
  }while(opc!=5);
}



[/code]
Last edited on
ban is what value for that loop?
It's a flag.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Line 10-11: Globals are a bad idea.

Line 13: typedef isn't required. Give the struct a tag.

Line 65: What is this line supposed to do? It does nothing.

Lines 70-73: Why are you searching unused entries? You should be searching nr entries.

Lines 77-84: Why are you printing unused entries? Shouldn't you be printing just the entry where code was found?

Lines 86-87: If cod wasn't found, you're printing "codigo no encontrado" 100 times? Do you really mean to do this?

Not tested:
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;

int nr = 0;

struct Estudiante  
{	int codigo;
	string nombre;
	string apellido;
	float calificacion;
};
Estudiante alumno[100];

void ingresarRegistro() {
	cout << "Ingresar codigo: ";
	cin >> alumno[nr].codigo;
	cout << "Ingresar nombre: ";
	cin >> alumno[nr].nombre;
	cout << "Ingresar apellido: ";
	cin >> alumno[nr].apellido;
	cout << "Ingresar calificacion: ";
	cin >> alumno[nr].calificacion;
	nr++;
}
void mostrarRegistro() {
	for (int i = 0; i < nr; i++) {
		cout << alumno[i].codigo << "\t"
			<< alumno[i].nombre << "\t"
			<< alumno[i].apellido << "\t"
			<< alumno[i].calificacion << endl;
		system("pause");
	}
	system("pause");
}

void ordenarRegistros() {
	Estudiante nm;
	for (int i = 0; i < nr; i++) {
		for (int j = 0; j < nr - 1; j++) {
			if (alumno[j].calificacion < alumno[j + 1].calificacion) {
				nm = alumno[j];
				alumno[j] = alumno[j + 1];
				alumno[j + 1] = nm;
			}
		}
	}
	mostrarRegistro();
}

void buscarRegistro() 
{	int	cod;

	cout << "ingresa codigo a buscar: " << endl;
	cin >> cod;	
	for (int i = 0; i < nr; i++) {
		if (alumno[i].codigo == cod) 
		{	cout << "codigo encontrado" << endl;
			cout << alumno[i].codigo << "\t"
				<< alumno[i].nombre << "\t"
				<< alumno[i].apellido << "\t"
				<< alumno[i].calificacion << endl;
		}
		else
			cout << "codigo no encontrado" << endl;
		_getch();
	}
}

int main() 
{	int		opc;

	do 
	{	system("cls");
		cout << "MENU" << endl;
		cout << "1.-Ingresa nuevo alumno" << endl;
		cout << "2.-Mostrar registros" << endl;
		cout << "3.-Ordenar registros" << endl;
		cout << "4.-Buscar registros por codigo" << endl;
		cout << "5.- Salir" << endl;
		cin >> opc;
		switch (opc) {
		case 1:
			ingresarRegistro();
			break;
		case 2:
			mostrarRegistro();
			break;
		case 3:
			ordenarRegistros();
			break;
		case 4:
			buscarRegistro();
			break;
		}
	} while (opc != 5);
	return 0;
}



It's a flag.

that was not my question. I can't trace its value, just see where it was init globally. Its set once at the top as a global, but are you sure of its value by the time you get to that loop?
Last edited on
@jonnin ban='v' is the value for true, no? I'm not sure, that's why I was asking for help.

@AbsractionAnnon, I will take your advice about the code tags, thanks. Also thanks for your feedback, I see I have a lot of mistakes.
In lines 86-87 if cod wasn't found it's supposed to primt just one time "codigo no encontrado". In the for, instead of i<100 is for(int i=0;i<cod;i++)?
I tried your code in dev-c, it's better now, but it has still a failure, it prints first "code not found" like two times, but then in prints code found and the data of that student.
probably, if its some form of verdad.

let me say it this way:
if you run the program in the debugger and go through it, does that flag's values match what they should be in all cases in all functions? If ban has the wrong value at any point in the program, your functions will give incorrect answers.
Oh I see, thanks for the explaination @jonnin.
Registered users can post here. Sign in or register to post.