Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,224 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,025 people online right now. Registration is fast and FREE... Join Now!




Keyboard.. help

 
Reply to this topicStart new topic

Keyboard.. help

dreambaff0
post 12 Mar, 2008 - 05:59 AM
Post #1


New D.I.C Head

*
Joined: 24 Feb, 2008
Posts: 22

Hello everyone

I wanted to know if is possible to give a fixed value to teh esc and enter button in order to quit or repeat teh program below...

I have a rough idea to assign teh esc button the "quit" capability but I don't know how to do so with the enter button.

CODE

/*This program will calculate the volume used by a box that contains a number of tins.
  The program will also calculate the number of tins in the box and the volume wasted. */

/*width_box=Width of the box in use
  height_box=Height of the box in use
  lenght_box=Lenght of the box in use

  width=The number of tins fitted along the width of the box
  lenght=The number of tins fitted along the lenght of the box

  height_tin=Height of the tin in use
  diameter_tin=Diameter of the tin in use(twice the radius)
  pi=is a costant used to calculate the volume of a cylinder and is equal to 3,141592
   , which was rounded to 3.142

  volume_used=Total volume of the tins in the box
  volume_box=Volume of the box in use
  volume_wasted=Volume wasted(Volume of the box-Volume Used)

  layers=Number of layers of tins
  tins=Total number of the tins*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define pi 3.142

void main(void)
{
  float width_box,height_box,lenght_box;
  float height_tin,diameter_tin;
  float volume_used,volume_box,volume_wasted;
  float tins,layers;
  float width,lenght;
  char buff [20];

  /*The user inserts details related to the width of
  the box and the diameter of the tin*/

  printf("Type in width details.\n");
  do
  {

  printf("\nType in the diameter of the tin:(cm)\n" ,diameter_tin);
  diameter_tin=atof(gets(buff));

       if (diameter_tin <= 0.0)
       {
       printf("\nPlease, type in an appropriate value for this figure.\n");
       }

   }while(diameter_tin<=0.0);

  do
  {

  printf("Type in the width of the box:(cm)\n" ,width_box);
  width_box=atof(gets(buff));

       if (width_box <= 0.0)
       {
       printf("\n\nPlease, type in an appropriate value for this figure.\n");
       }

   }while(width_box<=0.0);

  printf("___________________________________________________________________\n");

  /*The user inserts details related to the lenght of the box*/

  printf("\nType in leght details.\n");
  do
  {
  printf("\nType in the lenght of the box:(cm)\n",lenght_box);
  lenght_box=atof(gets(buff));

       if (lenght_box <= 0.0)
       {
       printf("\nPlease, type in an appropriate value for this figure.\n");
       }
    }while(lenght_box<=0.0);

  printf("___________________________________________________________________\n");


  /*The user inserts details related to the height of the box
  and the height of the tin*/

  printf("\nType in height details.\n");
  do
  {
  printf("\nType in the height of the tin:(cm)\n",height_tin);
  height_tin=atof(gets(buff));

       if (height_tin <= 0.0)
       {
       printf("\nPlease, type in an appropriate value for this figure.\n");
       }
    }while(height_tin<=0.0);

  do
  {
  printf("Type in the height of the box:(cm)\n",height_box);
  height_box=atof(gets(buff));

       if (height_box <= 0.0)
       {
       printf("\nPlease, type in an appropriate value for this figure.\n");
       }
    }while(height_box<=0.0);

  printf("___________________________________________________________________\n");

  /*The number of layers is defined here*/
  layers=height_box/height_tin;
  layers=(int) layers;/*Note the use of cast*/

  /*The values lenght and width are defined here in order to gain the correct value for tins*/
  lenght=lenght_box/diameter_tin;
  lenght=(int)(lenght);
  width=width_box/diameter_tin;
  width=(int)(width_box/diameter_tin);

  /*tins is defined here*/
  tins=lenght*width*layers;
  tins=(int) tins;
  printf("\nThe total number of tins in the box is:%8.0f \n",tins);/*No matter how many decimal
   places are positioned on the float value,this still appears as a whole number.*/

  /*volume_used is defined*/
  volume_used=(diameter_tin/2)* (diameter_tin/2) * height_tin * pi;
  printf("\nThe volume used by the tin is: %8.2f",volume_used);
  printf("cm3\n");
  /*volume_box is defined here*/
  volume_box=height_box*lenght_box*width_box;
  printf("\nThe volume used by the box is: %8.2f",volume_box);
  printf("cm3\n");
  /*volume_wasted is defined here*/
  do
  {

  volume_wasted=volume_box-volume_used;
  printf("\nThe volume wasted is: %8.2f",volume_wasted);
  printf("cm3\n");

   if (volume_wasted <=0.0)
   {
   printf("There isn't any volume wasted.");
   }
  }while(volume_wasted <=0.0);

  printf("\n_______________________________________________________\n");

/*The inputs inserted from the user are displayed.*/
printf("The following details are the values you have inserted.");
printf("\nWidth of Box: %8.2f",width_box);
printf("cm");
printf("\nHeight of Box: %8.2f",height_box);
printf("cm");
printf("\nLenght of Box: %8.2f ",lenght_box);
printf("cm\n");

printf("\nDiameter of Tin: %8.2f",diameter_tin);
printf("cm");
printf("\nHeight of tin: %8.2f",height_tin);
printf("cm");

printf("\nPress any key to quit....");
getch();
}
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 12 Mar, 2008 - 06:26 AM
Post #2


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


The esc character is 27 and the enter key is 13.

Here is a simple little utility that can be very helpful:
CODE
#include <stdio.h>

int main() {
    char in;
    while ((in = getch()) != 13) {
          printf("%c %x  = %d \n", in, in, in);
    }
}


it will print out the characters generated when you press various keys. You will note that some key (like the F keys) produce more than one character. if you press CTRL + A you will get char 1, and CTRL + B is char 2 etc.

Now to really get the most out of the keyboard you want to read keyboard scan codes (which will allow you to tell which physical key has been pressed -- for example did the user hit the left or right shift key etc.)
User is offlineProfile CardPM

Go to the top of the page

jeronimo0d0a
post 12 Mar, 2008 - 11:38 AM
Post #3


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


Absolutely correct but getch() is in conio.h on my compiler.
User is offlineProfile CardPM

Go to the top of the page

dreambaff0
post 13 Mar, 2008 - 02:06 AM
Post #4


New D.I.C Head

*
Joined: 24 Feb, 2008
Posts: 22

QUOTE(dreambaff0 @ 12 Mar, 2008 - 06:59 AM) *

Hello everyone

I wanted to know if is possible to give a fixed value to teh esc and enter button in order to quit or repeat teh program below...

I have a rough idea to assign teh esc button the "quit" capability but I don't know how to do so with the enter button.

CODE

/*This program will calculate the volume used by a box that contains a number of tins.
  The program will also calculate the number of tins in the box and the volume wasted. */

/*width_box=Width of the box in use
  height_box=Height of the box in use
  lenght_box=Lenght of the box in use

  width=The number of tins fitted along the width of the box
  lenght=The number of tins fitted along the lenght of the box

  height_tin=Height of the tin in use
  diameter_tin=Diameter of the tin in use(twice the radius)
  pi=is a costant used to calculate the volume of a cylinder and is equal to 3,141592
   , which was rounded to 3.142

  volume_used=Total volume of the tins in the box
  volume_box=Volume of the box in use
  volume_wasted=Volume wasted(Volume of the box-Volume Used)

  layers=Number of layers of tins
  tins=Total number of the tins*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define pi 3.142

void main(void)
{
  float width_box,height_box,lenght_box;
  float height_tin,diameter_tin;
  float volume_used,volume_box,volume_wasted;
  float tins,layers;
  float width,lenght;
  char buff [20];

  /*The user inserts details related to the width of
  the box and the diameter of the tin*/

  printf("Type in width details.\n");
  do
  {

  printf("\nType in the diameter of the tin:(cm)\n" ,diameter_tin);
  diameter_tin=atof(gets(buff));

       if (diameter_tin <= 0.0)
       {
       printf("\nPlease, type in an appropriate value for this figure.\n");
       }

   }while(diameter_tin<=0.0);

  do
  {

  printf("Type in the width of the box:(cm)\n" ,width_box);
  width_box=atof(gets(buff));

       if (width_box <= 0.0)
       {
       printf("\n\nPlease, type in an appropriate value for this figure.\n");
       }

   }while(width_box<=0.0);

  printf("___________________________________________________________________\n");

  /*The user inserts details related to the lenght of the box*/

  printf("\nType in leght details.\n");
  do
  {
  printf("\nType in the lenght of the box:(cm)\n",lenght_box);
  lenght_box=atof(gets(buff));

       if (lenght_box <= 0.0)
       {
       printf("\nPlease, type in an appropriate value for this figure.\n");
       }
    }while(lenght_box<=0.0);

  printf("___________________________________________________________________\n");


  /*The user inserts details related to the height of the box
  and the height of the tin*/

  printf("\nType in height details.\n");
  do
  {
  printf("\nType in the height of the tin:(cm)\n",height_tin);
  height_tin=atof(gets(buff));

       if (height_tin <= 0.0)
       {
       printf("\nPlease, type in an appropriate value for this figure.\n");
       }
    }while(height_tin<=0.0);

  do
  {
  printf("Type in the height of the box:(cm)\n",height_box);
  height_box=atof(gets(buff));

       if (height_box <= 0.0)
       {
       printf("\nPlease, type in an appropriate value for this figure.\n");
       }
    }while(height_box<=0.0);

  printf("___________________________________________________________________\n");

  /*The number of layers is defined here*/
  layers=height_box/height_tin;
  layers=(int) layers;/*Note the use of cast*/

  /*The values lenght and width are defined here in order to gain the correct value for tins*/
  lenght=lenght_box/diameter_tin;
  lenght=(int)(lenght);
  width=width_box/diameter_tin;
  width=(int)(width_box/diameter_tin);

  /*tins is defined here*/
  tins=lenght*width*layers;
  tins=(int) tins;
  printf("\nThe total number of tins in the box is:%8.0f \n",tins);/*No matter how many decimal
   places are positioned on the float value,this still appears as a whole number.*/

  /*volume_used is defined*/
  volume_used=(diameter_tin/2)* (diameter_tin/2) * height_tin * pi;
  printf("\nThe volume used by the tin is: %8.2f",volume_used);
  printf("cm3\n");
  /*volume_box is defined here*/
  volume_box=height_box*lenght_box*width_box;
  printf("\nThe volume used by the box is: %8.2f",volume_box);
  printf("cm3\n");
  /*volume_wasted is defined here*/
  do
  {

  volume_wasted=volume_box-volume_used;
  printf("\nThe volume wasted is: %8.2f",volume_wasted);
  printf("cm3\n");

   if (volume_wasted <=0.0)
   {
   printf("There isn't any volume wasted.");
   }
  }while(volume_wasted <=0.0);

  printf("\n_______________________________________________________\n");

/*The inputs inserted from the user are displayed.*/
printf("The following details are the values you have inserted.");
printf("\nWidth of Box: %8.2f",width_box);
printf("cm");
printf("\nHeight of Box: %8.2f",height_box);
printf("cm");
printf("\nLenght of Box: %8.2f ",lenght_box);
printf("cm\n");

printf("\nDiameter of Tin: %8.2f",diameter_tin);
printf("cm");
printf("\nHeight of tin: %8.2f",height_tin);
printf("cm");

printf("\nPress any key to quit....");
getch();
}

Thanks, but how do i insert it in my program?

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 08:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month