2016-04-07 78 views
0

因此,该程序应创建一个包含年份信息,安全等级和模型的汽车链表。但是,当输出安全等级时,例如如果用户输入“5”,则程序将其输出为非用户输入的非常大的数字。链接列表浮动不能正确输出

举例:如果用户输入

Enter Year for Car: 1992 

Enter Rating for Car (0.00 - 100.00): 25.5 

Enter Model for Car: Chevy 

程序输出是...

The Cars in the List Are As Follows: 
Year:1992 Rating:79960111893652368676401906121179136.00 Model:Chevy 

当它应该是....

The Cars in the List Are As Follows: 
Year:1992 Rating:25.50 Model:Chevy 

我在做什么错误?这里是我的完整代码...

#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)); 
    } 

} 
+0

为什么不检查'scanf'的返回值? –

+0

顺便说一句,你应该阅读:http://stackoverflow.com/help/mcve –

回答

2

您没有为汽车模型字符串分配任何空间。你是scanf("%s", &infoC);,但infoC是一个单一的char,没有字符串的空间。这是重写一些东西,可能是用事情弄糟了。然后当你制作你的汽车时,你会去strcpy,它会复制,直到达到NULL字节。 ,,等等,,,等。最好将其定义为char infoC[MAXSIZE]以匹配struct car中的内容。

+0

是的工作,感谢您的帮助! – bobblehead808

+0

@ bobblehead808好的交易..也请记住:http://stackoverflow.com/questions/5406935/reading-a-string-with-scanf – yano