2016-04-07 25 views
0

Bu programın, yılın bilgileri, Güvenlik Derecelendirmesi ve Model bilgileri olan bir araba için bağlantılı bir liste oluşturması gerekiyor. Ancak, güvenlik notu verilirken, kullanıcının "5" girip girmediğini söyleyin, program, kullanıcının girdiği gibi olmayan çok büyük bir sayı olarak çıkarır.Bağlantılı Liste Doğru Çıktılamamış Kayan Noktalı

Örnek: Ben ne yapıyorum

Enter Year for Car: 1992 

Enter Rating for Car (0.00 - 100.00): 25.5 

Enter Model for Car: Chevy 

program çıkarılır ...

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

olması gerektiği ....

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

kullanıcı girerse yanlış? İşte tam kodum ...

#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

Neden 'scanf''den dönüş değerlerini kontrol etmiyorsunuz? –

+0

BTW, şunu okumalısınız: http://stackoverflow.com/help/mcve –

cevap

2

Araba modeli dizesi için herhangi bir yer ayırmıyorsunuz. scanf("%s", &infoC);, ancak infoC tek bir char, bir dizeye yer yok. Bu bir şeyin üzerine yazıyor ve muhtemelen bir şeylerle uğraşıyor. Ardından arabanızı NULL10 baytına ulaşana kadar kopyalayacağınız zaman strcpy gidin. ,, vs, vs, UB. struct car ürününe sahip olmanız için char infoC[MAXSIZE] olarak tanımlayabilirsiniz.

+0

Evet, işe yaradı, yardım için teşekkürler! – bobblehead808

+0

@ bobblehead808 İyi anlaşma .. bunu da unutmayın: http://stackoverflow.com/questions/5406935/reading-a-string-with-scanf – yano

İlgili konular