newbie ?

This is a discussion on newbie ? within the C Programming forums, part of the General Programming Boards category; What would be the best way to stop this from looping ? when an Invalid character such as a letter ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    newbie ?

    What would be the best way to stop this from looping ? when an Invalid character such as a letter is input for the sale amount ?

    Code:
    #include <stdio.h>
       float fAmount;
        main()
         {   
          int iSelection = 0; 
          float fAmount = 0.0;//Purchased amount in US$
          float fTax1 = .0725;//Del Mar CA.
          float fTax2 = .0750;//Encinitas CA.
          float fTax3 = .0775;//La Jolla CA.
     
          printf("\n\tSelect the store location\n");
          printf("1\tDel Mar\n");
          printf("2\tEncinitas\n");
          printf("3\tLa Jolla\n");
          scanf("%d", &iSelection);
      
        // This makes the program run in a loop if an incorrect value is input for Purchased amount.
         do
         {
          printf("\nEnter Amount in US $\n");
          scanf("%f", &fAmount);
         }while((fAmount - floor(fAmount)) == 0.0); 
        
      
       //This calculates the tax rate for Del Mar CA. 
          if (iSelection == 1) {
          printf("\nYour tax amount is: $%.2f and the total price is $%.2f\n", (fAmount * fTax1), (fAmount + (fAmount * fTax1)));
          printf("\nClick the X in the upper right corner to exit this application.\n");
         } //end if
              
       //This calculates the tax rate for Encinitas CA. 
          if (iSelection == 2) {
          printf("\nYour tax amount is: $%.2f and the total price is $%.2f\n", (fAmount * fTax1), (fAmount + (fAmount * fTax1))); 
          printf("\nClick the X in the upper right corner to exit this application.\n");
         } //end if
     
       //This calculates the tax rate for La Jolla CA. 
          if (iSelection == 3) {
          printf("\nYour tax amount is: $%.2f and the total price is $%.2f\n", (fAmount * fTax1), (fAmount + (fAmount * fTax1)));
          printf("\nClick the X in the upper right corner to exit this application.\n");
         } //end if
       
     
      getch();
          return 0;
        
         }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Sent from my iPadŽ

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Don't cross-post: I need some help !
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21