Unable to delete a determinate position in the struct, the data persist - c

First at all english its not my first language i will try my best.
Hello guys, I have a problem i'm trying to delete a determinate position of the struct, so instead of deleting the info inside by modifying the string with strcpy or just setting the int or float to 0 i want to erase the data in the struct by changing the position with the next one so 1.2.3.4.5 will be 1.2<-3.4.5 and stay this way 1.2.3.4, problem is after 1h trying to make it works there are some problems, first: if there's only one student after the program ask me for the id to erase, the new id seems to be a random number or like garbage data, so i guess the position changed but the data inside of this id persist.
Example:
id: 1
name: john
lastname: smith
score1: 2
score2: 5
score3: 6
After the function ask me for the id to be erased:
id: 425262
name: john
lastname: smith
score1: 2
score2: 5
score3: 6
the second issue is, if i insert some students, and the program ask for the id to be erased, all id numbers changes to that id i just insert instead of deleting the id target.
Heres the full code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct alumnos{
int id;
char alumno[10];
char apellido[15];
float nota1;
float nota2;
float nota3;
};
int insertar_notas(struct alumnos notas[20],int n,int *id_alumno);
void mostrar_notas(struct alumnos notas[20],int n);
void buscar_alumno(struct alumnos notas[20],int n);
void cambiar_notas(struct alumnos notas[20],int n);
void eliminar_alumno(struct alumnos notas[20],int n);
int main (void){
int menu = 0, n = 0, id_alumno = 1;
struct alumnos notas[20];
puts("\n<><><>Bienvenido al recuento de notas de la escuela<><><>\n");
puts("\nQue deseas hacer?\n");
while (menu != 6){
puts("\n1)Insertas las notas de un alumno\n2)Ver todas las notas\n3)Ver las notas de un alumno\n4)Modificar notas\n5)Eliminar datos del alumno\n6)Salir\n");
scanf("%d", &menu);
switch(menu){
case 1:
n=insertar_notas(notas,n,&id_alumno);
break;
case 2:
mostrar_notas(notas,n);
break;
case 3:
buscar_alumno(notas,n);
break;
case 4:
cambiar_notas(notas,n);
break;
case 5:
eliminar_alumno(notas,n);
break;
}
}
}
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", &notas[n].nota1 );
system("cls");
puts("\nDime la Segunda nota trimestral del Alumno[1.23]\n");
scanf("%f", &notas[n].nota2 );
system("cls");
puts("\nDime la Tercera nota trimestral del Alumno[1.23]\n");
scanf("%f", &notas[n].nota3 );
n++;
system("cls");
puts("\nQuieres volver a insertar otro?[si|no]\n");
scanf("%3s", resp);
strlwr(resp);
}
return n;
}
void mostrar_notas(struct alumnos notas[20],int n){
int i;
system("cls");
if (n != 0 ){
puts("\nLos alumnos insertados son:\n");
for (i = 0; i < n; i++)
{
printf("\n\nID %d\n\n Nombre:%s\n Apellido: %s\n Primera nota:%0.2f\n Segunda nota:%0.2f\n Tercera nota:%0.2f\n\n", notas[i].id, notas[i].alumno, notas[i].apellido ,notas[i].nota1 ,notas[i].nota2 ,notas[i].nota3 );
}
}
else
{
puts("\n \aNo hay registro\n");
}
}
void buscar_alumno(struct alumnos notas[20],int n){
int num = 0;
float media;
if (n != 0){
char ape_alumno[15];
system("cls");
puts("\n\aBusqueda por alumno\n");
puts("\nDime el apellido del alumno\n");
scanf("%15s", ape_alumno);
for ( num = 0; num < n ; num++){
if (strcmp(notas[num].apellido,ape_alumno)==0){
printf("\nEl alumno introducido es: %s %s\n", notas[num].alumno, notas[num].apellido );
media=(notas[num].nota1+notas[num].nota2+notas[num].nota3)/3;
printf("\nLa nota media es %0.2f \n", media);
if (media<5){
puts("\nSuspendido no hace media\n");
}
if (media=5 & media>6){
puts("\nSuficiente\n");
}
}
}
}else{
puts("\a\nRegistro vacio\n");
}
}
void cambiar_notas(struct alumnos notas[20],int n){
char ape_notas[15];
float nueva_nota1,nueva_nota2,nueva_nota3,nota_n1t,nota_n2t,nota_n3t;
int j = 0, submenu_mod = 0, nota_mod;
if (n != 0){
system("cls");
puts("\n \aDime el apellido del alumno a modificar las notas\n");
scanf("%15s", ape_notas);
for (j = 0;j < n; j++){
if (strcmp(notas[j].apellido,ape_notas)==0){
printf("\nLas notas de este alumno %s %s son:\n \n1r Trimestre:%0.2f\n \n2n Trimestre:%0.2f\n \n3r Trimestre:%0.2f\n", notas[j].alumno,notas[j].apellido,notas[j].nota1 ,notas[j].nota2 ,notas[j].nota3 );
while(submenu_mod != 3){
puts("\nQue quieres hacer?:\n\n1)Modificar todas las notas\n2)Modificar solo una nota\n3)Salir\n");
scanf("%d", &submenu_mod);
switch(submenu_mod){
case 1:
puts("\nDime la primera nota trimestral\n");
scanf("%f", &nueva_nota1);
puts("\nDime la segunda nota trimestral\n");
scanf("%f", &nueva_nota2);
puts("\nDime la tercera nota trimestral\n");
scanf("%f", &nueva_nota3);
notas[j].nota1=nueva_nota1;
notas[j].nota2=nueva_nota2;
notas[j].nota3=nueva_nota3;
printf("\nLas nuevas notas de este alumno son:\n \n1r Trimestre:%0.2f\n \n2n Trimestre:%0.2f\n \n3r Trimestre:%0.2f\n", notas[j].nota1 ,notas[j].nota2 ,notas[j].nota3 );
system("pause");
break;
case 2:
while (nota_mod != 4){
puts("\nQue nota trimestral quieres modificar?:\n");
printf("\n1)Nota trimestral %0.2f\n2)Nota trimestral %0.2f\n3)Nota trimestral %0.2f\n4)Salir", notas[j].nota1,notas[j].nota2,notas[j].nota3);
scanf("%d", &nota_mod);
switch(nota_mod){
case 1:
puts("\nDime la nueva nota del Primer trimestre:\n");
scanf("%f", &nota_n1t);
notas[j].nota1=nota_n1t;
printf("La nueva nota del primer trimestre para el alumno %s %s es: \n%0.2f", notas[j].alumno,notas[j].apellido,notas[j].nota1);
break;
case 2:
puts("\nDime la nueva nota del Segundo trimestre:\n");
scanf("%f", &nota_n2t);
notas[j].nota2=nota_n2t;
printf("La nueva nota del Segundo trimestre para el alumno %s %s es: \n%0.2f", notas[j].alumno,notas[j].apellido,notas[j].nota2);
break;
case 3:
puts("\nDime la nueva nota del Tercer trimestre:\n");
scanf("%f", &nota_n3t);
notas[j].nota3=nota_n3t;
printf("La nueva nota del Tercer trimestre para el alumno %s %s es: \n%0.2f", notas[j].alumno,notas[j].apellido,notas[j].nota3);
break;
}
break;
}
}
}
} else {
puts("\nNo se ha encontrado ese apellido\n");
}
}
} else {
system("cls");
puts("\n \aRegistro vacio\n");
}
}
The function:
void eliminar_alumno(struct alumnos notas[20],int n){
int id_eli = 0, r = 0;
mostrar_notas(notas,n);
puts("\nInserta la id del alumno a eliminar\n");
scanf("%d",&id_eli);
for(r = 0;r < n;r++){
if (notas[r].id = id_eli){
notas[r].id=notas[r+1].id;
n--;
}
}
}

Compile with warnings:
if (notas[r].id = id_eli){
should be
if (notas[r].id == id_eli){
Also, this is wrong:
if (media=5 & media>6){
Suficiente = [5 - 6) right? Then you want if (media >= 5 && media < 6)

Try the following changes in your eliminar_alumno function-
for(r = 0;r < n;r++){
if (notas[r].id == id_eli){
for(i=r;i < (n-1);i++)
notas[i]=notas[i+1];
notas[i].id -= 1;
n--;
}
}

Related

incorrect output data after the second index

So im doing this homework for my programming class, but the data output doesnt match with the theoretical data output that it shoud be printing and it gives me a big number.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void menu(int *);
void CalculoVentas(float ventas[50][7], float ventasXproductos[50], int);
void PromedioDiario(float promedioXdia[7], float ventas[50][7], int);
int main()
{
char ciclos;
int sel, cont = 0, i = 0;
float ventas[50][7], ventasXproductos[50], promedioXdia[7];
bool lectura = false;
while(sel != 5)
{
menu(&sel);
system("cls");
switch(sel)
{
case 1:
printf("Hay Productos?: S/N ");
fflush(stdin);
scanf("%c", &ciclos);
while(ciclos == 's' || ciclos == 'S' && cont < 50)
{
for(i=0;i<7;i++)
{
do
{
printf("Teclea los ingresos generados por el producto %d el dia %d: ", cont+1, i+1);
scanf("%f", &ventas[cont][i]);
}while(ventas[cont][i] < 0);
printf("\n\n");
}
cont++;
system("cls");
printf("Hay Productos?: S/N ");
fflush(stdin);
scanf("%c", &ciclos);
lectura = true;
}
break;
case 2:
if(lectura == false)
printf("Primero ingresa los datos del producto!!!\n");
else
CalculoVentas(ventas, ventasXproductos, cont);
break;
case 3:
if(lectura == false)
printf("Primero ingresa los datos del producto!!!\n");
else
PromedioDiario(promedioXdia, ventas, cont);
break;
case 4:
if(lectura == false)
printf("Primero ingresa los datos del producto!!!\n");
else
printf("%35s\n", "Total de ventas");
for(i=0;i<cont;i++)
printf("%d %.2f\n", i+1, ventasXproductos[i]);
printf("\n\n\n\n");
printf("%35s\n", "Promedio de ventas por dia");
for(i=0;i<7;i++)
printf("Dia %d: %.2f\n", i+1, promedioXdia[i]);
printf("\n\n\n\n");
break;
}
}
}
void menu(int *seleccion)
{
printf("%20s\n%s\n%s\n%s\n%s\n%s\n", "Menu de opciones", "1.-lectura de datos", "2.-calculo de ventas por producto", "3.-promedio de ventas de cada dia", "4.-imprimir resultados", "5.-salir");
do{
printf("Seleccione una opcion: ");
scanf("%d", &*seleccion);
}while(*seleccion <= 0 || *seleccion > 5);
}
void CalculoVentas(float ventas[50][7], float ventasXproducto[50], int cont)
{
//realizar el calculo de ventas por productos, nada más es la suma de las ventas de todos los dias por producto
int i,j;
for (i = 0 ; i<cont ; i++)
for (j = 0 ; j < 7 ; j++)
ventasXproducto[i] = ventasXproducto[i] + ventas[i][j];
}
void PromedioDiario(float promedioXdia[7], float ventas[50][7], int cont)
{
int i,j;
for(i=0;i<7;i++)
{
for(j=0;j<cont; j++)
promedioXdia[i] = promedioXdia[i] + ventas[j][i];
promedioXdia[i] = promedioXdia[i]/cont;
}
}
the output is something like this, I try to type simple data so i can know easly if its wrong
Total de ventas
1 70.00
2 5103881324019006800000000000000000.00
3 210.00
4 280.00
Promedio de ventas por dia
Dia 1: 25.00
Dia 2: 25.00
Dia 3: 25.00
Dia 4: 1291386862541487300000000000000000.00
Dia 5: 25.00
Dia 6: -1.#R
Dia 7: 25.00
when i was trying to get help someone told me that i might be not initializing correctly the variables but i couldnt find any issue with that
When the program begins, sel is uninitialized and contains a garbage value. This garbage value is used in the while condition on its first iteration
while(sel != 5)
and as such invokes Undefined Behaviour.
You must restructure your loop to not read this uninitialized value, or simply initialize sel (to something other than 5).
Similarly, the contents of ventas, ventasXproductos, and promedioXdia are all uninitialized as well.
This means statements such as
ventasXproducto[i] = ventasXproducto[i] + ventas[i][j];
/* ... and ... */
promedioXdia[i] = promedioXdia[i]/cont;
will be operating with garbage values to start.
You can fix this by initializing your arrays:
float ventas[50][7] = { 0 }, ventasXproductos[50] = { 0 }, promedioXdia[7] = { 0 };
You should not ignore the return value of scanf. You should always check that its return value is the expected number of successful conversions, otherwise you will find yourself operating on incomplete data.
/* An example */
if (2 != scanf("%d%d", &num1, &num2)) {
/* handle failure */
}
(Better yet: avoid using scanf, and use fgets and sscanf to read and parse lines of input.)
You should clarify this expression by adding more parenthesis, otherwise you will run into issues with operator precedence:
while ((ciclos == 's' || ciclos == 'S') && cont < 50)
case 4 of the switch has misleading indentation. Only the first statement with the call to printf is contained within the else block. It is read as:
if(lectura == false)
printf("Primero ingresa los datos del producto!!!\n");
else
printf("%35s\n", "Total de ventas");
for(i=0;i<cont;i++)
printf("%d %.2f\n", i+1, ventasXproductos[i]);
/* ... */
Your lectura flag will not protect you from operating on incomplete data if this is selected. Enclose the code with curly braces:
case 4:
if(lectura == false) {
printf("Primero ingresa los datos del producto!!!\n");
} else {
printf("%35s\n", "Total de ventas");
for(i=0;i<cont;i++)
printf("%d %.2f\n", i+1, ventasXproductos[i]);
/* ... */
}
break;
Note that in &*seleccion, & and * balance each other out. This resolves to the same value as just writing seleccion would.
Note that fflush(stdin); is also (technically) Undefined Behaviour, and should not be relied upon.

Random number when allocation struct integer (int) in C

I'm allocating dynamically the amount of ram that I will need for my program. It is working fine until I came across this problem. This is when I type the content that is going to be inside of my one item that is inside the struct which is referred to as an Integer if I type more than 10 number for example (12345678912) it will send me back, a completely different number.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
struct cadastro
{
char email[50];
char nome[20];
int idade;
int telefone;
};
int main()
{
int quantidade_cadastros;
int decisao = 1;
printf("\nQuantos cadastros deseja realizar?\n");
printf("Quantidade: ");
scanf("%d", &quantidade_cadastros);
getchar();
//Definindo a quantidade de cadastros que podem ser realizados no struct e o nome que sera usado para chamar o struct
struct cadastro *cad;
cad = NULL;
cad = malloc(quantidade_cadastros * sizeof(struct cadastro));
for (int i = 0; i < quantidade_cadastros; i++)
{
printf("\nDigite o NOME da pessoa que sera inserida no indice %d: ", i);
fgets(cad[i].nome, 20, stdin);
printf("Digite o EMAIL da pessoa que sera inserida no indice %d: ", i);
fgets(cad[i].email, 50, stdin);
printf("Digite a IDADE da pessoa que sera inserida no indice %d: ", i);
scanf("%d", &cad[i].idade);
getchar();
printf("Digite o TELEFONE da pessoa que sera inserida no indice %d: ", i);
scanf("%d", &cad[i].telefone);
getchar();
}
for (int i = 0; i < quantidade_cadastros; i++)
{
printf("\nNOME: %s", cad[i].nome);
printf("EMAIL: %s", cad[i].email);
printf("IDADE: %d", cad[i].idade);
printf("\nTELEFONE: %d", cad[i].telefone);
printf("\n");
}
printf("\n");
free(cad);
}
Here is the input I entered:
Quantos cadastros deseja realizar?
Quantidade: 2
Digite o NOME da pessoa que sera inserida no indice 0: Test1
Digite o EMAIL da pessoa que sera inserida no indice 0: Email1
Digite a IDADE da pessoa que sera inserida no indice 0: 19
Digite o TELEFONE da pessoa que sera inserida no indice 0: 1234567891
Digite o NOME da pessoa que sera inserida no indice 1: Test2
Digite o EMAIL da pessoa que sera inserida no indice 1: Email2
Digite a IDADE da pessoa que sera inserida no indice 1: 18
Digite o TELEFONE da pessoa que sera inserida no indice 1: 12345678912
NOME: Test1
EMAIL: Email1
IDADE: 19
TELEFONE: 1234567891
NOME: Test2
EMAIL: Email2
IDADE: 18
TELEFONE: -539222976
You are running into an int overflow. The second 'phone number' you typed in is larger than an int can store, and you are not verifying the reading. The scanf probably failed, and stored nothing in the int - which means there is still random bits in its memory area, as you never cleared it. The printf then prints those random information.
You should always verify that scanf was successful - it will return the number of successfully assigned fields, here 1. So you should use `if (scanf(...) != 1) { /handle error/ }, instead of ignoring it.

Why is the output of my results 0.0000?

Hey people this is my first question on this page. I have been learning C in university and I have to complete an activity where I insert name, surname and 4 grades, of 3 different students. After that I must calculate average grade, maximum and minimum grade, and print those results on screen.
The program compiles correctly without errors, but when the results are printed, the program says that average grade, maximum and minimum grade are all equal to 0.000000.
I really don't know what the problem is and I would apreciate if anyone could check it out and help me in any way.
(Sorry if you don't understand the variable names, I'm from Argentina and they are in Spanish)
#include <stdio.h>
#define N 50
struct alumno
{
char nombre [N];
char apellido [N];
float nota1;
float nota2;
float nota3;
float nota4;
} alumno1, alumno2, alumno3;
struct notas
{
float prom;
float nmax;
float nmin;
} notas1, notas2, notas3;
float promedio (struct alumno a, struct notas b)
{
b.prom = (a.nota1 + a.nota2 + a.nota3 + a.nota4)/4;
return b.prom;
};
float notamaxima (struct alumno a, struct notas b)
{
if ((a.nota1>a.nota2) && (a.nota1>a.nota3) && (a.nota4>a.nota4))
{
b.nmax=a.nota1;
}
else if ((a.nota2>a.nota1) && (a.nota2>a.nota3) &&
(a.nota2>a.nota4))
{
b.nmax=a.nota2;
}
else if ((a.nota3>a.nota1) && (a.nota3>a.nota2) &&
(a.nota3>a.nota4))
{
b.nmax=a.nota3;
}
else if ((a.nota4>a.nota1) && (a.nota4>a.nota2) &&
(a.nota4>a.nota3))
{
b.nmax=a.nota4;
}
return 0;
};
float notaminima (struct alumno a, struct notas b)
{
if ((a.nota1<a.nota2) && (a.nota1<a.nota3) && (a.nota1<a.nota4))
{
b.nmin=a.nota1;
}
else if ((a.nota2<a.nota1) && (a.nota2<a.nota3) &&
(a.nota2<a.nota4))
{
b.nmin=a.nota2;
}
else if ((a.nota3<a.nota1) && (a.nota3<a.nota2) &&
(a.nota3<a.nota4))
{
b.nmin=a.nota3;
}
else if ((a.nota4<a.nota1) && (a.nota4<a.nota2) &&
(a.nota4<a.nota3))
{
b.nmin=a.nota4;
}
return 0;
};
int main ()
{
struct alumno;
struct notas;
printf ("Ingrese nombre del alumno:\n");
fgets (alumno1.nombre, N, stdin);
printf ("Ingrese apellido del alumno:\n");
fgets (alumno1.apellido, N, stdin);
printf ("Ingrese la primera nota del alumno:\n");
scanf ("%f", &alumno1.nota1);
printf ("Ingrese la segunda nota del alumno:\n");
scanf ("%f", &alumno1.nota2);
printf ("Ingrese la tercera nota del alumno:\n");
scanf ("%f", &alumno1.nota3);
printf ("Ingrese la cuarta nota del alumno:\n");
scanf ("%f", &alumno1.nota4);
promedio (alumno1, notas1);
notamaxima (alumno1, notas1);
notaminima (alumno1, notas1);
printf ("El promedio del primer alumno es %f\n", notas1.prom);
printf ("La nota maxima del primer alumno es %f\n", notas1.nmax);
printf ("La nota minima del primer alumno es %f\n", notas1.nmin);
printf ("Ingrese nombre del alumno:\n");
fgets (alumno1.nombre, N, stdin);
printf ("Ingrese apellido del alumno:\n");
fgets (alumno1.apellido, N, stdin);
printf ("Ingrese la primera nota del alumno:\n");
scanf ("%f", &alumno2.nota1);
printf ("Ingrese la segunda nota del alumno:\n");
scanf ("%f", &alumno2.nota2);
printf ("Ingrese la tercera nota del alumno:\n");
scanf ("%f", &alumno2.nota3);
printf ("Ingrese la cuarta nota del alumno:\n");
scanf ("%f", &alumno2.nota4);
promedio (alumno2, notas2);
notamaxima (alumno2, notas2);
notaminima (alumno2, notas2);
printf ("El promedio del segundo alumno es %f\n", notas2.prom);
printf ("La nota maxima del segundo alumno es %f\n", notas2.nmax);
printf ("La nota minima del segundo alumno es %f\n", notas2.nmin);
printf ("Ingrese nombre del alumno:\n");
fgets (alumno1.nombre, N, stdin);
printf ("Ingrese apellido del alumno:\n");
fgets (alumno1.apellido, N, stdin);
printf ("Ingrese la primera nota del alumno:\n");
scanf ("%f", &alumno3.nota1);
printf ("Ingrese la segunda nota del alumno:\n");
scanf ("%f", &alumno3.nota2);
printf ("Ingrese la tercera nota del alumno:\n");
scanf ("%f", &alumno3.nota3);
printf ("Ingrese la cuarta nota del alumno:\n");
scanf ("%f", &alumno3.nota4);
promedio (alumno3, notas3);
notamaxima (alumno3, notas3);
notaminima (alumno3, notas3);
printf ("El promedio del tercer alumno es %f\n", notas3.prom);
printf ("La nota maxima del tercer alumno es %f\n", notas3.nmax);
printf ("La nota minima del tercer alumno es %f\n", notas3.nmin);
return 0;
}
I think the function here you used doesn't make any changes
float promedio (struct alumno a, struct notas b)
All parameters you send to the function need to be the address type
like this
void promedio (struct alumno *a, struct notas *b)
.
.
.
promedio (&alumno3, &notas3);
The question about call by value & call by reference
Another question is you always return 0 in every each function
Why not use 'void' or return the calculation results , and the result must need to be accepted by a parameter
float notamaxima (struct alumno a, struct notas b)
{
if ((a.nota1>a.nota2) && (a.nota1>a.nota3) && (a.nota4>a.nota4))
{
return a.nota1;
}...
}
b.nmax = notamaxima (alumno1, notas1);
It's impossible your professors didn't know that.
sry for my eng

Unable to start the int correctly and keep the number back from the function

First at all sorry for my english, i will try my best.
Well, here's the problem, I am making a C program just for learning, the program is basically a students manager, I wanted to add a final function in which the user would be prompted by an id to by remove and after inserting that id the student would be eliminate from the database, the problem is... when the data is inserted in the first case there's added a subroutine that automatically adds the id as and int in the struct,problem is it shows a random int number like "43405" or so I thought to start the integer to 1, the problem is when the function is re-called again id will back to be 1 and that just simply don't work.
P.D:I read some of you guys told me about to make the code more readable, can you give me a nice "tutorial" or so to 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", &notas[n].nota1 );
system("cls");
puts("\nDime la Segunda nota trimestral del Alumno[1.23]\n");
scanf("%f", &notas[n].nota2 );
system("cls");
puts("\nDime la Tercera nota trimestral del Alumno[1.23]\n");
scanf("%f", &notas[n].nota3 );
n++;
system("cls");
puts("\nQuieres volver a insertar otro?[si|no]\n");
scanf("%3s", resp);
strlwr(resp);
}
return n;
return id_alumno;
}
Main for 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 by value, so to make id_alumno modifiable you need to pass it as a pointer:
int insertar_notas(struct alumnos notas[20], int n,int *id_alumno) { // id_alumno is now a pointer to int inside this 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 * to access the value
notas[n].id=id_alumno;
(*id_alumno)++; // Use * to access the value. ++ has higher precedence than *, so need parenthesis around *id_alumno
...
And in the call from main:
n=insertar_notas(notas, n, &id_alumno); // address-of id_alumno
Also, you need to initialize id_alumno in main:
int menu = 0, n = 0, id_alumno = 1;

My program starts the loop before scan is done

I have a little problem, my program works well until it arrives to the final step, a scanf which asks for continuation of the loop. The problem is that this scan isn't working, but the following system("cls") works. Looks like javascript when async.
Here is the code.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char elegir_dificultad;
int dificil = 1;
printf("Desea que se le indique si el numero es menor o mayor? \n s/n \n");
scanf("%c",&elegir_dificultad);
if(elegir_dificultad == 's'){
dificil = 0;
}
while(1){
int aleatorio, cont, introducido;
cont = 1;
aleatorio = rand()%101;
printf("%d",aleatorio);
int fallo = 1;
while(fallo){
printf("Introduce el numero, intento numero %d \n", cont);
scanf("%d",&introducido);
if(introducido == aleatorio){
fallo = 0;
}
if(cont == 10){
break;
}
if(!dificil){
if(introducido < aleatorio){
printf("El numero introducido es menor que el aleatorio \n");
}
if(introducido > aleatorio){
printf("El numero introducido es mayor que el aleatorio \n");
}
}
if(fallo){
cont++;
}
}
char continuar;
if(fallo){
printf("Has perdido... el numero era %d \n Quieres repetirlo? s/n \n",aleatorio);
scanf("%c",&continuar);
if(continuar=='n'){
break;
}
system("cls");
}else{
printf("°Has ganado! el numero era %d \n Quieres repetirlo? s/n \n",aleatorio);
scanf("%c",&continuar);
if(continuar=='n'){
break;
}
system("cls");
}
}
system("PAUSE");
return 0;
}
This problem is because of \n character left behind by the previous scanf. Place a space before each %c specifier in scanf to eat up \n. .
scanf(" %c", &introducido);
...
scanf(" %c",&continuar);

Resources