Jump to content
  • Advertisement
Sign in to follow this  
natec_k

i need help with a c program

This topic is 2752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok so i'm sposed to write a program mimicking an inventory system, it is spsoed to runa a calculation to determine the amount of goods on hand. one of the variable involved is in a structure, but based on how i've done it, it gives me an error saying it cannot convert "int *" to "int". can someone help me please?



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct parts{
char* Pname[3];
int Pnum;
int quantity;
int price;
int re_orderp;
};

int quancalc(int[]);

int main(){
struct parts arr[3];
int i, a,b,c;


printf("\t ***************************************************\n");
printf("\t Benz Auto Supplies Inventory System \n");
printf("\t ***************************************************\n\n");
printf(" Welcome, please read and follow all instructions carefully! \n");
printf("\n Please select one of the following options: \n");
while(a <= 3 || a > 3){
printf("\n Press 1 to enter item information");
printf("\n Press 2 to view supply info");
printf("\n Press 3 to exit the system \n");
printf("\n Selection: ");
scanf("%d",&a);

if(a == 1){
for(i = 0; i<= 1;i++){

printf("\n Please enter the item name: ");
scanf("%s",&arr.Pname);
printf("\n Please enter the item number: ");
scanf("%d",&arr.Pnum);
for(c = 0; c<i;c++){
while (arr[c].Pnum == arr.Pnum){
printf("\n Duplicate item number detected!, try again: ");
scanf("%d",&arr.Pnum);
}
}
printf("\n Please enter the item quantity: ");
scanf("%d",&arr.quantity);
printf("\n Please enter the item price: ");
scanf("%d",&arr.price);
printf("\n Please enter the item re-order point: ");
scanf("%d",&arr.re_orderp);
printf("\n\n");



}
}

else if(a == 2){
printf("\n Please enter item number: ");
scanf("%d",&B);

for(i = 0; i<= 1;i++){
if(b == arr.Pnum){
printf("\n\n");

printf("\n\t ----------------------");
printf("\n\t Inventory Report");
printf("\n\t ----------------------\n\n");

printf("\n Item Name: %s\n",arr.Pname);
printf("\n Item Number: %d\n",arr.Pnum);
printf("\n Item Quantity: \n"); quancalc(arr.Pnum);
printf("\n Item Price: $%d\n",arr.price);
printf("\n Item Re-Order Point: %d\n\n",arr.re_orderp);
}
}
printf("\n This is the end of the report. \n\n");
}

else if(a == 3){
printf("\n Thanks for visiting, goodbye! \n\n");
break;
}

else {
printf("\n You have made an invalid selection, please try again.\n\n");
}

}

system("pause");
}

quancalc (int x[]){
int qhand, sold;
printf("\n Item amount sold: ");
scanf("%d",&sold);
for(int f=0;f<=1;f++){
qhand =x[f]-sold;
} return qhand;
}

Share this post


Link to post
Share on other sites
Advertisement
A few tips:

1. use the [ code] and [ /code] tags (without spaces) to get code highlighted and indented.

2. Tell us at what line the error occurres by means of a comment.




assainator

Share this post


Link to post
Share on other sites
Looks to be because your quancalc() function takes an array of numbers, but you are trying to pass it a single value.


int quancalc(int[]);
//...
printf("\n Item Quantity: \n"); quancalc(arr.Pnum);

The quancalc function looks wrong too. You are indexing an array of numbers but no where in your program have you created such an array.
I think maybe you meant to pass a single int value by address in which case this would be what you are looking for:

void quancalc(int*);
//...
printf("\n Item Quantity: \n", arr.quantity); quancalc(&arr.quantity);
//...
void quancalc (int *x){
int qhand, sold;
printf("\n Item amount sold: ");
scanf("%d",&sold);
(*x)-=sold;
}

I'm not positive that this is the functionality you are looking for, but from what I can glean from your code it looks like it might be.

Also you try to read into B which doesn't exist in your program.

//...
printf("\n Please enter item number: ");
scanf("%d",&B); // Assuming this should be 'b'
//...


Also make sure that you initialize your variables, otherwise you'll have a lot of undefined behavior throughout your program.

Share this post


Link to post
Share on other sites
There are a few problems with your code.

Initialise your variables. The variable "b" is used before it is ever set*.

You should make space for your strings. Using raw string pointers in a structure is dangerous, here you are not allocating any memory so you write over arbitrary memory when you try read into them.

When you want to scanf() into a string, do not take the address of it. Pass it a pointer or an array.

Give your variables meaningful names. Instead of "a", use "menuChoice". Also look into replacing "magic numbers" with named constants, so you could replace if(a == 1) with if(menuChoice == MENU_CHOICE_ENTER_NEW_INFO).

If you want to loop over an array of 3 objects, the idiomatic loops if for(int i = 0 ; i < 3 ; i++). Again, following the above advice you can replace the number 3 with a named constant. Your current loop (i = 0 ; i <= 1 ; i++) only loops over 2 elements*.

Don't be afraid to write extra functions. For example, finding the array index of the element with the given number could be a separate function. Testing the array for duplicates could also be separate function, and you could write it in terms of the first one. For example, if you had already written int findItemByNumber(parts [] array, int numElements, int number) and written it such that it returns the index with the number, or -1 - you could write the following:

bool isDuplicate(parts [] array, int numElements, int number)
{
int index = findItemByNumber(array, numElements, number);
return index != -1;
}

Even choosing a menu option and the various menu choice logic should be moved to different functions.



There may be other issues but this will give you plenty to chew on.

*[size="1"] For these points, convince yourself this is the case

Share this post


Link to post
Share on other sites
thanx for all the info guys, but i was told by my lecturer last night that this is all wrong. i need to use a sequential FILE operation. so ama have to do it all over. but thankx anyway.

PS: belaryus and rip-off, i did actually get it working, i just scrapped the idea of loading the values into the function and used it simply to display the result of my calculation. i have to do it over now so i'm supposing i'm going to need to insert values, but check it out none the less:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct parts{ //creating the record of information//
char* Pname[30];
int Pnum;
int quantity;
int price;
int re_orderp;
int min_ord;
};

int quancalc(int); //function prototype to print the quantity on hand//

int main(){
struct parts arr[3]; //storing the records in an array//
int i, a,b,c,sold,qhand,orderam;
char ans,sel;


printf("\t ***************************************************\n");
printf("\t Benz Auto Supplies Inventory System \n");
printf("\t ***************************************************\n\n");
printf(" Welcome, please read and follow all instructions carefully! \n");
printf("\n Please select one of the following options: \n");
while(a <= 3 || a > 3){ //while loop to loop the menu//
printf("\n Press 1 to enter item information"); //menu selection initialization//
printf("\n Press 2 to view supply info");
printf("\n Press 3 to exit the system \n");
printf("\n Selection: ");
scanf("%d",&a);

if(a == 1){ //selection of menu item 1 definition//
for(i = 0; i<= 1;i++){ //sequential loop to populate info in record//

printf("\n Please enter the item name: ");
scanf("%s",&arr.Pname); fflush(stdin);
printf("\n Please enter the item number: ");
scanf("%d",&arr.Pnum);
for(c = 0; c<i;c++){ //error check for duplicate item numbers//
while (arr[c].Pnum == arr.Pnum){
printf("\n Duplicate item number detected!, try again: ");
scanf("%d",&arr.Pnum);
}
}
printf("\n Please enter the item quantity: ");
scanf("%d",&arr.quantity);
printf("\n Please enter the item price: ");
scanf("%d",&arr.price);
printf("\n Please enter the item re-order point: ");
scanf("%d",&arr.re_orderp);
printf("\n Please enter the minimum order amount for this item: ");
scanf("%d",&arr.min_ord);
printf("\n\n");



}
}

else if(a == 2){ //selection of menu item 2 definition//
printf("\n Please enter item number: ");
scanf("%d",&B);

for(i = 0; i<= 1;i++){ //sequential loop to display the records//
if(b == arr.Pnum){
if(arr.Pnum > 0){ //error check for when option 1 is bypassed//
printf("\n Item amount sold: "); //accepting a value that decrements the quantity//
scanf("%d",&sold);
printf("\n\n");

printf("\n\t ----------------------");
printf("\n\t Inventory Report");
printf("\n\t ----------------------\n\n");

printf("\n Heading Value ");
printf("\n ********* ******* \n\n");
printf("\n Item Name: %s\n",arr.Pname);
printf("\n Item Number: %d\n",arr.Pnum);
printf("\n Item Price: $%d\n",arr.price);
printf("\n Item Re-Order Point: %d\n",arr.re_orderp);
printf("\n Item Quantity: ");
qhand = arr.quantity - sold; //operation to calculate quantity on hand//
orderam = arr.re_orderp + (arr.min_ord - qhand); //operation to calculate order amount//
quancalc(qhand);
if(qhand <= arr.re_orderp) //operation to display order amount//
{printf("\n Quantity low, please re-order ");
printf("\n Order amount: %d\n\n",orderam);}

fflush(stdin);
printf("\n Update item info Y/N: "); //option to update item info//
scanf("%c",&ans); fflush(stdin);
printf("\n\n");

if(ans == 'Y'){
printf("\n Enter P for price, or Q for quantity: ");
scanf("%c",&sel);
if(sel == 'P'){ //updating item price//
printf("\n Please enter the item price: ");
scanf("%d",&arr.price);
printf("\n Item updated.");
printf("\n\n");
}
else if(sel == 'Q'){ //updating item quantity//
printf("\n Please enter the item quantity: ");
scanf("%d",&arr.quantity);
printf("\n Item updated.");
printf("\n\n");
}
}


}
else if(arr.Pnum == 0){
printf("\n No records stored! \n");
}
}

}

printf("\n End of report. \n\n"); //end of report message//
}

else if(a == 3){ //selection of menu item 3 definition//
printf("\n Thanks for visiting, goodbye! \n\n");
break;
}

else { //error check message for incorrect selection//
printf("\n You have made an invalid selection, please try again.\n\n");
}

}

system("pause");
}

quancalc (int y){ //function definition for quantity on hand//
printf("%d \n\n",y);
}

Share this post


Link to post
Share on other sites
You shouldn't need to flush() stdin like that. Most of the comments I gave above apply to your new code. Also note that your function definition for "quancalc" lacks a return type, and is declared as returning an int yet it doesn't feature a return value. Consider increasing your compiler warning level until it warns you about these kinds of mistakes. Use [ code ] tags in future when posting source code, it will syntax-highlight it and puts it in a scroll box.

Share this post


Link to post
Share on other sites


struct parts{ //creating the record of information//
char* Pname[30];
.
.
.

printf("\n Please enter the item name: ");
scanf("%s",&arr.Pname);
}


O boy there's a lot wrong, but I want to point out one..

Your structure "parts" defines an array of 30 char *'s (pointers to char). This doesn't do what you think it should. It's going to work because there's 30*sizeof(char*) bytes, which is 120 bytes, so it can hold up to 120 characters. What I believe you meant to do was simply:

char Pname[30];


This will define an array of 30 characters for your name.

Also, yor variable a (terrible variable names BTW) isn't initialized. You assume it will be 0, but it could be any number left on the stack. it shold be initialized to 0 so your 1st loop will always work.


Share this post


Link to post
Share on other sites
ok kool. thankx for that beernutts and rip-off. ama do it over, but i'm trying to learn file handling rite now so that i can do it. cause my lecturer said i have to use a file, it's part of the assignment. i'm really not too sure now that works, or if i can just fit it into my code above. but i'm trying to read up on it now. wen i'm done and i re-write it i'll let you guys take a look and tell me if it's good.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!