when the user enter 'no', only the last septh estimate should get recorded in the memory.
how can i do that?
insert
Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
bool yesNo(const char prompt[]);
double depth(double seconds2Bottom);
double toBottom;
char location[80], filename[80];
int i;
FILE *fileptr;
int main(void)
{
char choice='j';
while (choice !=('q'||'Q'))
{
printf("\t\t\tMain Menu\n\n");
printf("(D)epth: Enter pit location, Compute depth \n");
printf("(S)ave : Write pits data to file \n");
printf("(L)oad : Read mine shaft information from file.\n");
printf("(P)rint: Display mine shaft information.\n");
printf("(Q)uit : Exit program.\n");
printf("Enter choice(D/S/L/P/Q)\n\n");
scanf("%c",&choice);
fflush(stdin);
switch (choice)
{
case 'q': case 'Q':
printf("GOOD BYE\n\n\n");
return 'j';
break;
case 'd': case 'D':
do
{
printf("\n Enter mine shaft location or name:\n");
fgets(location, 80, stdin);
printf(("%s", location);
printf("\nEnter time in seconds to bottom: \n");
scanf("%i ", &toBottom);
fflush(stdin);
printf("\nShaft depth is %.2f m\n", depth(toBottom));
} while( yesNo("Do you want to take another measure"));
break;
case 's': case 'S':
printf("\n Enter name of file to enter pit data to:\n");
gets (filename);
if((fileptr=fopen(filename, "a"))==NULL)
{
printf("Error: cannot open input file for writing\n");
exit(0);
}
else
break;
case 'l': case 'L':
printf("Modify selected\n");
break;
case 'p': case 'P':
printf("Backup selected\n");
break;
default:
printf("Invalid selection. Please try again.\n");
} /*end of switch*/
} /*end of while*/
/* Pause, then exit */
printf("Press [Enter] to exit. ");
getchar();
return 0;
}
bool yesNo(const char prompt[])
{
char answer;
printf("\n%s (Y/N)? ", prompt);
answer = getchar();
fflush(stdin);
return (answer == 'y' || answer == 'Y');
}
double depth(double seconds2Bottom)
{
/* Acceleration of gravity */
const double G = 9.81;
return -0.5 * G * seconds2Bottom * seconds2Bottom;
}