Thread: Validation of my Program

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

    Validation of my Program

    Hi,
    I am making a small program of cash register. I am having trouble doing the validation. If someone could help me It would me a great help.

    My Program is:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char itemName ;
    float itemPrice = 0;
    float total = 0;
    int numberOfItems = 0;
    int number = 0;
    char scanItem ;
    float scanPrice = 0;
    float showadded();
    void add(float aPrice, char aName );
    void error_line();
    void options();
    float getTotal();
    
    float calaverage();
    
    int main(void){
    
    while (1) {
        options();
        scanf("%d",&number);
    
        if(number==1 && number<6){
    	printf ("\nEnter Price: ");
    	scanf("%f",&scanPrice);
        	if(scanPrice>1){
    			printf ("Enter item name: ");
    			scanf ("%s", &scanItem);
    			add(scanPrice,scanItem);
    			printf("%4.2f%s%s",scanPrice," ", &scanItem);
        	}
            else{ printf("Illegal entry: please reenter number");}
    	}
    
        else if (number == 2){
    	printf ("Total is %4.2f\n\n", getTotal());}
    
        else if (number == 3){
    	 printf ("The average is %4.2f\n\n", calaverage ());}
    
        else if (number == 4){
    
        }
    
        else if (number == 5){
          scanPrice=0;
          numberOfItems=0;
          total=0;
        }
        else if (number == 6){
    	break;
        }
    
       else if (!((number==1) && (number==2) && (number==3) && (number==4)&& (number==5) && (number==6))){
    	 printf("Invalid Option\n");
       }
    
    
    
    }
    
    
    }
    
    
    
    void add(float aPrice,char aName ){
        itemPrice = aPrice;
    
        total=total+aPrice;
    
        numberOfItems++;
    }
    
    float getTotal(){
        return total;
    }
    
    float calaverage(){
        return total/numberOfItems;
    }
    
    
    
    void options(){
        printf("\n1. Add Item\n");
        printf("2. Compute Total\n");
        printf("3. Compute Average\n");
        printf("4. Compute Change\n");
        printf("5. Reset Cash-register\n");
        printf("6. Quit\n");
        printf("Enter Option: ");
    
    
    }

    1. When I enter 7 as an option my program exits but when I enter 6a it goes in endless while loop.

    2.When the user enters the price the price can only be positive integers or positive real so when I enter -98 I get kicked out but when I enter 10.5.4 or 10a the program accepts the input.

    If someone could help me I would be greatful.

    Thanks
    Daisy

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    21
    I posted in other fourms so I could get diffrent kind of opinions for my code and diffrent suggestions.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    you're still ignoring some of the important things i mentioned
    here:

    http://cboard.cprogramming.com/showthread.php?t=75236

    your program simply does not need that many functions, i mean
    a function that does nothing but return a global variable is simply
    ridiculous. Who/what are you learning to program from because
    they/it are doing a horrendous job for you. You need to get out
    of the habit of using globals like that, it's just a plain bad habit
    to start off with.

    also, change this line

    Code:
    else if (!((number==1) && (number==2) && (number==3) && (number==4)&& (number==5) && (number==6)))
    to this:

    Code:
    else
    that works because of all of your else ifs.

    also change

    Code:
    if(number==1 && number<6){
    to this
    Code:
    if (number == 1)
    in respsonse to your question, try this link

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    but in my opinion, input validation is the least of your problems.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM