Issue with Code

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 */
 
D

dcorbit

/* Maybe this is a little closer */
#include <stdio.h>
#include <stdlib.h>

int main(void) /* implicit int is icky(tm). */
{
/* variable declarations will be using floating point numbers due
to the decimal point */
char input[256];
double Salesamount;
double TaxrateDelmar;
double TaxrateEncinitas;
double TaxrateLajolla;
int iYesorno = 0;

/* variable initializations - assigning values to the variables */
Salesamount = 0;
TaxrateDelmar = .0725;
TaxrateEncinitas = .0750;
TaxrateLajolla = .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
$");
input[0] = 0;
/* scanf() with %s as a format specifier will allow hostile or
careless users to overwrite memory. */
if (fgets(input, sizeof input, stdin) == NULL)
{
puts("ERROR: Failed to collect input from standard
input.\nPlease enter a number in dollars and cents.");
goto start;
}
if (sscanf(input, "%lf", &Salesamount) != 1) {
printf("\n Sorry, '%s' is not a number. Please enter a
number:\n", input);
goto start;
} else {
printf("\nThe sales dollar amount you entered is: $%.2f\n",
Salesamount);
yeah_no_maybe:
printf("\nIs this the correct amount to figure tax on? (1 for
Yes, 2 for No)? ");
if (scanf("%d", &iYesorno) != 1) /* don't forget the good habit
you showed above... */
{
puts("Don't forget, scanf() lives on the dark side of the
force. Let's try that again:");
fflush(stdout);
goto yeah_no_maybe;
}
}

switch (iYesorno) {

case 1:
/* Performs calculations if the entered amount is correct */
/* print calculation results - provides the output to the user
*/
/* sales tax calculation equals Salesamount multiplied by
specific store location variable */
printf("\nThe sales tax for this purchase in the Del Mar store
is $%.2f", Salesamount * TaxrateDelmar);
printf("\nThe total amount of this sale in the Del Mar store is
$%.2f\n", Salesamount * TaxrateDelmar + Salesamount);
printf("\nThe sales tax for this purchase in the Encinitas
store is $%.2f", Salesamount * TaxrateEncinitas);
printf("\nThe total amount of this sale in the Encinitas store
is $%.2f\n", Salesamount * TaxrateEncinitas + Salesamount);
printf("\nThe sales tax for this purchase in the La Jolla store
is $%.2f", Salesamount * TaxrateLajolla);
printf("\nThe total amount of this sale in the La Jolla store
is $%.2f\n", Salesamount * TaxrateLajolla + Salesamount);
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;
/* not reached: */
break;

default:
printf("\nYou didn't enter a correct response, please restart
the program:\n");
exit(EXIT_FAILURE);

}
return 0;
}
 
K

Keith Thompson

/* Maybe this is a little closer */
#include <stdio.h>
#include <stdlib.h>
[snip]

Close to what than what? Please provide context when you post a followup.
 
M

Mark McIntyre

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:

This sounds like FAQ 12.17
12.17: When I read numbers from the keyboard with scanf "%d\n", it
seems to hang until I type one extra line of input.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

Was this just to include the two lines?

Keith said:
/* Maybe this is a little closer */
#include <stdio.h>
#include <stdlib.h>
[snip]

Close to what than what? Please provide context when you post a followup.
[quoted signature snipped]

No, this was to ask the previous poster to provide context when
posting a followup.

Incidentally, please don't top-post.
See <http://www.caliburn.nl/topposting.html>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top