Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,664 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,165 people online right now. Registration is fast and FREE... Join Now!




Loop counting every entry

 
Reply to this topicStart new topic

Loop counting every entry

cypher--x
post 20 Aug, 2008 - 09:27 AM
Post #1


New D.I.C Head

*
Joined: 17 Aug, 2008
Posts: 5

As always I am new to programming so please help!

In my code there are two loops used to validate y/n choices. if you run the program it work fine but error checking does not.

Here is the issue : (I have noted the lines in the attached code, If you compile and run you will see what happens)

When I enter anything but Y||N, let's say the letter P and press [enter], it counts the P and the Return and errors twice. If I enter 12345 it error 6 different times. How can I get it to count it all as one entry?

The same loop is used later at the end and if you actually select Y and press enter in the first loop, it carries the [enter] to the loop at the end of the program and errors one time.

It seems that it is counting each alpha/numeric and the return as diffrent entries. Is there some way to have it count as one entry no matter how many alpha/numeric are entered.

I marked the issue areas with //ISSUE

i also attached the *.c file if that is eaiser.

Any help would be great!!

Thx - Eric

CODE

#include <stdio.h>
#include <ctype.h>

main(void)
{
    int valid_input = 0;
    char cYesNo='\0';
    char cRestart='\0';
    float x = 1.0725f;
    float y = 1.075f;
    float z = 1.0775f;
    float fResponse;
    fResponse= 0.00f;
        
    printf("\n*** Kudler Fine Foods - Sales Tax Calculator ***\n"); //Display program title
    
    start:    
        printf("\nEnter purchase amount: "); //Prompt user to enter purchase amount//
        scanf("%f", &fResponse);
        
        if (fResponse <= 0.00){
            printf("\nYou did not enter a number or your entry is less then 0\n");
            goto start;
        }    

    confirm:
        printf("\nAre you sure you want sales tax calculated on: $%.2f\n"); //Verify user input//
    
        valid_input = 0;            //ISSUE -> Loop #1 error happens here
        while( valid_input == 0 ) {
            printf("\nContinue (Y/N)?");
            scanf("%c", &cYesNo);
            cYesNo = toupper(cYesNo);
            if((cYesNo == 'Y') || (cYesNo == 'N') )  valid_input = 1;
            else  printf("\nError: Invalid choice\n");
        }

            switch (cYesNo){
    
                case 'y': case 'Y':
                     printf("\n\nSales Tax and Total by store:");
                    printf("\n\n\n\tDel Mar Sales Tax=$%.2f\n", (fResponse*x)-fResponse);
                    printf("\n\tDel Mar Total=$%.2f\n", (fResponse*x));
                    printf("\n\n\tEncinitas Sales Tax=$%.2f\n", (fResponse*y)-fResponse);
                    printf("\n\tEncinitas=$%.2f\n", (fResponse*y));
                    printf("\n\n\tLa Jolla Sales Tax=$%.2f\n", (fResponse*z)-fResponse);
                    printf("\n\tLa Jolla=$%.2f\n", (fResponse*z));
                    break;
                case 'n': case 'N':
                    goto start;
            } //end switch
    restart:            
        printf("\nWould you like to enter another amount?\n");
    
            valid_input = 0;        //ISSUE -> Loop #2 error happens here [return] from loop #1 carries to here
            while( valid_input == 0 ) {
                printf("\nContinue (Y/N)?");
                scanf("%c", &cRestart);
                cRestart = toupper(cRestart);
                if((cRestart == 'Y') || (cRestart == 'N') )  valid_input = 1;
                else  printf("\nError: Invalid choice\n");
            }

            if (cRestart=='y'||cRestart=='Y'){
                goto start;
            }
            if (cRestart=='n'||cRestart=='N'){
            printf("\nThank You\n");
            }            
        getch(); //Hold window to view results//    
}//end main

User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 20 Aug, 2008 - 09:35 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,441



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


It's to do with your input stream. It stores some temporary crap in a buffer, so sometimes it will spoof the program into thinking you entered something to answer the next question too.

After each instance of scanf(); try adding fflush(stdin); to clear that unwanted buffer.

From what I understand both of your problems to be, that should fix both of them.

Hope this helps smile.gif
User is online!Profile CardPM

Go to the top of the page

cypher--x
post 20 Aug, 2008 - 01:17 PM
Post #3


New D.I.C Head

*
Joined: 17 Aug, 2008
Posts: 5

QUOTE(gabehabe @ 20 Aug, 2008 - 10:35 AM) *

It's to do with your input stream. It stores some temporary crap in a buffer, so sometimes it will spoof the program into thinking you entered something to answer the next question too.

After each instance of scanf(); try adding fflush(stdin); to clear that unwanted buffer.

From what I understand both of your problems to be, that should fix both of them.

Hope this helps smile.gif


Thank you this worked!!!!! I have been trying to fix this for 2 weeks now.
User is offlineProfile CardPM

Go to the top of the page

cypher--x
post 20 Aug, 2008 - 01:29 PM
Post #4


New D.I.C Head

*
Joined: 17 Aug, 2008
Posts: 5

gabehabe -
Thank you again
I just discoverd that your fix for my loop problem also fixed my problem with my if statement to verify the purchase amount entered was greater then or equal to 0.00 . Before if you entered a letter it created an endless loop.

THX!!!
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 20 Aug, 2008 - 01:35 PM
Post #5


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,441



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Glad I could help smile.gif

You know, there is a "thanks" feature wink2.gif
User is online!Profile CardPM

Go to the top of the page

cypher--x
post 21 Aug, 2008 - 09:32 AM
Post #6


New D.I.C Head

*
Joined: 17 Aug, 2008
Posts: 5

QUOTE(gabehabe @ 20 Aug, 2008 - 02:35 PM) *

Glad I could help smile.gif

You know, there is a "thanks" feature wink2.gif


no I didn't but i do now! DONE!!
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 21 Aug, 2008 - 05:19 PM
Post #7


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


just a note: fflush(stdin); is non-standard and its behavior is not defined within the standard. Microsoft has added it as an extension but it is NOT platform independent. fflush is for OUTPUT streams. its behavior to "clear the input buffer" is illogical. It's purpose is to get the stream to write its buffer to the "sink". So regardless Microsoft's advocacy of irrational programming, you really should avoid using it.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:45AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month