daisypolly
Programmer
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
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