Counter in C displaying negative number - c

I've started recently to learn how to program in C. I'm following a class online and one of the projects we have to do is create a game where the computer comes up with a random number between two constants, and the user is supposed to find it with the minimal amount of tries possible.
I'm trying to implement a way of counting the tries each time the program passes through the loop (adding +1 to the counter) but all i get is that the number of tries is -472188416 whenever I execute the program as you can see here Program after execution. I don't understand why... Thank you in advance.
Here is the code I made :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
const int MIN = 1;
int nombreMystere = 0, nombreUser = 0, continuerPartie = 1, MAX = 0, Niveau = 0;
srand(time(NULL));
int compteurCoups = 1;
do
{
printf("Bienvenue qu jeu du numero mystere\n");
printf("Trouvez le bon numero en un minimum de coups\n\n");
printf("Tapez 1 pour le mode facile\n");
printf("Tapez 2 pour le mode moyen\n");
printf("tapez 3 pour le mode difficile\n");
switch(Niveau)
{
case 1:
MAX = 10;
nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;
case 2:
MAX = 100;
nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;
case 3:
MAX = 1000;
nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;
default:
printf("Veuillez introduire un nombre entre 1 et 3 : ");
scanf("%d", &Niveau);
break;
}
do
{
printf("Quel est le nombre ? ");
scanf("%d", &nombreUser);
if (nombreUser > nombreMystere)
{
printf("C'est moins !\n");
nombreUser++;
}
else if (nombreUser < nombreMystere)
{
printf("C'est plus !\n");
nombreUser++;
}
else
{
printf("Bravo, vous avez trouve le nombre mystere en %d essais !!!\n"), compteurCoups;
printf("Voulez vous rejouer ?\n");
printf("Si oui appuyer sur 1, sinon si vous voulez quitter appuyez sur 2\n");
scanf("%d", &continuerPartie);
printf("\n");
}
}while (nombreUser != nombreMystere);
}while (continuerPartie == 1);
return 0;
}

You increase nombreuser in stead of compteurCoups.

It's a syntax error :
printf("Bravo, vous avez trouve le nombre mystere en %d essais !!!\n"), compteurCoups;
Should be
printf("Bravo, vous avez trouve le nombre mystere en %d essais !!!\n", compteurCoups);
Don't forget that "compteurCoups" is an argument of the printf function. Thus, it belongs between the parenthesis after the function name.

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.

How can I make my code to repeat the same question in C

I wanted to make my code to repeat the question 3 times, but it stops as soon as i give my first answer. My code is:
void initialiser_jeu(S_joueur tab_joueur[3])
{
int i=0;
int classe;
while(i<3)
{
printf("Donnez la classe du joueur %d (tapez 1 pour nain, 2 pour elfe, 3 pour mage) :",i+1);
scanf("%d",&classe);
switch (classe)
{
case 1:
printf("\n Vous avez choisi nain, vous disposez donc de 300 PV, 50 d'attaque et 1 de vitesse\n");
tab_joueur[i].HP_MAX=300, tab_joueur[i].ATT=50, tab_joueur[i].vitesse=1, tab_joueur[i].exp=0,tab_joueur[i].soin=0,tab_joueur[i].niveau=1,tab_joueur[i].points=0,tab_joueur[i].cle=0;
tab_joueur[i].HP = tab_joueur[i].HP_MAX;
tab_joueur[i].ATT = tab_joueur[i].ATT_MAX;
break;
case 2:
printf("\n Vous avez choisi elfe, vous disposez donc de 200 PV, 70 d'attaque et 2 de vitesse \n");
tab_joueur[i].HP_MAX=200, tab_joueur[i].ATT=70, tab_joueur[i].vitesse=2, tab_joueur[i].exp=0,tab_joueur[i].soin=0,tab_joueur[i].niveau=1,tab_joueur[i].points=0,tab_joueur[i].cle=0;
tab_joueur[i].HP = tab_joueur[i].HP_MAX;
tab_joueur[i].ATT = tab_joueur[i].ATT_MAX;
break;
case 3:
printf("\n Vous avez choisi mage, vous disposez donc de 200 pv, 50 d'attaque, 1 de vitesse et un sort de soin\n ");
tab_joueur[i].HP_MAX=200, tab_joueur[i].ATT=50, tab_joueur[i].vitesse=1, tab_joueur[i].exp=0,tab_joueur[i].soin=1,tab_joueur[i].niveau=1,tab_joueur[i].points=0,tab_joueur[i].cle=0;
tab_joueur[i].HP = tab_joueur[i].HP_MAX;
tab_joueur[i].ATT = tab_joueur[i].ATT_MAX;
break;
i++;
}
}
for(i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tab_joueur[i].inventaire[j]=0;
}
}
}
It seems like your scanf is implemented correctly, if you write down at the comments, under this post, the input that you gave will be helpful.
So you haven't add i++; above every break;, so the one i++ you have added in the third case is wrong, as #kaylum said.
To explain it a little bit further, if the program passes through break; it leaves the case, so the i statement never gets increased. Therefore, your program loops until you choose three times the third option. So every case should be in the form of:
case 1:
printf("\n Vous avez choisi nain, vous disposez donc de 300 PV, 50 d'attaque et 1 de vitesse\n");
tab_joueur[i].HP_MAX=300, tab_joueur[i].ATT=50, tab_joueur[i].vitesse=1, tab_joueur[i].exp=0,tab_joueur[i].soin=0,tab_joueur[i].niveau=1,tab_joueur[i].points=0,tab_joueur[i].cle=0;
tab_joueur[i].HP = tab_joueur[i].HP_MAX;
tab_joueur[i].ATT = tab_joueur[i].ATT_MAX;
i++; // here
break;

Probleme when i call a function in c

i'm a student and i want to make a program that lunch 3 different games, i've maked a menu with switch/case but when i've copied my friend game ( i just made the menu) i can't lunch is function can you help me, sorry for my low skill in code and in english..
the function that i can't lunch is "int pfc(int argc, char* argv[])" and i call it in the third case at line 40.
thanks for who those gonna help us..
the code :`
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <string.h>
int menu();
int choixjeu();
int text(int);
int pfc(int argc, char* argv[]);
int main()
{
menu();
return 0;
}
int menu(){
printf("\n ======================================\n | bienvenue sur uGame |\n ======================================\n");
printf("\n selectionner un jeu !\n");
printf("\n 1.Pendu\n 2.Puissance 4\n 3.Pierre feuille ciseaux\n 4.quitter\n");
printf("\n Entre le numero de votre selection !\n");
printf("\n\n");
choixjeu();
}
int choixjeu(){
int choixjeu;
scanf("%d",&choixjeu);
switch(choixjeu){
case 1:
printf("pendu");
break;
case 2:
printf("puissance4");
break;
case 3:
pfc();
break;
case 4:
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n Au revoir merci !\n\n\n\n\n\n\n\n\n\n\n\n\n");
break;
default:
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n touche non assigne veuiller recommencer... \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
Sleep(1500);
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
menu();
break;
}
}
int pfc(int argc, char* argv[]){
char pierre[] = "pierre", feuille[] = "feuille", ciseaux[] = "ciseaux";
int choixM = 0, choixO = 0, manche = 0, gagneM = 0, gagneO = 0, nul = 0, choixJoueur = 0;
srand(time(NULL));
choixO = (rand() % 3+1);
printf("Bienvenue dans Pierre , Feuille , Ciseaux ! \n\n");
printf("La partie se deroulera en 3 manche !\n\n");
do{
printf("Quel est votre choix ? (tapez votre choix entre 1, 2 & 3)\n"
"1 - Pierre\n"
"2 - Feuille\n"
"3 - Ciseaux\n\n"
"Votre choix : ");
scanf("%d",&choixJoueur);
choixJoueur = text(choixJoueur);
if(strcmp(choixJoueur, pierre) == 0)
choixM = 1;
else if(strcmp(choixJoueur, feuille) == 0)
choixM = 2;
else if(strcmp(choixJoueur, ciseaux) == 0)
choixM = 3;
printf("%d\n",choixM);
printf("%d\n\n",choixO);
if(choixM == choixO){
printf("Egalite !\n\n");
nul++;
}
else if ((choixM == 1 && choixO == 3)||(choixM == 2 && choixO == 1) || (choixM == 3 && choixO == 2)){
printf("Bravo ! Vous avez gagne !\n\n");
gagneM++;
}
else if((choixM == 1 && choixO == 2) || (choixM == 2 && choixO == 3) || (choixM == 3 && choixO == 1)){
printf("L'ordi a gagne ! ! \n\n");
gagneO++;
}
manche++;
while (manche>3)
srand(time(NULL));
choixO = (rand() % (3 - 1 + 1)) + 1;
}while(manche < 3);
printf("La partie en %d manches est terminee !\n", manche);
printf("Les score est : \n");
printf("\t Ordinateur : %d points\n",gagneO);
printf("\t Joueur : %d points\n\n",gagneM);
if(gagneO > gagneM)
printf("L'Ordinateur a gagne !");
else if(gagneM > gagneO)
printf("Bravo ! Vous avez gagne\n\n");
else
printf("Egalite, personne ne gagne...\n\n");
return 0 ;
}
int text(int choix){
switch(choix){
case 1 :
choix = "pierre";
break;
case 2 :
choix = "feuille";
break;
case 3 :
choix = "ciseaux";
break;
default :
printf("Erreur. Veuillez reccomencer en appuyant sur n'importe quelle touche. \n");
break;
}
return choix;
}```
pfc requires 2 arguments, int and char* []. You're providing 0 arguments when you call it in choixjeu. Furthermore, you're not using argc or argv in the pfc function, so you might as well change its definition to int pfc(void);, then you can call it as you are.
you forget to add the #include<stdio.h>,and int text(int)should turn to char text(int)because choixJoueur = text(choixJoueur);is int,but strcmp need string.

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

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--;
}
}

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