I'm trying to do a small program in C ++ that asks the user for a range of values (xmin, xmax) , (ymin, ymax)
.
The aleatoriamente
program will choose two values between (xmin, xmax) y (ymin, ymax)
respectively and multiply them.
The program will ask in another step the number created by the user that is the result of the multiplication of both values and in case of failure aumentará el contador de intentos en 1
. And it will ask the user if he wants to continue testing.
The code I have is shown below:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int a, b;
int opc;
int intentos = 0;
int max1,max2,min1,min2;
int num1,num2,result,propuesta;
char repeat;
int defined;
do{
printf("--------------------------------\n");
printf("1. Indicar rango inicial y final\n");
printf("2. Generar multiplicacion\n");
printf("3. Salir del progrma\n");
printf("--------------------------------\n");
scanf("%d",&opc);
switch(opc) {
case 1: //system("cls");
printf("Dime el minimo del multiplicando 1: \n");
scanf("%d",&min1);
printf("Dime el maximo del multiplicando 1: \n");
scanf("%d",&max1);
printf("Dime el minimo del multiplicando 2: \n");
scanf("%d",&min2);
printf("Dime el maximo del multiplicando 2: \n");
scanf("%d",&max2);
printf("\n Has actualizado los rangos \n");
srand((unsigned) time(NULL)); //multiplicando 1
num1 = min1 + rand()%(max1-min1);
srand((unsigned) time (NULL)); //multiplicando 2
num2 = min2 + rand ()%(max2-min2);
result = num1*num2; // poner en variables
intentos = 0; //contador
defined = 1; // rangos definidos
break;
case 2:
if (defined == 0){ // rangos definidos?
printf("No has definido los rangos, vuelve a la opcion 1 del menu principal \n");
break;
}
do {
printf("Introduce el resultado que crees que pueda tener la operacion: \n");
scanf("%d", &propuesta);
//intentos++;
printf("intentos: " + intentos);
if(propuesta == result){
printf("Has acertado! \n");
printf("Para ello has necesitado %d intentos \n", &intentos);
//break;
} else {
printf("Has fallado! \n");
intentos++;
printf("Quieres intentarlo de nuevo? (S/N) \n %d", &intentos);
fflush(stdin);
repeat = toupper(getchar());
}
break;
} while (repeat != 'N');
break;
}
} while (opc != 3);
printf("Adios! Has utilizado %d intentos y no los has conseguido \n", &intentos);
return 0;
}
When compiling and executing it, I can check how in the first fault the counter has the value of 6618692
and although it keeps trying the counter does not change its value.
I have certain suspicions that the problem may be in the loop, although I certainly do not know. I'm new to C ++.
¿Dónde puede estar el error que causa que el contador tenga ese valor?
Here is an example of output:
--------------------------------
1. Indicar rango inicial y final
2. Generar multiplicacion
3. Salir del progrma
--------------------------------
1
Dime el minimo del multiplicando 1:
2
Dime el maximo del multiplicando 1:
3
Dime el minimo del multiplicando 2:
2
Dime el maximo del multiplicando 2:
3
Has actualizado los rangos
--------------------------------
1. Indicar rango inicial y final
2. Generar multiplicacion
3. Salir del progrma
--------------------------------
2
Introduce el resultado que crees que pueda tener la operacion:
2
intentos: Has fallado!
Quieres intentarlo de nuevo? (S/N)
6618692