Can you help me with this c++ structure?

I'm trying to write a structure where i ask to the user how much students he wants to register. Then it ask for the name, code, group and score of the students, and then print the results. When I run it, it goes ok until when it comes to print the results. I tried with just printed the name, but it doesn't work. What's wrong, how I fix it? Thanks


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
#include<iostream>
#include<conio.h>
#include<stdio.h>


using namespace std;

struct Alumno{
	int codigo;
	char nombre[30];
	char grupo[5];
	float calificacion;
} est[300];

int main(){
	int n_alumnos;
	cout<<"Digite el numero de alumnos a ingresar: ";
	cin>>n_alumnos;
	
	for(int i=0; i<n_alumnos;i++){
	
	    fflush(stdin);
		cout<<"Nombre del alumno: ";
		cin.getline(est[i].nombre, 30, '\n');
		
		cout<<"Codigo del alumno: ";
		cin>>est[i].codigo;
		
		cout<<"Ingrese su grupo: ";
		cin>>est[i].grupo,5;
		
		cout<<"Calificacion: ";
		cin>>est[i].calificacion;
//		cout"\n";
		}  
	
		
		cout<<"Nombre: "<<est[i].nombre<<endl;
		getch();
		return 0;
		}
Last edited on
Your 'i' variable does not exist outside of the for loop (nor should it).

You need to loop again to print the data in the array.
1
2
3
4
for (int i = 0; i < n_alumnos; i++)
{
    cout<<"Nombre: "<<est[i].nombre<< '\n';
}


Also,
cin>>est[i].grupo,5;
The 5 here doesn't do anything. It does not prevent a buffer overflow, which can happen because cin >> char array is not safe.

If you used strings (#include <string>), this would not be an issue.

char grupo[5] --> string grupo.
cin >> est[i].grupo;
Last edited on
Oh ok, thanks, I will try that.
Consider:

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
#include <iostream>
#include <iomanip>

using namespace std;

struct Alumno {
	int codigo {};
	char nombre[30] {};
	char grupo[5] {};
	float calificacion {};
} est[300] {};

int main() {
	int n_alumnos {};

	cout << "Digite el numero de alumnos a ingresar: ";
	cin >> n_alumnos;

	for (int i = 0; i < n_alumnos; ++i) {
		cout << "Nombre del alumno: ";
		cin >> ws;
		cin.getline(est[i].nombre, 30, '\n');

		cout << "Codigo del alumno: ";
		cin >> est[i].codigo;

		cout << "Ingrese su grupo: ";
		cin >> setw(5) >> est[i].grupo;

		cout << "Calificacion: ";
		cin >> est[i].calificacion;
	}

	for (int i = 0; i < n_alumnos; ++i)
		cout << est[i].nombre << "  " << est[i].codigo << "  " << est[i].grupo << "  " << est[i].calificacion << '\n';
}

@chxx,
Please see below:
PLEASE USE CODE TAGS (the <> formatting button to the right of this box), when posting code.

Along with the proper indenting, it makes it easier to read your code, and thus also easier to respond to your post.

Tutorials on how to use code tags:

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

I found the second link to be the most help.

Hint: You can hit "edit post", highlight your code and then press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the "preview" button at the bottom to see how it looks.

It just makes it easier for us to help you.

Thanks,
max
Thanks for your tip Max, done. I just need to indent the code.
Last edited on
Seeplus, that's a good version, thanks for writing it.
Topic archived. No new replies allowed.