malloc and free on a Stack in C - c

I'm trying to write a code that dynamically writes the Coordinates of a Point on the Stack and prints (and frees) them back:
#include <stdio.h>
#include <stdlib.h>
struct point{
float x;
float y;
float z;
}; typedef struct point POINT;
struct stackPoint{
POINT myPoint;
struct stackPoint *next;
}; typedef struct stackPoint STACKPOINT;
static STACKPOINT *stacktop = NULL;
void printStackElement(POINT aPoint){
printf(" x:%f \t y:%f \t z:%f\n", aPoint.x, aPoint.y, aPoint.z );
}
void push(POINT pushPoint){
STACKPOINT *newElem = malloc(sizeof(STACKPOINT));
stacktop = stacktop +1;
newElem->myPoint = pushPoint;
stacktop = newElem;
}
POINT pop(){
POINT b = stacktop->myPoint;
free(stacktop);
stacktop = stacktop -1;
return b;
}
int isEmpty(){
if(stacktop == NULL){
return 1;
}
return 0;
}
POINT readPoint(){
POINT a;
printf("Please enter your x-Coordinate: ");
scanf(" %f", &a.x);
printf("Please enter your y-Coordinate: ");
scanf(" %f", &a.y);
printf("Please enter your z-Coordinate: ");
scanf(" %f", &a.z);
return a;
}
int main(){
char quit = 0;
while(quit !=1 ){
printf("\n\n enter 'p' to enter another Point or 'q' to quit: " );
scanf(" %s", &quit);
switch(quit){
case 'p':
push(readPoint());
break;
case 'q':
quit = 1;
break;
default:
break;
}
}
while(isEmpty() == 0){
printStackElement(pop());
}
}
It prints the last entry but before printing the second to last entry, just an error message appears, that the "pointer beeing freed was not allocated".
I tried running it without the free() command, but then it just prints the first line and infite lines of just 0's
I also tried using the *stackTop pointer as a non static pointer instead of the *newElem pointer but that also didnt work..

It is supposed to be a linked list. Our professor just gave us this exercise and never even mentioned a linked list in any way or form.. Thank you very much, it works now!
I changed the push function to:
STACKPOINT *newElem = malloc(sizeof(STACKPOINT));
newElem->myPoint = pushPoint;
newElem->next = stacktop;
stacktop = newElem;
and the pop function to:
POINT b = stacktop->myPoint;
free(stacktop);
stacktop = stacktop->next;
return b;

Related

Error when adding another record to dynamic memory database

My task is to initialize a struct in a header with some data. Then using pointers, add/remove data by malloc-ing bigger/smaller chunks and copying the data over.
Currently, my addRecord function doesn't work as it always seems to add the same crap (Part of record number 1):
Name =
Fire Number = attan (Seems to be part of Manhattan)
Street = ork (Seems to be part of New York)
City = cret (Seems to be part of Secret)
State = tan (Seems to be part of Manhattan)
What am I doing wrong?
My header:
#include <stdio.h>
#include <stdlib.h>
struct structPointer{
char name[51];
char fireNumber[11];
char street[26];
char city[26];
char state[26];
};
My c file:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "myData.h"
struct structInit *sP;
struct structInit *sSP;
int recordNumber = 0;
int numberOfAccesses = 0;
int main(void) {
sP = (struct structInit *) malloc(5 * sizeof(struct structInit));
sSP = sP;
memcpy(sP->name,"Adam Baum",51);
memcpy(sP->fireNumber,"N1234",11);
memcpy(sP->street,"Top Secret",26);
memcpy(sP->city,"Manhattan",26);
memcpy(sP->state,"New York",26);
sP++;
recordNumber++;
memcpy(sP->name,"Adam Zapel",51);
memcpy(sP->fireNumber,"S4321",11);
memcpy(sP->street,"Throat",26);
memcpy(sP->city,"Manhattan",26);
memcpy(sP->state,"New York",26);
sP++;
recordNumber++;
memcpy(sP->name,"Al Bino",51);
memcpy(sP->fireNumber,"W1234",11);
memcpy(sP->street,"White",26);
memcpy(sP->city,"Anchorage",26);
memcpy(sP->state,"Alaska",26);
sP++;
recordNumber++;
memcpy(sP->name,"Anne Teak",51);
memcpy(sP->fireNumber,"E4321",11);
memcpy(sP->street,"Oak",26);
memcpy(sP->city,"Woodville",26);
memcpy(sP->state,"Wisconsin",26);
sP++;
recordNumber++;
memcpy(sP->name,"Barb Dwyer",51);
memcpy(sP->fireNumber,"N1234",11);
memcpy(sP->street,"Keepout",26);
memcpy(sP->city,"Kilgore",26);
memcpy(sP->state,"Texas",26);
recordNumber++;
sP = sSP;
int sel;
while (1){
printf("MENU\n");
printf("=====\n");
printf("1. Print All Records\n");
printf("2. Print Number of Records\n");
printf("3. Print Size of Database\n");
printf("4. Add Record\n");
printf("5. Delete Record\n");
printf("6. Print Number of Accesses to Database\n");
printf("7. Exit\n");
printf("=====\n");
printf("Enter selection: ");
scanf("%d", &sel);
printf("\n");
switch(sel){
case 1:
numberOfAccesses++;
printAllRecords(sP);
break;
case 2:
numberOfAccesses++;
fprintf(stderr,"There are a Total of %d records.\n\n", recordNumber);
break;
case 3:
numberOfAccesses++;
printSizeOfDatabase(sP);
break;
case 4:
numberOfAccesses++;
sP = sSP;
addRecord(sP);
break;
case 5:
numberOfAccesses++;
deleteRecord(sP);
break;
case 6:
numberOfAccesses++;
fprintf(stderr,"The total number of Accesses is %d\n\n", numberOfAccesses);
break;
case 7:
exit(0);
default:
printf("Error: Input was not a valid selection.\n\n");
break;
}
}
return 0;
}
int printAllRecords(struct structInit *structPointer){
int i;
structPointer = sSP;
printf("All Records: \n");
for(i=1;i<=recordNumber;i++){
printf("Record Number: %d\n", i);
fprintf(stderr, "Name = \%s\n", structPointer-> name);
fprintf(stderr, "Fire Number = \%s\n", structPointer-> fireNumber);
fprintf(stderr, "Street = \%s\n", structPointer-> street);
fprintf(stderr, "City = \%s\n", structPointer-> city);
fprintf(stderr, "State = \%s\n\n", structPointer-> state);
structPointer++;
}
return 1;
}
int printSizeOfDatabase(struct structInit *structPointer) {
int size = 0;
int i;
for (i=1;i<=recordNumber;i++) {
size += sizeof(structPointer->name);
size += sizeof(structPointer->fireNumber);
size += sizeof(structPointer->street);
size += sizeof(structPointer->city);
size += sizeof(structPointer->state);
structPointer++;
}
fprintf(stderr, "The size of the database is %d bytes.\n\n", size);
return size;
}
int addRecord(struct structInit *structPointer){
char entryName;
char entryFireNumber;
char entryStreet;
char entryCity;
char entryState;
recordNumber++;
struct structInit *theStruct;
theStruct = (struct structInit *) malloc ((recordNumber+1) * sizeof(struct structInit));
int i;
for (i=1;i<recordNumber;i++){
memcpy(theStruct->name,structPointer->name,51);
memcpy(theStruct->fireNumber,structPointer->fireNumber,11);
memcpy(theStruct->street,structPointer->street,26);
memcpy(theStruct->city,structPointer->city,26);
memcpy(theStruct->state,structPointer->state,26);
/*if(i==recordNumber){
theStruct++;}
else{
theStruct++;
structPointer++;}*/
theStruct++;
structPointer++;
}
theStruct++;
printf("Enter the Name of the New Record: \n");
scanf("%s",&entryName);
memcpy(theStruct->name,&entryName,51);
printf("Enter the Fire Number of the New Record: \n");
scanf("%s",&entryFireNumber);
memcpy(theStruct->fireNumber,&entryFireNumber,11);
printf("Enter the Street of the New Record: \n");
scanf("%s",&entryStreet);
memcpy(theStruct->street,&entryStreet,26);
printf("Enter the City of the New Record: \n");
scanf("%s",&entryCity);
memcpy(theStruct->city,&entryCity,26);
printf("Enter the State of the New Record: \n");
scanf("%s",&entryState);
memcpy(theStruct->state,&entryState,26);
structPointer=theStruct;
printf("Record has been added.\n\n");
return 0;
}
int deleteRecord(struct structInit *structPointer){
struct structInit *anotherStruct;
anotherStruct = (struct structInit *) malloc ((recordNumber+1) * sizeof(struct structInit));
int i;
for(i=0;i<5;i++){
memcpy(anotherStruct->name,structPointer->name,51);
memcpy(anotherStruct->fireNumber,structPointer->fireNumber,11);
memcpy(anotherStruct->street,structPointer->street,26);
memcpy(anotherStruct->city,structPointer->city,26);
memcpy(anotherStruct->state,structPointer->state,26);
structPointer++;
}
structPointer=anotherStruct;
recordNumber--;
printf("Record has been deleted.\n\n");
return 0;
}
At a glance:
The %s format specifier for scanf specifies a pointer to the first element of an array of char.
You're passing a pointer to one char.
It's just bad luck that the program doesn't crash completely.
You want
char entryName[51];
/* ... */
printf("Enter the Name of the New Record: \n");
scanf("%s", entryName);
and similar for the rest of the inputs.
Replace memcpy with strncpy - you shouldn't copy anything from outside the source object.
If you use realloc you can get rid of the entire copying loop when you expand your table.
Assigning to a parameter doesn't modify the value of the variable whose value you passed in.
This is as true for pointers as it is for everything else.
If you want to modify a variable, pass a pointer to it:
int addRecord(struct structInit **structPointer)
{
/* ... */
*structPointer = theStruct;
/* ... */
}
You don't need to do a memberwise memcpy between structs - in fact you don't need memcpy at all, since assignment works:
struct structInit a = /* ... */;
struct structInit b = /* ... */;
a = b; /* OK */
You need to make up your mind about whether to use global variables or parameters.
In particular, printAllRecords disregards the value of its parameter completely.
deleteRecord assumes that there are five records in your table.
You can also replace printSizeOfDatabase with this:
int printSizeOfDatabase() {
int size = recordNumber * sizeof(struct structInit);
fprintf(stderr, "The size of the database is %d bytes.\n\n", size);
return size;
}
You could use realloc in the add function.
A typedef would make the program a lot easier to read.
Please add this line to the end of mydata.h
typedef struct structPointer structInit;
Then before main() it becomes
structInit *sP;
structInit *sSP;
Then printAllRecords become :
int printAllRecords(structInit *structPointer)
Then turn ON your debugger and READ the messages.

Working With Stacks in C

After many hours of working on trying to debug this code myself I'm giving up an seeking help. I created a program that is designed to hold a "record", a struct, of my choice and show I know how to utilize/manipulate them in memory. In my code I made a car inventory that allows you to add, delete, and print out structs. My struct looks like the following:
struct car{
char *make;
char *model;
int year;
int mpg;
int mileage;
int cost;
};
I use a menu system of number that allows the user to choose what to do with the struct. Anyways here's where I'm having the issue:
I can add two struct and print them all out just fine. However if I add more than two structs than I run into a seg fault. I've already tried to run GDB on it and I get the following error:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a60a03 in _IO_vfprintf_internal (s=<optimized out>, format=<optimized out>,
ap=<optimized out>) at vfprintf.c:1661
1661 vfprintf.c: No such file or directory.
As for how I am taking in this data I have 6 scanf() functions that take in the 6 characteristics of the struct, store them to a temporary variable, malloc for each string, malloc a new car struct called newCar, and set each variable inside struct to its temporary variable. If it's the first car entry then carInventory is equal to newCar. When adding another car the same process occurs except a while loop moves along a new struct called newCarInventory and stores data from carInventory using memcpy(). Below is the actual code I'm using:
void addRecord(){
newCar = (struct car *) malloc(sizeof(struct car)); //Allocates memory for a car entry
int i;
int inventoryAmountTemp = inventoryAmount; //Grabs the global value and stores it to a local variable
//Local variables used to hold onto inputted values
char make[25];
char model[25];
int year;
int mpg;
int mileage;
int cost;
printf("\nMake: ");
scanf("%s", &make);
printf("\nModel: ");
scanf("%s", &model);
printf("\nYear: ");
scanf("%i", &year);
printf("\nMiles Per Gallon: ");
scanf("%i", &mpg);
printf("\nMileage: ");
scanf("%i", &mileage);
printf("\nCost: ");
scanf("%i", &cost);
newCar->make = (char *)malloc(strlen(make)+2); //Allocates memory for string
newCar->model = (char *)malloc(strlen(model)+2);
strcpy(newCar->make, make); //Coppies string into newCar
strcpy(newCar->model, model);
newCar->year = year;
newCar->mpg = mpg;
newCar->mileage = mileage;
newCar->cost = cost;
if(inventoryAmountTemp == 0){
carInventory = newCar;
}
else{
newCarInventory = (struct car *) malloc(inventoryAmountTemp * sizeof(struct car)); //Memory made for a new Car Inventory.
headPtr = carInventory;
headPtr2 = newCarInventory;
tailPtr = newCar;
i = 0;
while(i <= inventoryAmountTemp){
memcpy(&(newCarInventory->make), &(headPtr->make), sizeof(headPtr->make));
memcpy(&(newCarInventory->model), &(headPtr->model), sizeof(headPtr->model));
memcpy(&(newCarInventory->year), &(headPtr->year), sizeof(headPtr->year));
memcpy(&(newCarInventory->mpg), &(headPtr->mpg), sizeof(headPtr->mpg));
memcpy(&(newCarInventory->mileage), &(headPtr->mileage), sizeof(headPtr->mileage));
memcpy(&(newCarInventory->cost), &(headPtr->cost), sizeof(headPtr->cost));
i++;
if(i < inventoryAmountTemp && i != inventoryAmountTemp){
headPtr++;
newCarInventory++;
}
else if(i == inventoryAmountTemp){
headPtr = tailPtr;
newCarInventory++;
}
}
newCarInventory = headPtr2;
carInventory = newCarInventory;
}
inventoryAmountTemp += 1;
accessTemp += 1;
access = accessTemp;
inventoryAmount = inventoryAmountTemp;
}
I know the issue resides from this function because I had fprintf() statements (that I took out in this code) that would test each step. The ones that would fail were the ones inside the while loop the as the loop went around the second time, of the third car entry.
Any guidance would be really appreciated!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct car{
char *make;
char *model;
int year;
int mpg;
int mileage;
int cost;
};
int inventoryAmount=0;
struct car* CarList=0;
char *copyString(char *str){
char *buff=0;
if(str){
int l=strlen(str);
buff=malloc(l+1);
if(buff){
for(int i=0;i<=l;i++)
buff[i]=str[i];
}
}
return buff;
}
/*_______________________________________________________
*/
void propmt(const char *msg,const char*fmt,void *output){
printf("\n%s: ",msg);
scanf(fmt,output);
return;
}
/*_______________________________________________________
*/
void addRecord(void){
struct car *newCar ;
//Local variables used to hold onto inputted values
char make[25];
char model[25];
int year;
int mpg;
int mileage;
int cost;
if(CarList){
newCar= realloc(CarList,sizeof(struct car)*(inventoryAmount+1));
}else{
newCar= malloc(sizeof(struct car));
}
if(!newCar){
printf("Failed to allocate new car\n");
exit(1);
}
CarList=newCar;
newCar=&newCar[inventoryAmount];
inventoryAmount++;
propmt("Make","%s", &make);
propmt("Model","%s", &model);
propmt("Year","%i", &year);
propmt("Miles Per Gallon","%i", &mpg);
propmt("Mileage","%i", &mileage);
propmt("Cost","%i", &cost);
newCar->make=copyString( make); //Coppies string into newCar
newCar->model=copyString( model);
newCar->year = year;
newCar->mpg = mpg;
newCar->mileage = mileage;
newCar->cost = cost;
return;
}
/*_______________________________________________________
*/
void listCars(void){
int i;
if(!inventoryAmount){
printf("\nNo cars in inventory\n");
return;
}
for(i=0;i<inventoryAmount;i++){
printf("Make: %s\n" ,CarList[i].make);
printf("Model:%s\n" ,CarList[i].model);
printf("Year:%i\n" ,CarList[i].year);
printf("Miles Per Gallon:%i\n" ,CarList[i].mpg);
printf("Mileage:%i\n" ,CarList[i].mileage);
printf("Cost:%i\n" ,CarList[i].cost);
printf("======================================\n");
}
return;
}
void pause(void){
printf("Press a key to continue");
_getch();
printf("\n");
}
int main(void){
int q;
for(;;){
_clrscr();
printf(
"1 - List cars\n"
"2 - Add new car\n"
"0 - Exit program\n");
scanf("%i",&q);
switch(q){
case 1:
listCars();
pause();
break;
case 2:
addRecord();
pause();
break;
case 0:
return 0;
break;
}
}
return 0;
}
a good sample of using malloc and realloc
Try using backtrace in gdb to see where the segmentation fault originates

Displaying stack linked list in C Issue

This is the source code:
#include <stdio.h>
#include <stdlib.h>
struct Fraction{
int num;
int denom;
};
typedef struct Fraction Frac;
typedef Frac* FracPtr;
struct FracNode {
Frac fr;
struct FracNode* next;
};
typedef struct FracNode FrNode;
typedef FrNode* FrNodePtr;
struct FracStack {
int stkSize;
FrNodePtr frNodePtr;
};
typedef struct FracStack FrStk;
typedef FrStk* FrStkPtr;
FrNodePtr createFrNode(void);
FrStkPtr createFrNodeStk(void);
void pushFrNode(FrStkPtr*, FrNodePtr);
void printStack(FrStkPtr);
int main() {
FrNodePtr frNodeTmpPtr = 0;
FrNodePtr frNodeTmpPtr2 = 0;
FrStkPtr frStkPtr = 0;
frNodeTmpPtr = createFrNode();
frStkPtr = createFrNodeStk();
pushFrNode(&frStkPtr, frNodeTmpPtr);
printStack(frStkPtr);
frNodeTmpPtr2 = createFrNode();
pushFrNode(&frStkPtr, frNodeTmpPtr2);
printStack(frStkPtr); // Only the newly created fraction is printed. frNodeTmpPtr didn't get printed as part of the stack. Is there an issue with my print function or the push function?
free (frNodeTmpPtr);
free (frNodeTmpPtr2);
free (frStkPtr);
return 0;
}
//Print Stack
void printStack(FrStkPtr stkPtr) {
while (stkPtr->frNodePtr != 0) {
printf("Numerator: %d & Denominator: %d\n\n", stkPtr->frNodePtr->fr.num, stkPtr->frNodePtr->fr.denom);
stkPtr->frNodePtr = stkPtr->frNodePtr->next;
}
return;
}
//Push Node
void pushFrNode(FrStkPtr* stkPtr, FrNodePtr nPtr) {
if ((*stkPtr)->frNodePtr != 0) {
nPtr->next = ((*stkPtr))->frNodePtr;
}
(*stkPtr)->frNodePtr = nPtr;
(*stkPtr)->stkSize++;
return;
}
//Create Stack
FrStkPtr createFrNodeStk() {
FrStkPtr frStkPtr;
frStkPtr = (FrStkPtr) malloc (sizeof(FrStk));
frStkPtr->stkSize = 0;
frStkPtr->frNodePtr = 0;
return frStkPtr;
}
//Create Node
FrNodePtr createFrNode() {
FrNodePtr frNodePtr;
frNodePtr = (FrNodePtr) malloc (sizeof(FrNode));
frNodePtr->next = 0;
printf("Enter the numerator: ");
scanf("%d", &(frNodePtr->fr.num));
do {
printf("\nEnter a non-zero denominator: ");
scanf("%d", &(frNodePtr->fr.denom));
if (frNodePtr->fr.denom < 0) {
frNodePtr->fr.denom = -(frNodePtr->fr.denom);
frNodePtr->fr.num = -(frNodePtr->fr.num);
}
} while (frNodePtr->fr.denom == 0);
return frNodePtr;
}
In main, i created one fraction, pushed it into the created stack. Print it out. Then, i created another fraction, pushed it into the stack and print it. The problem is when i print out the stack it only prints out the last fraction created, not both fractions.
Sample run:
Enter the numerator: 1
Enter a non-zero denominator: 1
Numerator: 1 & Denominator: 1
Enter the numerator: 2
Enter a non-zero denominator: 2
Numerator: 2 & Denominator: 2 //I want this part to print out 1/1 and 2/2 not just 2/2
Change the printing function to the following:
//Print Stack
void printStack(FrStkPtr stkPtr) {
FrNodePtr pointer = stkPtr->frNodePtr;
while (pointer != 0) {
printf("Numerator: %d & Denominator: %d\n\n", pointer->fr.num, pointer->fr.denom);
pointer = pointer->next;
}
return;
}

Adding records with pointers to arrays

I have to create a program which adds records to a simple phone book. The code is below, but it doesn't work - function ends and then it stucks on declaring struct record x and doesn't want to display my added record - the program breaks down. When I put this part of code on the end of the function (but instead of "struct record x = array[0];" I put "struct record x = (*array)[0]") it works - record is printed. So I guess the problem is something about pointers, but I'm struggling and I really couldn't find out what's wrong. I remember that few weeks ago I created a program which was very similar but it was adding a new record to an array of integers, with fixed values and it was working well, so maybe there's something with structures that I don't know about. Thanks for any help!
I know the program isn't done yet and I know that I didn't make any action for temp_array == NULL, it'll be done after I found out what's going on.
struct record {
char f_name[SIZE];
char name[SIZE];
long int phone;
};
int add_record(struct record** array, int n)
{
struct record* temp_array = malloc((n+1) * sizeof(struct record));
if (temp_array == NULL)
{
free(temp_array);
return -1;
}
int i;
for (i=0; i < n; i++)
{
temp_array[i] = (*array)[i];
}
struct record new_record;
printf("\nAplly data.");
printf("\nFirst name: "); /*fgets(new_record.f_name, SIZE, stdin);*/ scanf("%s", &new_record.f_name);
printf("Surname: "); /*fgets(new_record.name, SIZE, stdin);*/ scanf("%s", &new_record.name);
printf("Phone number: "); scanf("%d", &new_record.phone);
temp_array[n] = new_record;
free (*array);
*array = temp_array;
//struct record x = (*array)[0];
//puts(x.f_name); puts(x.name); printf("%d", x.phone);
return 0;
}
main()
{
struct record* array; int n = 0;
int choice;
printf("\n1. Add record\n2. Delete record\n3. Find record\n0. Exit\n\nChoose action: ");
scanf("%d", &choice);
switch(choice) {
case 0: printf("\nKsiazka zostala zamknieta.\n"); return;
case 1: add_record(&array, n); n++; break;
case 2: return;
case 3: return;
default: printf("Wrong choice.\n\n"); return;
}
struct record x = array[0];
puts(x.f_name); puts(x.name); printf("%d", x.phone);
}
struct record* array=NULL;, and use %ld for long int – BLUEPIXY

Searching through a linked list.

So i've been trying to understand the difference between a regularly defined structure without using malloc linked list that does utilize malloc.
The issue that i'm having right now is trying to search through the structure (pi) to find each part number that has a cost greater than what was entered into the search. this is my entire program so far. I've added comments for each section.
I'm simply not sure how i am suppose to search through each structure to compare its price to the search price.
#include <stdio.h>
#include <stdlib.h>
struct Item {
int quantity;
float cost;
char partNum[10];
struct Item *next;
};
void printItem(struct Item* pi);
void enterItem(struct Item* pi);
char search [100];
void main(int argc, char* argv[])
{
struct Item *pi;
struct Item *head;
int done = 0;
int i,j;
char choice;
// ENTERING ITEM INTO THE STRUCTURE
head = NULL;
while (!done) {
printf("Enter another item? (y/n)");
choice = getchar();
if (choice == 'y' || choice == 'Y') {
pi = (struct Item *)malloc(sizeof(struct Item));
enterItem(pi);
pi->next = head;
head = pi;
} else {
done = 1;
}
}
// SEARCHING FOR ITEM BY PRICE
printf("Enter a price to find all items more expensive, or type 'exit':");
while (strcmp(search, "exit") !=0) {
gets(search);
for (j = 0; j<i ; i++) {
if (strcmp(pi[j].cost, search) ==0) {
printItem(pi);
pi = pi->next;
}
}
}
}
getchar();
getchar();
}
// FUNCTION FOR PRINTING STRUCTURE ITEM
void printItem(struct Item* pi) {
printf("Quantity: %d\n", pi->quantity);
printf("Cost: $%.2f\n", pi->cost);
printf("Part # %s\n", pi->partNum);
printf("\n\n");
}
// FUNCITON FOR ENTERING IN NEW ITEM
void enterItem(struct Item* pi) {
printf("Quantity? ");
scanf("%d", &pi->quantity);
printf("Cost? ");
scanf("%f", &pi->cost);
getchar(); //need to clear out the carriage return from typeing in the cost
printf("Part Number? ");
gets(pi->partNum);
}
What you were doing wrong is comparing string(search variable) with a float(cost variable) using strcmp. This won't give you the desired output.
Instead, lets use -1 to indicate the exit, since parsing string and converting it to float is off-topic. Start iterating from head up to finding NULL, and compare the prices of the each item.
float price;
struct Item *it = head;
printf("Enter a price to find all items more expensive, or type '-1' to exit:");
scanf("%f", price);
// check price for the 'exit' -- compare with -1
while (it != NULL) {
if (it->cost > price)
printItem(pi);
it = it->next;
}

Resources