I have to hit <Enter> twice after inputting my dollar amount and store selection
I think I am just missing something simple somewhere, but am going stupid looking for it.

Went to the internet to try and find a good debugger, but have no idea how or where to even begin that process.

any suggestions are welcome

#include <stdio.h>  // standard input output library

//defines tax value for calculations
#define DelMar 7.25
#define Encinitas 7.5
#define LaJolla 7.75

char cAgain;  //variable for Y/N questions

float user_input() // week 4 addition for sales amount
{ //prompt, collect, and store sale amount
    float fAmount; //defines amount type as float
    printf("Enter Amount of Sale $ "); 
    while ((scanf("%f", &fAmount) != 1) || (fAmount <= 0.0)) //week 5 character validation addition
		{         
		while (getchar() != '\n') {;}     // week 5 character validation    
		printf("PLEASE ENTER POSITIVE DOLLAR AMOUNT:");        
		}     
			while (getchar() != '\n') //week 5 character validation
			{;}    
			return fAmount;
}

int iMenu()  //defines menu variable
{ // menu which prompts, collects, and stores store number
    int iSelection; //defines selection type as integer
    printf("Tax Calculation for Purchase\n\n"); //changed to reflect week 4 updates
    printf("1. Del Mar \n2. Encinitas \n3. La Jolla\n");
    printf("\n\nPlease Select Number of Store for Which to Calculate: ");
    scanf("%d", &iSelection); 
    while (getchar() != '\n') // week 5 character validation
	{;}     
	while (iSelection < 1 || iSelection > 3) //integer validation
	{        
	printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3:");
	scanf("%d", &iSelection);        
		while (getchar() != '\n') //week 5 character validation
		{;}    
		}     
		return iSelection;
		} 

int main() //main loop getting shorter an shorter
{
    float fSales; // Sales value for calculations
    int iStorenum; //defines store for main loop, menu will not work without

    do // week 5 do/while loop to replace GOTO and STARTOVER 
    {				    
    fSales = user_input(); //new week 4 sales total
    iStorenum = iMenu(); //displays menu created above
    
    
    if (iStorenum == 1)
//Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
       printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, DelMar, fSales*DelMar/100, DelMar*fSales/100+fSales); 
    if (iStorenum == 2)
//Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
       printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, Encinitas, fSales*Encinitas/100, Encinitas*fSales/100+fSales); 
    if (iStorenum == 3)
 //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
       printf("\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, LaJolla, fSales*LaJolla/100, LaJolla*fSales/100+fSales); 
                
  
    printf("\n\tRun Calculator Again? (y/n) ");//prompts for repeat
            scanf("%c", &cAgain);//scans input at store number prompt   
            
       } while (cAgain == 'y' || cAgain == 'Y'); // week 5 do/while end   
            
}

Yeah, you never returned anything out of main.
After "Run Calculator Again? (y/n)" is printed, the program immediatly exits. This is becouse there is newline left in your buffer. Use getchar() to eat it up.

I do not have to press enter twice at any part of your program. It's probably caused by those weird while-getchar's. Remove all of those, and make sure a getchar(); is used after every scanf. That should solve your problem.
Good luck.

I need those for character validation. Without them, if a user puts in an A instead of 10.00 it freaks out.
I know there are better ways to do that, but right now that is simple and it works for me.

i will play with the getch(); and see what I can come up with.
Thanks

Edited 3 Years Ago by happygeek: fixed formatting

My preference is to read a line of input with fgets and then use sscanf to obtain numeric data. It's very much like what you're doing, with fewer headaches. Then maybe after you've got things going good that way take a pass at redoing it with scanf ?

Thanks Dave.
I tried playing with those a few weeks ago when I first started, but just got confused.
I have been using scanf simply because it is a shorter command and easier to understand.

It confuses me how Hiroshe and yellowSnow both run the same code as me without having to hit <Enter> extra times but I do.

Could that be a Miracle C issue as well?

Could that be a Miracle C issue as well?

Danger Will Robinson! That's not a compiler, it's an unfinished toy.

I'd recommend finding a different compiler, yes.

Comments
Absolutely!

That is what I have heard, but it is required for class.
I looked for a debugger today just to help me troubleshoot so that maybe I can quit bothering you guys with this silly stuff, but I cannot find anything that seems useful.

Hello, yes it's me again.

I cannot believe you. I gave you a full blown working version of what you're looking for and you still keep on insisting on posting versions of the code that don't work. I spent a great deal of time in your PM response (which you should never have sent to me), but you just happily say "it doesn't work" and keep posting.

I not only "fixed" the basic validation issues you were having, I re-factored your code and made it far more readable. You ignored my advice about moving the validation code out of main() and into the functions that are responsible for that data and you have totally ignored the additional function that takes a whole lotta crapola out of your main().

Based on one of the last posts to your thread:
http://www.daniweb.com/forums/thread212611.html

you again have ignored my advice to RUN MY CODE - you just keep going back to the original versions of your code that you originally had problems with and then keep posting that it doesn't work.

The previous posts in this thread do make a good point though. Turbo C is not good - but Miracle C actually makes TC look half decent (though it ain't).

yellowSnow -
Are you really aggravated that I did not take your code and post it in my program verbatim? I thought the idea out here was NOT to do that.

I am not IGNORING your advice. I simply do not understand it all, or want to try something on my own.
I do not want free code handed out to me. I never asked for it, and I told you that. I also thanked your for your time, and I appreciate the effort you gave on the problem, but you also continually ignore what I am asking and simply gave me code. I loved the code, and it was a great help, but I still want to write and turn in my own work.

I ask for help in understanding, NOT free fixes to problems that I did not write. Your code was great, but I did not write it, and I do not want credit for it.
I am working on trying to understand problems and come up with my own fixes to things. I am never going to learn anything if I just take your code and put it in a program.
I know my fixes are not as smooth as yours, but I have been coding for 4 weeks now, I do not think they would be. If I just take your code, put it in my program and move on I am never going to learn anything.

Comments
You're welcome :)

yellowSnow -
I am not IGNORING your advice. I simply do not understand it all, or want to try something on my own.
I do not want free code handed out to me. I never asked for it, and I told you that. I also thanked your for your time, and I appreciate the effort you gave on the problem, but you you also continually ignore what I am asking and simply give me code.
I ask for help in understanding, NOT free fixes to problems that I did not write. Your code was great, but I did not write it, and I do not want credit for it.
I am working on trying to understand problems and come up with my own fixes to things. I am never going to learn anything if I just take your code and put it in a program.
I know my fixes are not as smooth as yours, but I have been coding for 4 weeks now, I do not think they would be. If I just take your code, put it in my program and move on I am never going to learn anything.
I am sorry this seems to aggravate you.

It's very admirable that you want to try something on your own - unlike some others on this forum. And for the record - it does not aggravate me - it does disappoint me a little.

But please look back at your previous thread and you'll see that I gave you advice (besides others) about why goto should not be used.

After we got over that hurdle your thread predominantly was focused on why your loop was not re-entering (due to scanf picking up left over chars).

Once I thought we got over that issue, then the focus was on validation. Many good suggestions were offered (from Dave Sinkula in particular) but you explained that these solution options were beyond your capabilities at this time.

From there on, I was attempting to offer code templates to assist you in your assignment. If you go back and read my posts from your previous thread, you'll see that I have always put reasons forward why you should do things a certain way.

But then, you went back to your original code using goto. Once I saw this, I decided to give you a full code posting of what I believed would help you - and I did this because I can see that you were working hard on getting this assignment done and I was concerned that you were getting confused with all the thread traffic.

I was never expecting you to take my code (as average as it is) and submit it and take credit for it. My motive behind my code postings was to give you a working template to work from. And I did explain why I did certain things in my code - and as you well know, elaborate on why I did so if you didn't understand the first time.

Thanks again, I really do appreciate your efforts, and I am glad you are not aggravated.
I have gone to great lenghts to NOT copy your code because it is so well written (at least to me it is) and it does work. I just would not feel right with it.

It may look like I am not listening all the time, but I promise you I have studied EVERYTHING you guys have said, and tried most of it many times over. At the moment though I am just trying to stay as simple as I can and get through this class with code I can read and explain back to my instructor. Even if I do not use what you told me, it may have been a stepping stone to what I did use, as ugly as it ended up being! lol

What I have here works, and I have all the GOTOs out of it, but in my Miracle C is forces me to <ENTER> twice after I put in a number.

The code you wrote did the same thing for me, so I am assuming it is just Miracle C.

Again, thanks for your time and help, I am sorry I could not apply more of what you tried to teach me.

I need those for character validation. Without them, if a user puts in an A instead of 10.00 it freaks out.
I know there are better ways to do that, but right now that is simple and it works for me.

i will play with the getch(); and see what I can come up with.
Thanks

Those while-getchar's look like they just clear the buffer (up to a newline). You can get rid of those, and put a getchar after each scanf to keep the buffer clear at all times. Remember if you have a scanf in a loop condition, you need to have a getchar inside and outside the loop. Here's the runtime after the modification's:

Enter Amount of Sale $ a
PLEASE ENTER POSITIVE DOLLAR AMOUNT:10.55
Tax Calculation for Purchase

1. Del Mar 
2. Encinitas 
3. La Jolla


Please Select Number of Store for Which to Calculate: 1

Del Mar 	Sale $10.55	Rate 7.25%	Tax $0.76%	Total= 	$11.31	


	Run Calculator Again? (y/n) n

You can expect your computer to shoot CD's at your head while running the code with Miracle. I don't think pressing return twice will matter too much. :P

Comments
very helpful

Remember if you have a scanf in a loop condition, you need to have a getchar inside and outside the loop. Here's the runtime after the modification's:

Thanks for that tip. I did not know that was a rule.
I will play with that when I get home again tonight. I have them inside, but did not know they needed to be outside as well.
I am guessing just a simple

getchar();

for the outside is what you are referring to, to balance what I already have inside.

This question has already been answered. Start a new discussion instead.