Firstly, i press ENTER to exit the 1st do..while loop
and then the programe ask Rent Paid(y/n):
if i press n and then ENTER,
the program return rent_step2,
and it is supposed to wait for me before i press ENTER again


But it just break the 1st do..while loop and not waiting for me pressing ENTER
it skips to ask Rent Paid(y/n):

so how can i solve this problem....

I have also uploaded all of the source code

void rent_locker(){

rent_step1:
           //................
rent_step2:

   do{
       if(GetAsyncKeyState(VK_UP) != 0){
           //................
       }else if(GetAsyncKeyState(VK_DOWN) != 0){
           //................
       }else if(GetAsyncKeyState(VK_LEFT) != 0){
           //................
       }else if(GetAsyncKeyState(VK_RIGHT) != 0){
           //................
       }else if(GetAsyncKeyState(8) != 0){
           goto rent_step1;
       }
           //................
	   Sleep(300);
   }while(!(GetAsyncKeyState(13)));

rent_step3: 
   char confirm[10];
   confirm[0]='u';
   do{
      cout << "Rent Paid(y/n): ";
      cin >> confirm;
   }while(strlen(confirm)>1 || !(confirm[0]=='y'||confirm[0]=='Y'||
			      confirm[0]=='n'||confirm[0]=='N'));  
   
   if(confirm[0]=='y' || confirm[0]=='Y'){
	   cout << "*** PAID!!! ***";
   }else if(confirm[0]=='n' || confirm[0]=='N'){
	   confirm[0]='u';
	   goto rent_step2;
   }
   system("pause");
   return;
}

Well first off main() cannot be void... it must return int... so I changed it to

int main()       //changed void main() to int main()
{
  .............
}

Also the code below needs redeclaration of i
cuz its declared in the first for statement and it is not in scope in the second for statement. you gotta declare another variable for the second for statement.
See i isnt declared outside the first loop and is fixed below..

for(i=0;i<4;i++;){                                              // your also missing ;  which was fixed after the j++ and before the )
	   for(int j=0;j<25;j++){
		   if(check_available(stack_no,[b]i=0[/b],j)==1){                            // Changed i to i=0
			   x_position=j;
			   y_position=i;
			   break;
		   }
	   }

OR u can do the following:

int i=0;       // Put "i" outside the scope of the for function so it is defined for all functions below
   for(i<4;i++){
	   for(int j=0;j<25;j++){
		   if(check_available(stack_no,i,j)==1){
			   x_position=j;
			   y_position=i;
			   break;
		   }
	   }

As for getasynckey.. cant u just do:

if(keybd_event(VK_RETURN, 0, 0, 0); keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);)
{
      //code
}

Not sure if the above will work...

but Im trying to fix it right now... hopefully I come up with a solution... btw in other compilers, putting the variable inside the scope like that is illegal... microsoft's compiler for some reason accepts this and says its appropriate...

Due to not being able to edit previous posts after 30 mins, i posted the following below...

rent_step3: 
   gotoxy(0,16);
   string rental_price (locker_data[stack_no][y_position][x_position],2,2); 
   cout << "The rental price is $" << rental_price << endl;
   
   fflush(stdin);
   char confirm[10];
   confirm[0]='u';
   do{
      cout << "Rent Paid(y/n): "<< flush;
	  fflush(stdin);
      cin.clear();
      cin >> confirm;
   }while(strlen(confirm)>1 || !(confirm[0]=='y'||confirm[0]=='Y'||
			                     confirm[0]=='n'||confirm[0]=='N'));  

   fflush(stdin);
   if(confirm[0]=='y' || confirm[0]=='Y'){
	   confirm[0]='u';
	   cout << "*** PAID!!! ***";
	   cin.get();                                  //Notice the cin.get(); That waits for u to press enter and then it does not go back to rent a locker but rather it goes back to the floor selection menu because of your return command... aka going back to

void rent_locker();

I just noticed ur problem is when u press no and it skips asking u to choose a floor... this is because the memory is not cleared and it keeps that choice stored... stack_no still has the value stored from the previous time that u chose the locker... so u need to clear stack_no so that it allows u to choose which floor u want.. I will try n see if I can do that with all these goto's everywhere but I had that problem yesterday and solved it.... my solution was to remove goto commands as they do not deconstruct properly... rather they skip over all the code and jumps to a specified point in the program... this leaves all that stuff in the memory and doesnt clear it... at least thats what I read on the stackoverflow website last night.. i solved it by making everything a function and whenever I wanted the program to get back to the start or a specific point id just call that function

Example:

#include <iostream>
#include <windows.h>

int x;

using namespace std;

void menu();   // declares the prototype function - menu(); before it is used...
void stuff();
void rent();
void cancel();
void admin();
void tryagain();


int main()
{
   menu();
return 0;
}

void menu()
{
  cout<<"rent a locker\n";
   cout<<"cancel locker renting\n";
   cout<<"admin mode\n";
   stuff();
}
void stuff()
{
  cin>>x;
  cin.ignore();
  switch(x)
  {
    case '1': rent();
            break;
    case '2': cancel();
            break;
    case '3': admin();
            break;
    default: cout<<"That option is not in the menu, try again\n";
             menu();
             break;
   }
}
void rent()
{
  //do stuff
}
void cancel()
{
  //do stuff
}
void admin()
{
  //do stuff
}

notice I didnt say goto menu(); for the default... because if I did, it will not deconstruct stuff() properly and will give me an infinite loop kinda like what u have... the options will definitely stay there rather than being removed from the memory... I dont know exactly how else to clear the memory

Thank you so much for your explanation!!!!!!

i would change the "goto" structure into another format.....still trying

I have changed the structure by not using goto. but the problem still exists.

wait...i get wht you mean...

I hope so and I found bugs in your code aswell where if I type the confirm char before the price comes up, it skips confirming...

example:

if I press y = yes here... it skips asking
Do you want to pay 15$ for this locker?
cin>>x;
if (x == y)
{
  cout<<"paid";
}
else if(x == n)
{
  cout<<"choose a different locker";
}

it skips all of the code and goes back to the main menu...
another thing is lets say I choose a locker in the first *Column* and then press yes to confirm it, when I go to choose another locker aka the one right below it, it lets me choose whether or not to pay... while deciding if u want to pay, press the right arrow and u will see the previous choice from the first time...=> u will see the letter "y". now if I exit, restart, choose my first locker again(first COLUMN), paid.. and go to choose a second locker, *presses the right arrow*, *presses enter*, u will notice that the previous choice was already in memory and will be confirmed automatically when u press enter...

Fortunately for u... I just decided to spend 10 minutes and figure out how to fix this without doing all that clear memory bs... and the solution was:
when user presses right key during locker selection, automatically press backspace = erase last choice...
when user presses left key during locker selection, automatically press backspace = erase last choice.... and so on for every key pressed...

of course I had to change your other menu selection key that states... if user presses backspace, go back to previous menu... changed it to if user presses ESCAPE, automatically press enter and take them back to the previous menu...

this definitely solve your problem of clearing the stupid memory and u will find bugs that I have after testing your code for 10 minutes straight.. if u press n then select another locker and press y, u will see a bug that I have seen...

Here is the edited code...

if(GetAsyncKeyState(VK_LEFT) != 0){
       keybd_event(VK_BACK, 0, 0, 0); //that will fix memory issue. Allows new choice
       keybd_event(VK_BACK, 0, KEYEVENTF_KEYUP, 0);
              if(locker_remain(stack_no,y_position,x_position,2) != -1){
			     highlight(x_position*3,y_position*2+3,
			               x_position*3+3,y_position*2+5,
					       FW | 0);
                 x_position=locker_remain(stack_no,y_position,x_position,2);
			     clicked=1;
			  }
		  }else if(GetAsyncKeyState(VK_RIGHT) != 0){
                keybd_event(VK_BACK, 0, 0, 0);
                keybd_event(VK_BACK, 0, KEYEVENTF_KEYUP, 0);
              if(locker_remain(stack_no,y_position,x_position,3) != -1){
			     highlight(x_position*3,y_position*2+3,
			               x_position*3+3,y_position*2+5,
				   	       FW | 0);
                 x_position=locker_remain(stack_no,y_position,x_position,3);
			     clicked=1;
			  }
		  }else if(GetAsyncKeyState(VK_ESCAPE) != 0){
              step=1;
              keybd_event(VK_RETURN, 0, 0, 0);
              keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);

other minor fixes like void mainmenu(); added... U can obviously remove it and put it back how u want it... I was just testing it to see if it made a difference as I had that problem exactly two days ago on my own project (still incomplete due to school)... mainmenu(); does replace goto however.

Anyway the rest is really up to u.. your project.. I just liked it so continue what you were doing :).. dont forget that goto WILL NOT deconstruct stuff properly.. u can try this by compiling both of the codes below and see the difference.

For Your learning purposes: Notice the line #133 in questiontesting.cpp has rentlocker: and when your program reaches the part where u pay, press n and it will goto rentlocker and u will see that if u try to get a second locker, you will not be given a choice as the program has not decontructed.


Compare that to Untitled2.cpp where when u pay and press n, it calls the function rentlocker(); and since it decontructions perfectly, the user gets to choose a locker on the second choice...

GOOD LUCK! Great Project :)

Thank you so much. I finally solve this problem with ur solution.
hope you can also get a good academic result in ur school.

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.