Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation of My program

Status
Not open for further replies.

daisypolly

Programmer
Apr 28, 2005
38
CA
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: ");


}
</code>

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
 
Your scanf conversion is failing. Look at the return value of scanf:

Code:
if(scanf("%d",&c) != 1)
   printf("scanf conversion error\n");
 
thanks for the reply I fixed that problem. I have another problem how can I check that when the user enters the price it is in correct format. For example if the user enters 1.5.5.5 It should give an error.
 
Look for a '.' and make sure there is only one (See the strchr() function).
Then make sure all characters before and after the '.' are only numbers (See the isdigit() function).

Of course the easiest way would be to use the atof() function to convert from a char* to a double. If atof() returns 0.0 and the string is not (0 or 0.0) then there was an error converting to a double and therefore the string isn't a valid number.
 
I think people should look at this thread :
The OP has asked this question before - and has clearly just left it a week and tried to repost the same question again !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top