2016-04-07 13 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バイトに当たるまでコピーされます。 ,,など、UB。 struct carと一致するようにchar infoC[MAXSIZE]と定義することをお勧めします。

+0

はい、うまくいった、助けてくれてありがとう! – bobblehead808

+0

@ bobblehead808良いこと..これも覚えておいてください:http://stackoverflow.com/questions/5406935/reading-a-string-with-scanf – yano

関連する問題