Counter unexpectedly increases within a loop

1

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
    
asked by Jose Hermosilla Rodrigo 09.12.2016 в 17:41
source

2 answers

4

It seems to me that you are failing in the outputs on the screen, try changing the last line of printf so I will leave you, also you must change the other forms of printing.

printf("Adios! Has utilizado %i intentos y no los has conseguido \n", intentos);

When I tried once, I indicated the 1. I hope I helped you.

    
answered by 09.12.2016 / 18:00
source
3

As you have been told, the cause of the error is:

&intentos

You are passing the address of the variable intentos , and not its value , which is what the function printf( ) expects.

In scanf( ) if you have to use a pointer , because this function needs a memory address in which to store the data.

In other words: &intentos != intentos .
The first one returns a pointer that, worth the redundancy, points to the memory address where the data is located.
The second returns the data itself, its value .

    
answered by 09.12.2016 в 18:58