i am having trouble making a new selection from this menu of 4 items. it seems like when it asks for a selection i have to type it in twice, and when i want to quit it picks up the 'q' the second time.
i suspect the get_first() function has something to do with it. somehow the char is not picked up with the first try.
thanx in advance.
Code:
/* Oct, 02 rev38 */
#include <stdio.h>
#define PI 3.14
#define STAR "*******************************************************"
char get_choice(void);
char get_first(void);
float sfm(void);
float rpm(void);
float ipm(void);
float fpt(void);
int main(void)
{
int choice;
while ((choice = get_choice()) != 'q')
{
switch (choice)
{
case '1' : sfm();
break;
case '2' : rpm();
break;
case '3' : ipm();
break;
case '4' : fpt();
break;
default : printf("Program error!\n");
break;
}
}
printf("Bye.\n");
return 0;
}
char get_choice(void) // menu function
{
int ch;
printf("%s\n",STAR);
printf("1: SFM having cutter Dia and RPM\n");
printf("2: RPM having SFM and cutter Dia\n");
printf("3: IPM having Feed per Tooth, Number of Teeth and RPM\n");
printf("4: FPT having IPM Number of Teeth and RPM\n");
printf("%s\n",STAR);
printf(" Q to quit\n");
printf("ENTER CHOICE: ");
ch = get_first();
while ((ch < '1' || ch > '4') && ch != 'q')
{
printf("choose from the above numbers or Q to quit.");
ch = get_first();
}
return ch;
}
char get_first()
{
int ch;
ch = getchar();
while (getchar() != '\n' && ch != 'q')
continue;
return ch;
}
float sfm(void)
{
float doc_; // depth of cut
float rpm_; // rpm
float sfm_; // surface feet per minute
printf("Enter Diameter of Cutter ");
scanf("%f", &doc_);
printf("Enter RPM: ");
scanf("%f", &rpm_);
sfm_ = (PI * doc_ * rpm_) / 12;
printf("\n SFM is equal to %.2f\n\n\n",sfm_);
return 0;
}
float rpm(void)
{
float sfm_; // surface feet per minute
float cd_; // cutter diameter
float rpm_; // rpm
printf("Enter SFM: ");
scanf("%f", &sfm_);
printf("Enter Cutter Diameter: ");
scanf("%f", &cd_);
rpm_ = (sfm_ * 12) / (PI * cd_);
printf("\n RPM is equal to %.2f\n\n\n",rpm_);
return 0;
}
float ipm(void)
{
float fpt_; // feed per tooth
float not_; // number of teeth
float rpm_; // rpm
float ipm_; // inches per minute
printf("Enter Feed per Tooth: ");
scanf("%f", &fpt_);
printf("Enter Number of Teeth: ");
scanf("%f", ¬_);
printf("Enter RPM: ");
scanf("%f", &rpm_);
ipm_ = (fpt_ * not_ * rpm_);
printf("\n IPM is equal to %.2f\n\n\n",ipm_);
return 0;
}
float fpt(void)
{
float ipm_; // inches per minute
float not_; // number of teeth
float rpm_; // rpm
float fpt_; // feed per tooth
printf("Enter ipm: ");
scanf("%f", &ipm_);
printf("Enter Number of Teeth: ");
scanf("%f", ¬_);
printf("Enter RPM: ");
scanf("%f", &rpm_);
fpt_ = (ipm_) / (not_ * rpm_);
printf("\n FPT is equal to %.2f\n\n\n",fpt_);
return 0;
}
[code][/code]tagged by Salem