Hi...
I have been trying to figure this out, but it has been an impossible task 4 me...:rolleyes:
this is a gasoline pump counter, and i have developed an infinite cycle to print the gas counter and the cash counter...:mrgreen: the problem is, i need a stopper for the cycle, where the idea is that the user stops the flow of gas by pressing ENTER... but i'm done with trying everything to do this...:mad:

The lines are in spanish, but they are just junk...:cool:

Please help me... i'd really appreciate that...:)

Here's the code:

I'm unable to view the attachment, problem with my firewall.
Seems like you wanna do 2 things:
1. Print teh counter
2. Accept user input.

If you wanna do both in parallel, use multi-threading. In this case it would be simple.
- fork() a child thread from the main thread.
- The thread function should just wait on user input till "ENTER" is pressed. When enter is pressed set a bool to true.
- In the main thread keep printing the counter in a loop whose terminating condition is the bool (viz set in child thread)

Here is the sample code in Win32.

#include <Windows.h>
#include <conio.h>
#include <iostream>

using namespace std ;

bool exit_now ;

DWORD WINAPI user_input_function(LPVOID iValue) ;
bool gas_counter_function() ;

int main()
{
    exit_now = false ;
    HANDLE hThread1 ;
    DWORD dwGenericThread;

    hThread1 = CreateThread(NULL, 0, user_input_function, NULL, 0, &dwGenericThread ) ;

    gas_counter_function() ;

    return 0 ;
}

bool gas_counter_function()
{
    char tmp[10] ;
    int j = 0 ;
    while( ! exit_now )
    {
        for( int i = 0; i <= 1000; i++ )
            sprintf( tmp, "%d", i ) ;
        cout << ++j << endl ;
    }
    return true ;
}

DWORD WINAPI user_input_function(LPVOID iValue)
{
    // 13 is newline in ascii
    while( 13 != getch() ) ;
    exit_now = true ;
    return 0 ;
}

One other thing, ideally the setting of exit_now (exit_now = true ;) should be an atomic operation. So use mutex if needed.

thnx for your help. now, this library (windows.h) is completely new for me so i did what i understood was right, and the program runs almost to perfection...

i have already adapted the source code you gave me to my program. Though, now it presents a new problem... the inputs are not running like they should, i have turned all of the text to engish so you can understand what is the purpose of the program.

#include <iostream.h>
#include <stdio.h>
#include "conio.c"
#include <time.h>
#include <Windows.h>

int end=0;
DWORD WINAPI user_input_function(LPVOID iValue);

main(){
   HANDLE hThread1;
   DWORD dwGenericThread;
   hThread1=CreateThread(NULL,0,user_input_function,NULL,0,&dwGenericThread);
   const float flow = 0.1549;
   const float super = 2.42;
   const float regular = 2.31;
   const float diesel = 1.86;
   char carb[30],comb,sure[30];
   float timer;
   time_t sec, diff;
   do{
      cout<<"\n                 Welcome to the gas station\n";
      cout<<"\n\nEnter the kind of fuel you want to buy\n\n-->";
      cin>>carb;
      comb=carb[0];
      while (!((comb=='s')||(comb=='S')||(comb=='d')||(comb=='D')||(comb=='r')||(comb=='R'))){
         cout<<"\n\nThe entered fuel is invalid.\n\aPlease enter it again\n\n-->";
         cin>>carb;
         comb=carb[0];
      }do{
         cout<<"Are you sure you want to buy ";
         if ((comb=='s')||(comb=='S')) printf("Super?\n\n-->");
         if ((comb=='r')||(comb=='R')) printf("Regular?\n\n-->");
     if ((comb=='d')||(comb=='D')) printf("Diesel?\n\n-->");
         cin>>sure;
         if (!((sure[0]=='s')||(sure[0]=='S')||(sure[0]=='n')||(sure[0]=='N'))) printf("\nLa respuesta ingresada es incorrecta\n\aPor favor Ingrese SI o NO\n");
      }while (!((sure[0]=='s')||(sure[0]=='S')||(sure[0]=='n')||(sure[0]=='N')));
   }while ((sure[0]=='n')||(sure[0]=='N'));
   if ((comb=='s')||(comb=='S')) printf("\n\nSuper actually costs $3.4f/Galon\n\n", super);
   if ((comb=='r')||(comb=='R')) printf("\n\nRegular actually costs $%3.4f/Galon\n\n", regular);
   if ((comb=='d')||(comb=='D')) printf("\n\nDiesel actually costs $%3.4f/Galon\n\n", diesel);
   cout<<"\n         Press >>ENTER<< to start dispensing the fuel\n"; 
   diff = clock();
   getchar();
   getchar();
   system("cls");
   while (end==0){
      sec = clock() - diff;
      timer=float(sec)/1000;
      gotoxy(15,4);
      if ((comb=='s')||(comb=='S')) printf("Super-->");
      if ((comb=='r')||(comb=='R')) printf("Regular-->");
      if ((comb=='d')||(comb=='D')) printf("Diesel-->");
      gotoxy(28,4);
      printf("%3.4f",timer*flow);
      gotoxy(15,5);
      printf("Dollar-->");
      gotoxy(28,5);
      if ((comb=='s')||(comb=='S')) printf("%3.4f",timer*flow*super);
      if ((comb=='r')||(comb=='R')) printf("%3.4f",timer*flow*regular);
      if ((comb=='d')||(comb=='D')) printf("%3.4f",timer*flow*diesel);
      gotoxy(2,8);
      cout<<"\n       Press >>ENTER<< again to stop the fuel flow\n";
      system("cls");
   }printf("\n>>Press >>ENTER<< to exit<<");
   getchar();
   _exit(0);
   return 0;
}

DWORD WINAPI user_input_function(LPVOID iValue){
      while (13!=getch())
            end=1;
      return 0;
}

//the prices are just for refference. That's basically it, so plz help me...

I didn't exactly get what you mean by "the inputs are not running like they should", but anyway you should add "<< flush" at teh end of every cout you have. It'll ensure that teh buffer of cout is flushed. Sometimes what happens is you see the input msg AFTER your prompt of cin.

E.g. change:

cout<<"\n Welcome to the gas station\n";
cout<<"\n\nEnter the kind of fuel you want to buy\n\n-->";
cin>>carb;

to

cout<<"\n Welcome to the gas station\n" << flush ;
cout<<"\n\nEnter the kind of fuel you want to buy\n\n-->" << flush ;
cin>>carb;

E.g. change:

cout<<"\n Welcome to the gas station\n";
cout<<"\n\nEnter the kind of fuel you want to buy\n\n-->";
cin>>carb;

to

cout<<"\n Welcome to the gas station\n" << flush ;
cout<<"\n\nEnter the kind of fuel you want to buy\n\n-->" << flush ;
cin>>carb;

Better (IMO):

cout<<"\n Welcome to the gas station\n";    // Don't need the flush on this line...
cout<<"\n\nEnter the kind of fuel you want to buy\n\n-->" << ends;

cin>>carb;

The ends ends an output stream and flushes the buffer.
But why would you want to output:

Enter the kind of fuel you want to buy

-->(input here)

Why not just output:

Enter the kind of fuel you want to buy: (input here)

:?:
Save all that blank space and use cout<<"\n\nEnter the kind of fuel you want to buy: " << ends; And don't forget that endl is the same as ends with a newline.

did you try to run the program i posted? i mean, the second one... because when i run it, when it asks me for the first input (and by input i mean about data entry), i cannot see what i'm writing, and the program slows down completely... i don't know if it is because of the compiler i'm using (bloodshed dev-cpp) or what...

anyway, i'm still stuck in there...

i've inferred somewhere out there how to use fork() o c, but the program i'm running is in c++... so i would loooove to know if there is something similar in c++

The problem is that you start the user_input thread little too early.
You start it and then you print other stuff for getting inputs for other things. So at the point when cin on lines 24, 28 and 35 are executed (in main thread), there is also another thread running doing getch() (in user_input_function, the child thread).
Now when you enter something, I really donno what's the behaviour, but my guess is it's undefined. Sometimes the input will go to main thread and sometimes it'll go to child thread, if it goes to child thread you don't see anything printed on the screen as getch() doesn't print what it gets. If it goes to main thread it'll be printed as cin prints what it gets.
So move line 13 to line 46 and that should solve your problem.
Also just to go easy on CPU you might wanna add a li'l sleep in user_input_function's loop. Say about 100 mili secs or so (beware of more than that).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.