Does anyone know how I can change the value of a variable from time to time?

-1

I am simulating an investor platform and I have variables where "stock shares" are kept, the investor registers and accesses a menu where there is an option to see the quotes, what I want to do is that the quote change every 2 minutes. This is what I have so far

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    #include <windows.h>
    #include <string.h>
    #define cls system("cls")  /*LIMPIAR LA PANTALLA*/
    #define flush fflush(stdin) /*LIMPIA LA MEMORIA (SE UTILIZA CUANDO SE SCANEAN VALORES CHAR Y COMIENZA A ARROJAR DATOS BASURA*/
    #define pause system("pause") //PAUSA EN LA PANTALLA

    int pos=-1; /*VARIABLE PARA CONTROLAR LA POSICION DEL INDICE AL MOMENTO DE HACER REGISTROS*/
    int i; /*VARIABLE QUE GUARDA LA POSICION DEL INDICE AL INICIAR SESION (UTILIZAR PARA ACCEDER A CUALQUIER INFORMACION DEL INVERSIONISTA)*/
    int acceso = 0; /*BANDERA PARA PODER ACCEDER AL MENU DEL INVERSIONISTA*/
    float monedas[5]; //VECTOR PARA ALMACENAR LOS PRECIOS DE LAS MONEDAS
    char nombres[5][15] = {"BITCOIN","ETHEREUM","RIPPLE","LITECOIN","DASH"}; //NOMBES DE LAS CRIPTOMONEDAS

    struct traders /*ESTRUCTURA DEL INVERSIONISTA CON SUS ATRIBUTOS*/
    {
        char usuario[20],password[20]; //Datos para inicio de sesion
        float balance, status; //Datos de inversion
    }investor[5]; //Arreglo de 5 inversionistas

    float random()//FUNCION QUE GENERA NUMEROS ALEATORIOS FLOTANTES
    {
        float a = 10/*ESTA VARIABLE ES EL NUMERO MAXIMO AL QUE GENERA NUMEROS ALEATORIOS, EN ESTE CASO DE 0 - 10*/,ran;
        int x;
        for(x=0;x<4;x++)
            ran = ((float)rand()/(float)(RAND_MAX)) * a;
        return ran; //SE RETORNA EL ULTIMO VALOR DE FOR PARA OBTENER NUMEROS QUE VARIEN MAS
    }

    void registrar()//FUNCION PARA REGISTRAR NUEVOS USUARIOS
    {
        cls;
        if(pos<4)
        {
            pos++;
            printf("\t---------------------REGISTRARSE-------------------- ");
            printf("\nUsuario: ");scanf("%s",&investor[pos].usuario);
            printf("Contrase%ca: ",164);scanf("%s",&investor[pos].password);
        }
        else
        {
            printf("NO SE ADMITEN MAS REGISTROS\n");
        }
    }

    void mostrar()//FUNCION PARA MOSTRAR LOS USUARIOS REGISTRADOS
    {
        cls;
        int j;
        if(pos==-1)
        {
            printf("AUN NO HAY REGISTROS\n");
        }
        else
        {
            printf("\t---------------------USUARIOS REGISTRADOS--------------------\n");
            for(j=0;j<=pos;j++)
            {
                printf("Usuario: %s \n",investor[j].usuario);
                printf("Contrase%ca: %s\n",164,investor[j].password);
                printf("----------------------------------------------------------------------\n");
            }
        }
    }

    void login() //FUNCION PARA INICIO DE SESION, COMPRUEBA QUE USUARIO Y CONTRASEÑA COINCIDAN
    {
        cls;
        char user[20],pass[20]; //Datos para comparar con los que estan en la estructura
        int band=0;
        printf("\t---------------------INICIAR SESION-------------------- ");
        if(pos==-1)
        {
            printf("\n\nAUN NO HAY REGISTROS PARA INICIO DE SESION\n");
            pause;
        }
        else
        {
            printf("\nUsuario: ");scanf("%s",&user);
            printf("Contrase%ca: ",164);scanf("%s",&pass);
            for(i=0;i<=pos;i++)
            {
                if(strcmp(user,investor[i].usuario)==0 && strcmp(pass,investor[i].password)==0)
                {
                    printf("\nIniciando sesion.......\n");
                    acceso = 1;
                    band++;
                    break;
                }
            }
            if(band==0)
                printf("\nEL USUARIO O CONTRASE%cA NO COINCIDEN\n",165);
                pause;
        }
    }

    void cotizacion()
    {
        int i;
        for(i=0; i<5; i++)
            monedas[i] = random();
    }


    int main()
    {
        srand((unsigned int)time(NULL)); //INICIADOR PARA NUMEROS ALEATORIOS
        int op; //OPCION PARA NAVEGAR EN LOS MENUS
        do
        {
            cls;
            printf("\t\t---------------------BIT TRADER-------------------- \n1)Iniciar Sesion \n2)Registrarse \n3)Mostrar usuarios/contrase%cas \n4)Salir \n: ",164); //MENU PRINCIPAL
            flush;
            scanf("%d",&op);
            switch(op)//MENU PRINCIPAL
            {
                case 1:
                    login();
                    if(acceso==1) //SI USUARIO Y CONTRASEÑA COINCIDEN SE PASA EL MENU DEL INVERSIONISTA
                    {
                            do{
                                cls;
                                cotizacion();
                                Sleep(2000);
                                printf("\t\t---------------------Bienvenido %s-------------------- \n1)Ver cotizaciones \n2)Depositar a tu cuenta \n3)Invertir \n4)Ver balance actual \n5)Reporte \n6)Cerrar sesion \n: ",investor[i].usuario,164);//Menu del inversionista
                                flush;
                                scanf("%d",&op);
                                switch(op)//MENU DE INVERSIONISTA
                                { 
                                    case 1:
                                        cls;
                                        printf("\t\t---------------------COTIZACIONES--------------------\n");
                                        int j;
                                        for(j=0; j<5; j++)
                                        {
                                            printf("%s: ",nombres[j]);
                                            printf("%.2f\n",monedas[j]);
                                        }
                                        printf("\n");

                                        pause;
                                        break;
                                    case 2:
                                        printf("\t\t---------------------DEPOSITAR A TU CUENTA--------------------\n");
                                        pause;
                                        break;
                                    case 3:
                                        printf("\t\t---------------------INVERTIR--------------------\n");
                                        pause;
                                        break;
                                    case 4:
                                        printf("\t\t---------------------BALANCE ACTUAL--------------------\n");
                                        pause;
                                        break;
                                    case 5:
                                        printf("\t\t---------------------REPORTE--------------------\n");
                                        pause;
                                        break;
                                    case 6: //CIERRA SESION Y REGRESA AL MENU PRINCIPAL
                                        printf("\nCerrando sesion....\n");
                                        pause;
                                        break;
                                    default:
                                        printf("\nOpcion incorrecta, prueba de nuevo\n");
                                        pause;}
                                }while(op != 6);
                    }
                    acceso = 0;
                    break;
                case 2:
                    registrar();
                    pause;
                    break;
                case 3:
                    mostrar();
                    pause;
                    break;
                case 4:
                    printf("\nVuelva pronto....\n");
                    break;
                default:
                    printf("\nOpcion incorrecta, prueba de nuevo\n");
                    pause;
            }
        }while (op != 4);
        return 0;
        getch();
    }
    
asked by Arath Jimenez 28.11.2017 в 21:57
source

2 answers

0

What you can do is save a timestamp and check, before printing the values, if more than two minutes have passed since the last timestamp ... if yes, you update the values:

time_t marcaDeTiempo = 0;

// ...

time_t actual = time(NULL);
if( actual - marcaDeTiempo >= 120 ) // time_t almacena segundos
{
  // actualizamos la marca de tiempo
  marcaDeTiempo = actual;

  // actualizar valores
  // ...
}
    
answered by 29.11.2017 / 07:49
source
1

You can use the function Sleep(); (value in milliseconds) to be able to use it you must add the% liberation #include <windows.h> , to put 2 minutes = 120 seconds = > Sleep (120000); Or you can make pressing a key change the values (use enter '\ n'), use srand(time(NULL)); to define random as a function of time, and add the values with int valor = rand();

I hope I helped you. Put the code of the program you have made so we can help you a little more.

    
answered by 28.11.2017 в 22:10