So this program is supposed to create a linked list for a car with the information of the year, Safety Rating, and Model. However when outputting the safety rating, say if the user enters "5", the program outputs it as some very large number that isn't what the user entered.
Example: If the user enters
Enter Year for Car: 1992
Enter Rating for Car (0.00 - 100.00): 25.5
Enter Model for Car: Chevy
The program output is...
The Cars in the List Are As Follows:
Year:1992 Rating:79960111893652368676401906121179136.00 Model:Chevy
When it should be....
The Cars in the List Are As Follows:
Year:1992 Rating:25.50 Model:Chevy
What am I doing wrong? Here is my full code...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 400
struct car {
int year;
float safeRating;
char model[MAXSIZE];
struct car *next;
};
typedef struct car Car;
typedef struct car *CarPtr;
void menu();
void printList(CarPtr);
CarPtr makeCar(int, float, char *);
CarPtr removeCar(CarPtr, int);
CarPtr addCar(CarPtr sPtr, int infoA, float infoB, char *infoC);
void viewCar(CarPtr sPtr, int infoA, float infoB, char *infoC);
int main()
{
CarPtr startPtr;
int infoA, choice;
float infoB;
char infoC;
startPtr = NULL;
menu();
scanf("%d", &choice);
while (choice != 5){
switch (choice){
case 1: printf("\nEnter Year for Car: ");
scanf("%d", &infoA);
printf("\nEnter Rating for Car (0.00 - 100.00): ");
scanf("%f", &infoB);
printf("\nEnter Model for Car: ");
scanf("%s", &infoC);
startPtr = addCar(startPtr, infoA, infoB, &infoC);
printList(startPtr);
printf("\n");
break;
case 2: printf("\nEnter Car for deletion : ");
scanf("%d", &infoA);
startPtr = removeCar(startPtr, infoA);
printList(startPtr);
printf("\n");
break;
case 3: printf("\nEnter Car Number to View : ");
scanf("%d", &infoA);
viewCar(startPtr, infoA, infoB, &infoC);
printf("\n");
break;
case 4: printList(startPtr);
printf("\n");
break;
default: printf ("Invalid Option... Please Try Again \n");
break;
}
menu();
scanf("%d", &choice);
}
return 0;
}
void menu()
{
printf ("\t1: Insert Car into Ordered List\n");
printf ("\t2: Remove Car from List\n");
printf ("\t3: View Car from List\n");
printf ("\t4: Printing the List\n");
printf ("\t5: Exit\n");
printf ("\tEnter Choice: ");
}
CarPtr makeCar(int infoA, float infoB, char *infoC)
{
CarPtr np = (CarPtr) malloc(sizeof(Car));
np->year = infoA;
np->safeRating = infoB;
strcpy(np->model, infoC);
np->next = NULL;
return np;
}
void printList(CarPtr sPtr)
{
if(sPtr == NULL){
printf ("\nThere are no Cars to be Printed\n");
}
else {
printf("The Cars in the List Are As Follows Sorted by Year: \n");
while (sPtr != NULL) {
printf("Year:%d Rating:%.2f Model:%s\n", sPtr->year, sPtr->safeRating, sPtr->model);
sPtr = sPtr->next;
}
}
}
CarPtr addCar(CarPtr sPtr, int infoA, float infoB, char *infoC)
{
CarPtr newPtr, currPtr, prevPtr;
newPtr = makeCar(infoA, infoB, infoC);
prevPtr = NULL;
currPtr = sPtr;
while (currPtr != NULL && infoA > currPtr->year) {
prevPtr = currPtr;
currPtr = currPtr->next;
}
if (prevPtr == NULL) { // inserting at the start of the list
newPtr->next = sPtr;
sPtr = newPtr; // start of list has now changed
}
else {
newPtr->next = currPtr;
prevPtr->next = newPtr;
}
return sPtr;
}
CarPtr removeCar(CarPtr sPtr, int infoA)
{
CarPtr previousPtr, currentPtr, tempPtr;
previousPtr = NULL;
currentPtr = sPtr;
if(sPtr == NULL){
printf ("\nThe List is empty... No cars to be Removed\n");
return sPtr;
}
while (currentPtr != NULL && currentPtr->year != infoA) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if(currentPtr == NULL){
printf("\nCar ( %d ) was not found \n", infoA);
}
else if (previousPtr == NULL){ // if node to be deleted is the first node
tempPtr = sPtr;
sPtr = sPtr->next; // start of list has been changed
printf("\nCar ( %d ) was deleted \n", tempPtr->year);
free(tempPtr);
}
else{
tempPtr = currentPtr;
previousPtr->next = currentPtr->next;
printf("\nCar ( %d ) was deleted \n", tempPtr->year);
free(tempPtr);
}
return sPtr;
}
void viewCar(CarPtr sPtr, int infoA, float infoB, char *infoC)
{
CarPtr previousPtr, currentPtr;
previousPtr = NULL;
currentPtr = sPtr;
int position = 0;
if(sPtr == NULL){
printf ("\nThe List is empty... No cars to View\n");
return;
}
while (currentPtr != NULL && currentPtr->year != infoA) {
previousPtr = currentPtr;
currentPtr = currentPtr->next;
position++;
}
if(currentPtr == NULL){
printf("\nCar ( %d ) was not found \n", infoA);
}
else{
printf("\nCar ( %d ) Rating: %.2f Model: %s was found at position : %d\n", currentPtr->year, currentPtr->safeRating, currentPtr->model, (position + 1));
}
}