G
golfer1212
I wrote this program and it seems to work but I have to hit the enter
key twice before the else statement will execute. I have no idea what
the problem is? Here is the code:
#include <stdio.h> /* Header File */
#include <string.h>
main()
/* Start Program Code */
{
/* variable declarations will be using floating point numbers due to
the decimal point */
char input[256];
float fSalesamount;
float fTaxrateDelmar;
float fTaxrateEncinitas;
float fTaxrateLajolla;
int iYesorno = 0;
int itest = 0;
/* variable initializations - assigning values to the variables */
fSalesamount = 0;
fTaxrateDelmar = .0725;
fTaxrateEncinitas = .0750;
fTaxrateLajolla = .0775;
/* ask user to input the total sales amount for sales tax calculation
*/
start: /* define section to use goto statement if amount is incorrect
*/
printf("\nPlease enter the total dollar amount for this purchase $");
/* asks for user input */
//init input string
strcpy(input, "");
scanf("%s", input); //captures input
if (sscanf(input, "%f", &fSalesamount)!=1){
printf("\n Sorry, '%s' is not a number. Please enter a number:\n",
input);
goto start;
} //end if statement
else {// validate user entry
printf("\nThe sales dollar amount you entered is: $%.2f\n",
fSalesamount); /* user validation */
printf("\nIs this the correct amount to figure tax on? (1 for Yes, 2
for No)? "); /* Case Switch */
scanf("%d", &iYesorno); /* captures switch input */
} //end else statement
switch (iYesorno) { //start switch function
case 1:
/* Performs calculations if the entered amount is correct */
/* print calculation results - provides the output to the user */
/* sales tax calculation equals fSalesamount multiplied by specific
store location variable */
printf("\nThe sales tax for this purchase in the Del Mar store is
$%.2f", fSalesamount * fTaxrateDelmar); /* %.2f is to display two
decimal points */
printf("\nThe total amount of this sale in the Del Mar store is
$%.2f\n", fSalesamount * fTaxrateDelmar + fSalesamount);
printf("\nThe sales tax for this purchase in the Encinitas store is
$%.2f", fSalesamount * fTaxrateEncinitas);
printf("\nThe total amount of this sale in the Encinitas store is
$%.2f\n", fSalesamount * fTaxrateEncinitas + fSalesamount);
printf("\nThe sales tax for this purchase in the La Jolla store is
$%.2f", fSalesamount * fTaxrateLajolla);
printf("\nThe total amount of this sale in the La Jolla store is
$%.2f\n", fSalesamount * fTaxrateLajolla + fSalesamount);
break;
case 2:
/* Directs user back to start section to enter correct amount */
printf("\nYou will now be asked to enter the correct sales amount
again:\n");
goto start; //send user back to start
break;
default:
printf("\nYou didn't enter a correct response, please restart the
program:\n");
exit();
} //end switch
exit();
} /* End Program Code */
key twice before the else statement will execute. I have no idea what
the problem is? Here is the code:
#include <stdio.h> /* Header File */
#include <string.h>
main()
/* Start Program Code */
{
/* variable declarations will be using floating point numbers due to
the decimal point */
char input[256];
float fSalesamount;
float fTaxrateDelmar;
float fTaxrateEncinitas;
float fTaxrateLajolla;
int iYesorno = 0;
int itest = 0;
/* variable initializations - assigning values to the variables */
fSalesamount = 0;
fTaxrateDelmar = .0725;
fTaxrateEncinitas = .0750;
fTaxrateLajolla = .0775;
/* ask user to input the total sales amount for sales tax calculation
*/
start: /* define section to use goto statement if amount is incorrect
*/
printf("\nPlease enter the total dollar amount for this purchase $");
/* asks for user input */
//init input string
strcpy(input, "");
scanf("%s", input); //captures input
if (sscanf(input, "%f", &fSalesamount)!=1){
printf("\n Sorry, '%s' is not a number. Please enter a number:\n",
input);
goto start;
} //end if statement
else {// validate user entry
printf("\nThe sales dollar amount you entered is: $%.2f\n",
fSalesamount); /* user validation */
printf("\nIs this the correct amount to figure tax on? (1 for Yes, 2
for No)? "); /* Case Switch */
scanf("%d", &iYesorno); /* captures switch input */
} //end else statement
switch (iYesorno) { //start switch function
case 1:
/* Performs calculations if the entered amount is correct */
/* print calculation results - provides the output to the user */
/* sales tax calculation equals fSalesamount multiplied by specific
store location variable */
printf("\nThe sales tax for this purchase in the Del Mar store is
$%.2f", fSalesamount * fTaxrateDelmar); /* %.2f is to display two
decimal points */
printf("\nThe total amount of this sale in the Del Mar store is
$%.2f\n", fSalesamount * fTaxrateDelmar + fSalesamount);
printf("\nThe sales tax for this purchase in the Encinitas store is
$%.2f", fSalesamount * fTaxrateEncinitas);
printf("\nThe total amount of this sale in the Encinitas store is
$%.2f\n", fSalesamount * fTaxrateEncinitas + fSalesamount);
printf("\nThe sales tax for this purchase in the La Jolla store is
$%.2f", fSalesamount * fTaxrateLajolla);
printf("\nThe total amount of this sale in the La Jolla store is
$%.2f\n", fSalesamount * fTaxrateLajolla + fSalesamount);
break;
case 2:
/* Directs user back to start section to enter correct amount */
printf("\nYou will now be asked to enter the correct sales amount
again:\n");
goto start; //send user back to start
break;
default:
printf("\nYou didn't enter a correct response, please restart the
program:\n");
exit();
} //end switch
exit();
} /* End Program Code */