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!

A simple program in C

Status
Not open for further replies.

daisypolly

Programmer
Apr 28, 2005
38
CA
Hi,

I am making a cash register in C. I want to have an add ,remove functionality.I am doing that I get an error.
The user will press 1 and they will be able to add the item name and the item price.
my code is:

<code>
#include <stdio.h>
int itemPrice = 0;
char itemName ;

int main(int argc, char *argv[]){

int n = 0;
int number=0;
int price1=0;
char item1;



while ( number <= 6 ) {
printf("1. 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: ");
scanf("%d",&number);

if(number==1){

scanf("%d,%c",price1,item1);
add(price1,item1);

//printf("basket is %d",price1,item1);
}

if(number==3){
printf("you pressed3\n");
}
if(number==4){
printf("you pressed4\n");
}
if(number==5){
printf("you pressed5\n");
}
if(number==6){
break;
}
if(number>6){
printf("Invalid Option\n");
}
break;
}
}

void add(int aPrice,char aName){
itemPrice = aPrice;
itemName = aName;
}
</code>

Thanks for anyone who can help.
Daisy
 
You need to put the prototype of the 'add' function before the main() function.

Eg :

Code:
void add(int aPrice,char aName);

int main ....

You also need to pass pointers into scanf like this :

Code:
scanf("%d,%c",&price1,&item1);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
thanks for the reply. I did what you told me the program is now compiling but is in doing what I want to do.some how there is a finite while loop when I enter a string as an input also when the user adds the item the item is not being added correctly.

<code>
#include <stdio.h>
int itemPrice = 0;
char itemName ;
void add(int itemPrice,char itemName);
int main(int argc, char *argv[]){

int n = 0;
int number=0;
int price1=0;
char item1;



while ( number <= 6 ) {
printf("1. 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: ");
scanf("%d",&number);

if(number==1){

scanf("%d,%c",&itemPrice,&itemName);
add(itemPrice,itemName);

printf("basket is %d\n",itemPrice,itemName);
}

if(number==3){
printf("you pressed3\n");
}
if(number==4){
printf("you pressed4\n");
}
if(number==5){
printf("you pressed5\n");
}
if(number==6){
break;
}
if(number>6){
printf("Invalid Option\n");
}

}
}

void add(int aPrice,char aName){
itemPrice = aPrice;
itemName = aName;
}
</code>

Thanks for the help.
 
there is a finite while loop when I enter a string as an input

Thats because you are scanf'ing one int and one char - not a string.
To add strings, you will need to change your programme to handle char pointers, not just chars.

Did you want us to help get your current programme running, or to write the whole thing for you ? How will you learn if the second ?


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

Part and Inventory Search

Sponsor

Back
Top