c - Unable to start the int correctly and keep the number back from the function -
first @ sorry english, try best.
well, here's problem, making c program learning, program students manager, wanted add final function in user prompted id remove , after inserting id student eliminate database, problem is... when data inserted in first case there's added subroutine automatically adds id , int in struct,problem shows random int number "43405" or thought start integer 1, problem when function re-called again id 1 , don't work.
p.d:i read of guys told me make code more readable, can give me nice "tutorial" or make that?
function:
int insertar_notas(struct alumnos notas[20], int n,int id_alumno){ char resp[3]; system("cls"); puts("\n \a insercion del alumno\n"); while (!strstr(resp,"no")){ fflush(stdin); printf("\nel id de este alumno sera: %d\n", id_alumno); notas[n].id=id_alumno; id_alumno++; puts("\ndime el nombre del alumno\n"); scanf("%10s", notas[n].alumno ); system("cls"); fflush(stdin); puts("\ndime el apellido del alumno\n"); scanf("%10s", notas[n].apellido ); system("cls"); puts("\ndime la primera nota trimestral del alumno[1.23]\n"); scanf("%f", ¬as[n].nota1 ); system("cls"); puts("\ndime la segunda nota trimestral del alumno[1.23]\n"); scanf("%f", ¬as[n].nota2 ); system("cls"); puts("\ndime la tercera nota trimestral del alumno[1.23]\n"); scanf("%f", ¬as[n].nota3 ); n++; system("cls"); puts("\nquieres volver insertar otro?[si|no]\n"); scanf("%3s", resp); strlwr(resp); } return n; return id_alumno; }
main more info:
int main (void){ int menu = 0, n = 0, id_alumno; struct alumnos notas[20]; puts("\n<><><>bienvenido al recuento de notas de la escuela<><><>\n"); puts("\nque deseas hacer?\n"); while (menu != 5){ puts("\n1)insertas las notas de un alumno\n2)ver todas las notas\n3)ver las notas de un alumno\n4)modificar notas\n5)salir\n"); scanf("%d", &menu); switch(menu){ case 1: n=insertar_notas(notas,n,id_alumno); break;
c uses pass value, make id_alumno modifiable need pass pointer:
int insertar_notas(struct alumnos notas[20], int n,int *id_alumno) { // id_alumno pointer int inside function char resp[3]; system("cls"); puts("\n \a insercion del alumno\n"); while (!strstr(resp,"no")){ fflush(stdin); printf("\nel id de este alumno sera: %d\n", *id_alumno); // use * access value notas[n].id=id_alumno; (*id_alumno)++; // use * access value. ++ has higher precedence *, need parenthesis around *id_alumno ...
and in call main:
n=insertar_notas(notas, n, &id_alumno); // address-of id_alumno
also, need initialize id_alumno in main:
int menu = 0, n = 0, id_alumno = 1;
Comments
Post a Comment