Difficulty in C: read two functions at the same time[Completed]

Asked

Viewed 193 times

-1

I would like to know how to perform two functions at the same time in c Being one while and the other a scanf:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#include <windows.h> //para usar o sleep

int main(){
    setlocale(LC_ALL, "Portuguese");
    char name[10];
    int sec=0,chose,monster=64,Fome=10,Higiene=10,Felicidade=10,i=0;

printf("Insira o Nome do Monstrinho.\n");
scanf("%s",name);

system("cls"); // Limpar a tela

while(i<80000){
system("cls");//limpa a tela windows    
printf("###########################################\n");
printf("#Nome: %s                                 #\n",name);
printf("#Tempo de Vida : %ds                      #\n",sec);
printf("#Fome:%d   Felicidade;%d  Higiene:%d      #\n",Fome,Felicidade,Higiene); 
printf("#                                         #\n"); 
printf("#                                         #\n");
printf("###########################################\n");
printf("(1)Comida (2)Carinho (3)Banho (4)...\n");
Sleep(1000); //função sleep
sec++; //segundos
}

It would be before or after the { (key of while), and how can I implement.

    printf("Digite uma opção:\n");
    scanf("%d",chose); //variavel de escolha de ação
}
  • 1

    Do you really need to do this? Know that in this case you will be working with parallel processing. Within the parallel processing models that exist, I don’t know which one is most suitable for a CLI game (I’m considering your tamagoshi a game, by the way), but I’m biased to a shared variables model (see my answer on CPI).

  • Yeah, you gotta do that

  • Maybe you have a better solution, give more information about what you are doing and why you need it, you are having some problem at the moment?

  • Good evening, then work and perform a menu that updates with seconds(sec) using the Sleep function, but did not find a way to work also the scanf together.

1 answer

-3


A possible solution would be:

// todo código anterior
    printf("(1)Comida (2)Carinho (3)Banho (4)...\n");

    printf("Digite uma opção:\n");
    scanf("%d",chose); //variavel de escolha de ação

    Sleep(1000); //função sleep
    sec++; //segundos
}

What’s he gonna do in a suit: inside your loop while it will ask to type an option at each loop iteration, wait 1 second and then follow the code, realize that the waiting time for the input does not count, that is, the user can take 5 minutes to decide to choose one of the options and yet after choosing it will wait 1 second to complete, this because you set the 1000 milliseconds (1 second divided by one thousand) pause.

Right after the user chooses the option, you will need to put a flow control as a if for each of the options, I recommend you use the flow switch - case he is more suitable than the if for these cases where a variable can assume different values, in addition to being more readable and make the code more elegant, all this should be inside your loop while, realize that your loop is the flow control of your application, so all actions will stay within it to maintain the cycle.

  • It’s not working yet

  • @Raullima, what is your problem?

  • does not update the sec (seconds) variable in while.

  • @Raullima, but understand that you don’t use it in while, see that this is called an "infinite loop" i.e., you pass a true condition (true) and it will never leave this loop, you start the variable i with 0 and your loop says this: "while i is less than 80000", but the value of i is never updated, that is, it will always be smaller than 80000, that amounts to a while(true).

  • Already the variable secbegins with 0 and is increasing at the end of the loop, that is, in the next cycle the value will change.

  • Legal negative personnel without explaining the reason or presenting a more appropriate solution. (y)

  • @bruno101: its solution does not meet the request of the author of the question, it executes the commands sequentially while the question asks for the executions to occur at the same time: in parallel. View comments of Jefferson Quesado and Maniero on the question.

  • @anonimo, I presented the answer to what he wanted and I made recommendations out of the question, including in comments, if it did not solve would simply not approve the answer and then yes understand the negative, now negative for no reason or because not liked only, there escapes the scope.

  • So the problem was not solved. but I did not deny

  • So @Raullima, I asked you what’s going on, what’s going on with your program to understand exactly where it’s not working to help you.

  • the job is to perform a menu that updates with seconds(sec) using the Sleep function, but I did not find a way to work the scanf together as well. Problem, the scanf in reads inside the loop, I wonder if there is any way that the loop continues together with the scanf or separated, because inside it, the loop only continues with the typing of any number, breaking the screen update proposal

  • @Raullima, now it’s clear what you want to do, well, to do that will be a little more complicated, because you need to use threads, Currently your project follows a procedural logic, that is, one step after the other, one instruction needs to wait for the other to be executed, it is not that my answer is wrong, it works, but it is not suitable to what you need. Using threads you can make two or more functions run simultaneously, so you will isolate the second counter in a thread and your loop in another.

  • @Raullima, here we ran into some possible problems, as far as I know, no compiler implementation C for windows has support for threads making it necessary to have some external lib, I don’t know which version of gcc has been implemented natively, but if you are using a previous implementation, you will have the same problem as in windows, plus the use of threads is extremely discouraged in C and in the C++, because the chances of problems like memory segmentation are quite large.

  • 1

    gets guy following his first advice, implementing a little more, even at the moment problem solved

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.