I am trying to make a seat reservation program using structures and functions (as part of my effort to try to learn C).
In the following code I have some problems in:
-Lines 82,143 ( seat reservation check is not working consistently) (Solved)
I changed from temp to temp - 1 the code in these lines and everything is normal..
-Lines 109 and 122 where I want to compare two phone-numbers that are saved as unsigned short arrays of 10 elements. (Solved)
#include <stdio.h>
#include <string.h>
int i,j,numberofseats,temp;
char selection;
typedef struct
{
char fullname[40];
unsigned short phonenr[10];
unsigned int seatnr;
}PASSENGERS;
void changeData(PASSENGERS *target){
char firstname[20];
char lastname[20];
char phone[11];
printf("Enter passenger's first name:");
scanf("%s",firstname);
printf("Enter passenger's last name:");
scanf("%s",lastname);
strcpy(target->fullname,firstname);
strcat (target->fullname, " ");
strcat(target->fullname,lastname);
printf("Enter passenger's phone Nr:");
for (i=0;i<10;i++) scanf("%hu",&(target->phonenr[i]));
}
void cancelSeat(PASSENGERS *target){
strcpy(target->fullname,"\0");
for (i=0;i<10;i++)
target->phonenr[i]=0;
printf("Seat Nr %d is now free",temp);
}
int main(void)
{
numberofseats=53;
PASSENGERS passenger[numberofseats];
for (j=0;j<numberofseats;j++)
{passenger[j].seatnr=j+1;
strcpy(passenger[j].fullname,"\0");
}
do{
printf("\n\nNeo Sistima katagrafis thesewn leoforeiou\n");
printf("Please make a selection:\n\n");
printf("0. Exit\n");
printf("1. Empty Seats \n");
printf("2. Book Specific Seat \n");
printf("3. Advanced Search of booked Seats\n");
printf("4. Cancel Seat Booking\n");
printf("5. Show List of booked Seats\n");
scanf(" %c",&selection);
if (selection=='1')
{int freeseats = 0;
for (j=0; j<numberofseats; j++)
{
strcmp(passenger[j].fullname,"\0")==0 ? freeseats = freeseats + 1 : freeseats ;}
printf ("There are %d free seats in this bus \n", freeseats);
printf("Seats that are available are:\n");
for (j=0; j<numberofseats; j++)
{if (strcmp(passenger[j].fullname,"\0")==0)
printf ("%hd\n", passenger[j].seatnr);
}
freeseats = 0;
}
else if (selection=='2')
{
printf("Please give seat nr (between 1 and %d) that you want to book:\n", numberofseats);
scanf("%d",&temp);
if (temp >numberofseats || temp <= 0)
{printf("Error: Seat nr should be between 1 and %d", numberofseats);}
else if (strcmp(passenger[temp].fullname,"\0")!=0)
printf("Error: Seat is already booked");
else
changeData(&passenger[temp-1]);
}
else if (selection=='3')
{
char tempsel,tmpfirst[20],tmplast[20];
unsigned short tempphone[10];
int counter, checkphone;
unsigned int tempseat;
printf("Do you want to search with Name (1) or Phone Nr (2)?\n");
scanf(" %c",&tempsel);
if (tempsel == '1')
{ printf("Enter passenger's first name:");
scanf("%s",tmpfirst);
printf("Enter passenger's last name:");
scanf("%s",tmplast);
strcat (tmpfirst, " ");
strcat(tmpfirst,tmplast);
for (j=0;j<numberofseats;j++)
if (strcmp(passenger[j].fullname,tmpfirst)==0)
printf ("Passenger %s has seat nr #: %hd\n",tmpfirst,passenger[j].seatnr);
}
else if (tempsel == '2')
{ checkphone=0;
printf("Enter passenger's phonenr:");
for (i=0;i<10;i++)
scanf("%hu",&tempphone[i]);
for (j=0;j<numberofseats;j++)
{
counter=0;
for(i=0;i<10;i++)
{
if (passenger[j].phonenr[i]==tempphone[i])
counter=counter+1;
if (counter ==10)
{checkphone=1;
tempseat=passenger[j].seatnr;
}}
}
if (checkphone==1)
{printf ("Passenger has seat #: %hd\n",tempseat);
checkphone=0;}
}
}
else if (selection=='4')
{
printf("Please give seat nr (between 1 and %d) that you want to cancel booking:\n", numberofseats);
scanf("%d",&temp);
if (temp >numberofseats || temp <= 0)
{printf("Error: Seat nr should be between 1 and %d", numberofseats);}
else if (strcmp(passenger[temp].fullname,"\0")==0)
printf("Error: Seat is already free");
else
cancelSeat(&passenger[temp-1]);
}
} while (selection!='0');
}
you have two problems :-
passed integer in strcpy and strcmp.
to print integer data you used %s (for temp)
Do modify your code like that :-
void cancelSeat(PASSENGERS *target){
strcpy(target->fullname,"\0");
for (i=0;i<10;i++)
// strcpy(target->phonenr[i],"\0");
printf("Seat Nr %d is now free",temp);
}
and change you case where you comparing telephone number
if (strcmp(passenger[j].phonenr[i],tempphone[i])==0) ==> if (passenger[j].phonenr[i]==tempphone[i])
Related
I am writing a program where if the user chooses "2" the program will add a new student record and if he chooses "4" then it will compare his surname with all the other surnames, it will find everyone that has the same surname and print their school information. The problems are the following:
When the user selects "2" my program actually shows all the prompts that are needed and you can input all the information however when I try to print it just gives me 0 0 0.000000 .When it should have given me 2(id) B(name) b(surname) 1(semester) 4(grade).
When the user selects "4" it reads the surname but when it goes to compare it with the others to see if it exists or not it just crashes.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student{
int id;
char name[50];
char surname[50];
int semester;
float grade;
}student;
int main()
{
int x,std,i;
x=0;
int car=0;
int flag=1;
struct student *ptr = NULL;
while (x!=8)
{
printf("\n1. Initialize student list\n2. Add a student record\n3. Delete a student record\n4. Display a student record by student surname\n5. Display students passed\n6. Display students failed\n7. Display all student records\n8. Exit");
printf("\nYour choice: ");
scanf("%d",&x);
if (x==1)
{
printf("How many new students? ");
scanf("%d",&std);
ptr = (student*)malloc(std*sizeof(student));
for(i = 0; i < std; i++){
printf("Enter detail of student #%d\n", (i + 1));
ptr[i].id=i+1;
printf("Enter first name: ");
scanf("%s", ptr[i].name);
printf("Enter last name: ");
scanf("%s", ptr[i].surname);
printf("Enter semester: ");
scanf("%d", &ptr[i].semester);
printf("Enter grade: ");
scanf("%f", &ptr[i].grade);
}
}
else if (x==2)
{
ptr=realloc(ptr,100* sizeof(student));
std=std+1;
ptr[std].id=std+1;
printf("Enter first name: ");
scanf("%s", ptr[std].name);
printf("Enter last name: ");
scanf("%s", ptr[std].surname);
printf("Enter semester: ");
scanf("%d", &ptr[std].semester);
printf("Enter grade: ");
scanf("%f", &ptr[std].grade);
flag=0;
}
else if (x==3)
{
/* code */
}
else if (x==4)
{
printf("Give the surname: ");
const char *sur;
scanf("%s", sur);
for (i = 0; i < std; i++)
{
if(strcmp(sur, ptr[i].surname)==0){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
car=1;
}
}
if (car=0)
{
printf("That surname does not exist");
}
}
I may have missed a "{" but this is only part of my code
else if (x==7)
{
for(i = 0; i < std; i++){
printf("%d ", ptr[i].id);
printf("%s ", ptr[i].name);
printf("%s ", ptr[i].surname);
printf("%d ", ptr[i].semester);
printf("%f ", ptr[i].grade);
printf("\n");
}
}
}
}
For when the user chooses "2" everything goes well until its time to print so i tried increasing std by 1 two times.One when the program goes to option "2" and another time when it goes back to main. It didnt do anything so i returned it to how it was in the beginning.
For when the user chooses "4" I tried going with strcmp but it would just throw errors because of sur. I had it written as char sur. After I wrote it as const char *sur it stopped throwing errors but it would just crash when it went to compare it with the other.
I need to make some storage record, I need to be able to insert, see the data, and search according to an id (I haven't reached the search part yet). My problem is either the data not recorded, or it couldn't be printed both of them due to indexing problem imo. what do I need to do?
struct item{
char *name;
int qty;
}item [10];
int main ()
{
int i=0,n,menu;
printf("Goody Storage\n");
printf("=============\n");
printf("Input Storage[1..10]: ");
scanf("%d",&n);
for(;;i++){
if(n<=10){
printf("\nGoody Storage\n");
printf("=============\n");
printf("\n1. Add Items\n");
printf("2. See Items\n");
printf("3. Search Items\n");
printf("4. Exit\n");
printf("Choose Menu[1..4]: ");
scanf("%d",&menu);
switch(menu){
case 1 :
printf("Input name of item %d: ", i+1);
scanf("%s",&item[i].name);
printf("Input qty of item %d: ", i+1);
scanf("%d",&item[i].qty);
continue;
case 2 :
printf("\nNo.\tName\t\t\tQuantity\n");
printf("========================================\n");
printf("%d\t%s\t\t\t%d\n", i, item[i].name, item[i].qty);
continue;
/*case 3 :
printf("\nInvalid Choice");
continue;*/
case 4 :
return 0;
default :
printf("\nInvalid Choice");
}
} else break;
}
}
So in the function getRoster, I have two arrays that are defined in the same way in main and called in the same way. But for some reason, one of the scanf functions write garbage data to the array so that in line 91 (A debugging line in this case) one value is the one I entered and the other is seemingly random. I've checked this code top to bottom already to see if there were any notations that I added to one array and not the other, and I can't seem to find a single one.
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers[10], int *playerRatings[10])
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1);
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers[10], int *playerRatings[10])
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int *jerseyNumbers[10];
int *playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(&jerseyNumbers[10], &playerRatings[10]);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(&jerseyNumbers[10], &playerRatings[10]);
return 0;
}
Refer to #NaveenKumar and #DeiDei's comment to your question.
Actually, the logic of your program is alright. But where you've gone wrong is the syntax regarding how you have declared the arrays and passed them as arguments to the functions. I have listed the changes to be made below:
First of all Dylan, do not declare an array as: int
*jerseyNumbers[10] and int *playerRatings[10]. For the regular array which you actually need, just declare as:
a. *jerseyNumbers and *playerRatings... Remove the size. OR
b. jerseyNumbers[10] and playerRatings[10]... Remove the * from the declaration.
When you're passing these arrays as arguments to a function, don't ever send it how you've done. Just pass the array name as an argument. Like this: getRoster(jerseyNumbers, playerRatings); and menu(jerseyNumbers, playerRatings);.
In the function definition, the parameters representing the array should be either *arrayname or arrayname[size]. Since we have used the first during declaration, use the same here as follows:
void getRoster(int *jerseyNumbers, int *playerRatings) {...},
void updateRating(int *jerseyNumbers, int *playerRatings){...},
void outputRoster(int *jerseyNumbers, int *playerRatings) {...},
void menu(int *jerseyNumbers, int *playerRatings) {...}.
And as #PaulSm4 has suggested don't use the semi-colon at the end of an if conditional. Though this isn't the cause of your problem here, it is a practice you need to follow and mistake to be avoided.
I have attached the working code below, along with the output.
CODE:
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1)
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers, int *playerRatings)
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(jerseyNumbers, playerRatings);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(jerseyNumbers, playerRatings);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int jerseyNumbers[10];
int playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(jerseyNumbers, playerRatings);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(jerseyNumbers, playerRatings);
return 0;
}
OUTPUT:
Enter player 1's jersey number
1
Enter player 1's rating
10
Enter player 2's jersey number
2
Enter player 2's rating
20
Enter player 3's jersey number
3
Enter player 3's rating
30
Enter player 4's jersey number
4
Enter player 4's rating
40
Enter player 5's jersey number
5
Enter player 5's rating
50
1 20
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: o
Player 1 -- Jersey number: 1 , Rating: 10
Player 2 -- Jersey number: 2 , Rating: 20
Player 3 -- Jersey number: 3 , Rating: 30
Player 4 -- Jersey number: 4 , Rating: 40
Player 5 -- Jersey number: 5 , Rating: 50
Hope this helps.
STRONG SUGGESTION:
Get in the habit of using curly braces often - even when you don't need them.
void updateRating(int *jerseyNumbers, int *playerRatings)
// You probably only need a pointer or an array - but probably not both ;)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i) { // Curly brace here...
if(jerseyNumbers[i]==n) { // And here...
hold = n;
}
}
if (n=-1) { // BUG ALERT: deleted ";" (and added a brace)
printf("Error"); // BUG ALERT: fixed this by removing ";"?
} else {
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
Or perhaps better:
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int n;
printf("Enter a jersey number:\n");
scanf("%d", n);
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
And, as Rishikesh Raje correctly pointed out:
The definition of the array's and the function calls for OP is also
not correct. Please mention this also.
Currently, I am trying to create a program that takes inventory of a stores products. I have been able to create a function that allows the user to input new items in a struct array but when I attempt to print the values I get garbage values. (Please ignore the switch statements as the code is work in progress).
#include <stdio.h>
#include <stdlib.h>
#define MAX_INVENTORY_SIZE 100
typedef struct {
char item_Number[3];
char item_Name[20];
float item_Profit;
float latest_Price;
unsigned int stock;
unsigned int total_Sold;
struct InventoryItemType *next;
}InventoryItemType;
void MainMenu();
void displayInventory(InventoryItemType *(*));
void addItem(InventoryItemType, int i);
int main()
{
int i=1;
char selection;
int count=1;
InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE] ;
inventoryItems[0]=NULL;
while(1)
{
MainMenu();
scanf(" %c", &selection);
switch(selection)
{
case 'A' :
displayInventory(inventoryItems);
break;
case 'B' :
case 'C' :
inventoryItems[count]= (InventoryItemType*)malloc(sizeof(InventoryItemType));
addItem(*inventoryItems[count], count);
continue;
case 'D' :
case 'E' :
case 'F' :
case 'G' :
case 'H' :
default :
printf("Invalid\n" );
}
printf("Bottom of code\n");
system("pause");
}
}
void MainMenu()
{
printf("A. Display Inventory\n");
printf("B. Display Sales\n");
printf("C. Add Item\n");
printf("D. Remove Item\n");
printf("E. Enter Shipment\n");
printf("F. Update Sales\n");
printf("G. Sort\n");
printf("H. Exit\n");
printf("Make a selection\n");
}
void displayInventory(InventoryItemType *display)
{
int i;
for(i=0; i<MAX_INVENTORY_SIZE; i++)
{
printf("Name:%s\n", display[i].item_Name);
printf("Stock:%d\n", display[i].stock);
printf("Price:%.2f\n", display[i].latest_Price);
printf("Total Value:%.2f\n", (display[i].stock)*(display[i].latest_Price));
printf("\n");
}
}
void addItem(InventoryItemType display)
{
printf("\nEnter details of item \n\n");
printf("Enter Item Name: \n");
scanf("%s", display.item_Name);
printf("\nEnter Item no: \n");
scanf("%s", display.item_Number);
printf("\nEnter Stock: \n");
scanf("%d", &display.stock);
printf("\nPurchase Price: \n");
scanf("%f", &display.latest_Price);
}
Update
I attempted to apply all answers (with a series of trial and error) and came up with this code. I currently have the code properly taking in a new item and displaying that item, but it breaks down after adding more items(if I continue through the errors I get garbage display values). I am pretty certain it has to do with the way in which I am allocating memory via malloc. My goal is to use malloc to create the space for the item before initializing values.
Edited code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_INVENTORY_SIZE 100
typedef struct {
char item_Number[4];
char item_Name[20];
float item_Profit;
float latest_Price;
float selling_Price;
unsigned int stock;
unsigned int total_Sold;
}InventoryItemType;
void MainMenu();
void displayInventory(InventoryItemType *, int);
void displaySales(InventoryItemType *, int);
InventoryItemType *addItem(InventoryItemType *, int);
int main()
{
int i=0, item_count=0;
char selection;
InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE];
while(1)
{
MainMenu();
scanf(" %c", &selection);
switch(selection)
{
case 'A' :
displayInventory(*inventoryItems, item_count);
break;
case 'B' :
displaySales(*inventoryItems, item_count);
break;
case 'C' :
inventoryItems[item_count]=(InventoryItemType*)malloc(sizeof(InventoryItemType));
inventoryItems[item_count]=addItem(inventoryItems[item_count],item_count);
item_count++;
continue;
case 'D' :
case 'E' :
case 'F' :
case 'G' :
case 'H' :
default :
printf("Invalid Entry\n" );
system("pause");
}
system("cls");
}
}
void MainMenu()
{
printf("A. Display Inventory\n");
printf("B. Display Sales\n");
printf("C. Add Item\n");
printf("D. Remove Item\n");
printf("E. Enter Shipment\n");
printf("F. Update Sales\n");
printf("G. Sort\n");
printf("H. Exit\n");
printf("Make a selection\n");
}
void displayInventory(InventoryItemType *display, int key)
{
if(display[0].item_Name == NULL)
{
printf("No stock");
system("pause");
}
else
{
int i;
for(i=0; i<key; i++)
{
printf("Item No.:%s\n", display[i].item_Number);
printf("Item Name:%s\n", display[i].item_Name);
printf("Item Stock:%d\n", display[i].stock);
printf("Item Purchased Price:%.2f\n", display[i].latest_Price);
printf("Total Value of Items:%.2f\n", (display[i].stock)*(display[i].latest_Price));
printf("\n");
system("pause");
}
}
}
void displaySales(InventoryItemType *display, int key)
{
int i;
float total_profit=0;
for(i=0; i<key; i++)
{
printf("Item No.:%s\n", display[i].item_Number);
printf("Item Name:%s\n", display[i].item_Name);
printf("Number of Item Sold:%d\n", display[i].total_Sold);
printf("Item Selling Price:%.2f\n", display[i].selling_Price);
printf("Total Profit from Item:%.2f\n", (display[i].selling_Price-display[i].latest_Price)*display[i].total_Sold);
total_profit=total_profit+((display[i].selling_Price-display[i].latest_Price)*display[i].selling_Price);
if(i==key)
printf("Total Over-all Profit:%.2f", total_profit);
system("pause");
}
}
InventoryItemType *addItem(InventoryItemType *change, int key)
{
InventoryItemType *current= (InventoryItemType*)malloc(sizeof(InventoryItemType));
printf("\nEnter details of item \n\n");
printf("Enter Item no: \n");
scanf("%s", current[key].item_Number);
printf("Enter Item Name: \n");
scanf("%s", current[key].item_Name);
printf("Enter Stock: \n");
scanf("%d", ¤t[key].stock);
printf("Enter Purchase Price: \n");
scanf("%f", ¤t[key].latest_Price);
current[key].selling_Price=(current[key].latest_Price)*1.5;
current[key].total_Sold=0;
change=current;
system("cls");
return change;
}
There are a number of bugs/conflicts. Too many to comment on individually. I've annotated and corrected the code, showing bugs and before/after. There is still more work to do, but this should get you closer.
#include <stdio.h>
#include <stdlib.h>
#define MAX_INVENTORY_SIZE 100
typedef struct {
char item_Number[3];
char item_Name[20];
float item_Profit;
float latest_Price;
unsigned int stock;
unsigned int total_Sold;
struct InventoryItemType *next;
} InventoryItemType;
void MainMenu();
#if 0
void displayInventory(InventoryItemType *(*));
#else
void displayInventory(InventoryItemType *,int);
#endif
#if 0
void addItem(InventoryItemType, int i);
#else
void addItem(InventoryItemType *);
#endif
int main()
{
int i=1;
char selection;
// NOTE/BUG: starting at 1 will leave item 0 undefined
#if 0
int count=1;
#else
int count=0;
#endif
// better to do this with a stack array than pointers to malloc'ed items
#if 0
InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE] ;
#else
InventoryItemType inventoryItems[MAX_INVENTORY_SIZE] ;
#endif
#if 0
// NOTE/BUG: this will segfault because inventoryItems is undefined here
inventoryItems[0]=NULL;
#endif
while (1) {
MainMenu();
scanf(" %c", &selection);
switch(selection) {
case 'A' :
#if 0
displayInventory(inventoryItems);
#else
displayInventory(inventoryItems,count);
#endif
break;
case 'B' :
case 'C' :
#if 0
inventoryItems[count]= (InventoryItemType*)malloc(sizeof(InventoryItemType));
addItem(*inventoryItems[count], count);
#else
addItem(&inventoryItems[count]);
count += 1;
#endif
continue;
case 'D' :
case 'E' :
case 'F' :
case 'G' :
case 'H' :
default :
printf("Invalid\n" );
}
printf("Bottom of code\n");
system("pause");
}
}
void MainMenu()
{
printf("A. Display Inventory\n");
printf("B. Display Sales\n");
printf("C. Add Item\n");
printf("D. Remove Item\n");
printf("E. Enter Shipment\n");
printf("F. Update Sales\n");
printf("G. Sort\n");
printf("H. Exit\n");
printf("Make a selection\n");
}
#if 0
void displayInventory(InventoryItemType display)
{
int i;
for(i=0; i<MAX_INVENTORY_SIZE; i++)
{
printf("Name:%s\n", display[i].item_Name);
printf("Stock:%d\n", display[i].stock);
printf("Price:%.2f\n", display[i].latest_Price);
printf("Total Value:%.2f\n", (display[i].stock)*(display[i].latest_Price));
printf("\n");
}
}
void addItem(InventoryItemType display)
{
// NOTE/BUG: because this is passed by _value_ nothing will be retained
// after function return
printf("\nEnter details of item \n\n");
printf("Enter Item Name: \n");
scanf("%s", display.item_Name);
printf("\nEnter Item no: \n");
scanf("%s", display.item_Number);
printf("\nEnter Stock: \n");
scanf("%d", &display.stock);
printf("\nPurchase Price: \n");
scanf("%f", &display.latest_Price);
}
#else
void displayInventory(InventoryItemType *item,int count)
{
int i;
for (i=0; i<count; i++, item++) {
printf("Name:%s\n", item->item_Name);
printf("Stock:%d\n", item->stock);
printf("Price:%.2f\n", item->latest_Price);
printf("Total Value:%.2f\n", (item->stock)*(item->latest_Price));
printf("\n");
}
}
void addItem(InventoryItemType *item)
{
printf("\nEnter details of item \n\n");
printf("Enter Item Name: \n");
scanf("%s", item->item_Name);
printf("\nEnter Item no: \n");
scanf("%s", item->item_Number);
printf("\nEnter Stock: \n");
scanf("%d", &item->stock);
printf("\nPurchase Price: \n");
scanf("%f", &item->latest_Price);
}
#endif
There are many problems with your code.
InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE] ;
This declares an array of dimension MAX_INVENTORY_SIZE of InventoryItemType
pointers. These pointers are uninitialized, so you need to allocate memory for
it. You do it here:
inventoryItems[count]= (InventoryItemType*)malloc(sizeof(InventoryItemType));
Don't cast malloc
The count variable is initialized to 1 and it is never changed. So every
time you are allocating new memory for inventoryItems[1] and you are losing
the pointer of the previous malloc call, thus it's leaking.
When you print your data structure, you do:
for(i=0; i<MAX_INVENTORY_SIZE; i++)
{
printf("Name:%s\n", display[i].item_Name);
...
but inventoryItems[i] is for all i != 1 uninitialized, this yield undefined
behaviour.
What's the point of having an array of InventoryItemType pointers when the
structure itself has a pointer next pointer for the next item in the inventory. It seems you
are mixing concepts here, linked lists vs arrays. I'd suggest you pick one and
stick to it.
Why did you
void addItem(InventoryItemType display);
declare addItem like this? display is only a copy of the orginal, so any
change of the values of display will only affect display, not the original.
So when you do
addItem(*inventoryItems[count], count);
you are passing a copy to addItem and you only change the copy,
inventoryItems[count] remains unchanged. Also your function takes only one
parameter, you are passing it two.
addItem should take a pointer, so that the changes are done through the
pointer into the original. The function should look like this:
int addItem(InventoryItemType *display);
{
if(display == NULL)
return 0;
printf("\nEnter details of item \n\n");
printf("Enter Item Name: \n");
if(scanf("%19s", display->item_Name) != 1)
return 0;
clear_stdin();
printf("\nEnter Item no: \n");
if(scanf("%2s", display->item_Number) != 1)
return 0;
clear_stdin();
printf("\nEnter Stock: \n");
if(scanf("%d", &display->stock) != 1)
return 0;
printf("\nPurchase Price: \n");
if(scanf("%f", &display->latest_Price) != 1)
return 0;
return 1;
}
The function should return 1 on success, 0 otherwise. The caller of addItem
will know whether addItem failed or not. If you declare the function as
void, then you will never know if the function failes or not.
And clear_stdin could look like this:
void clear_stdin(void)
{
int c;
while((c = getchar()) != '\n' && c != EOF);
}
edit
As you've made an update and you've decided to use the array, then I can give
you a few more pointers.
You are declaring to much memory. Either you allocate memory before addItem
and pass the newly allocated memory to addItem, or you allocate the memory in
addItem itself and return it. Your addItem already does that, so the first
line
inventoryItems[item_count]=(InventoryItemType*)malloc(sizeof(InventoryItemType));
is not necessary, because addItem does that for you.
I'd change addItem to look like this:
InventoryItemType *addItem(void)
{
InventoryItemType *current = calloc(1, sizeof *current);
if(current == NULL)
return NULL;
printf("\nEnter details of item \n\n");
printf("Enter Item no: \n");
scanf("%2s", current->item_Number);
clear_stdin();
printf("Enter Item Name: \n");
scanf("%20s", current->item_Name);
clear_stdin();
printf("Enter Stock: \n");
scanf("%d", ¤t->stock);
printf("Enter Purchase Price: \n");
scanf("%f", ¤t->latest_Price);
current->selling_Price=(current->latest_Price)*1.5;
current->total_Sold=0;
system("cls");
return current;
}
Because addItem allocates the memory for the new object, you don't need to
pass the array nor the index (or key) where you want to store it. Then in the
main you can do this:
case 'C':
// checking that you don't step outside of bounds
if(item_count == MAX_INVENTORY_SIZE - 1)
{
fprintf(stderr, "Array is full\n");
break;
}
inventoryItems[item_count] = addItem();
if(inventoryItems[item_count] == NULL)
{
fprintf(stderr, "Not enough memory\n");
break;
}
item_count++;
continue;
case 'D':
...
The first screen, gotoxy code is working. But in second screen. Nothing happens, like, it does not read the gotoxy code at all. Please enlighten me about the problem.
Here is the 1st screen :
Here is the 2nd screen :
Here is the code. I would love to learn more about gotoxy.
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
void gotoxy( int column, int line );
int main();
int addProduct();
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
COORD coord = {0, 0};
void gotoxy (int x, int y)
{
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int choice;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22,8);
printf("1. Add Product\n\n");
gotoxy(22,10);
printf("2. Display Product\n\n");
gotoxy(22,12);
printf("3. Search Product\n\n");
gotoxy(22,14);
printf("4. Reorder Level of Product\n\n");
gotoxy(22,16);
printf("5. Update Product\n\n");
gotoxy(22,20);
printf("Please Enter Your Choice : ");
scanf(" %d", &choice);
switch(choice)
{
case 1 : addProduct();
break;
case 2 : displayProduct();
break;
case 3 : searchProduct();
break;
case 4 : reorderProduct();
break;
case 5 : updateProduct();
break;
default : printf("Wrong input. Please try again.");
system("cls");
main();
}
return (0);
}
int addProduct()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
do
{
fp = fopen("inventory.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
if(checker == 'N')
{
main();
}
else
{
do{
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
}while(checker != 'Y' && checker != 'N');
if(checker == 'Y'){addProduct();}
if(checker == 'N'){
system("cls");
main();}
}
return(0);
}