2017-04-19 17 views
0

ソースの概要:ユーザはオプションを選択します。1. Bugattiを作成します。 2.ブガッティを作成する。または3.プログラムを終了します。各オプションが完了したら、ユーザーはメニューに戻って別のオプションを選択する必要があります。パラメータが正常に正しく返されない

(注:それが作成されるまで、ユーザが2の場合には、それゆえif文が車を表示することはできません):へcreateCar()関数のユーザーの入力はmain()に戻されていない(特に

問題ケース2 - Bugattiを表示)、ユーザーの入力ではなく、大きな、奇妙な値を表示しています。私はそれがメモリに格納されていない値と関係があることを知っている/ main()に戻ってコールバック。

createCar()while文は、何らかの理由でパラメータを使用すると完全に無視されます。

可能であれば、個人的に解決しやすくするコードの回答をいただきたいと思います。

#include <stdio.h> 
#include <math.h> 
#define now 2017 

//Function headers 
void printMenu(void); 
void createCar(int *speed, int *year, int *bhp, int *age); 

int main(void) 
{ 
    //Variables 
    int userInput; 
    int topSpeed, yearMade, horsepower, carAge; 

    /***Loop program to return to menu after option is completed***/ 
    for(;;) 
    { 
     //Print menu and get input from user 
     printMenu(); 
     scanf("%i", &userInput), fflush(stdin); 

     //Validate input 
     while(userInput < 1 || userInput > 3) 
     { 
      printf("\nWrong input, please retry...\n"); 
      scanf("%i", &userInput), fflush(stdin); 
     } 

     //Make decisions after user's choice 
     switch(userInput) 
     { 
      //Option 1: Create car then return to menu 
      case 1: 
       createCar(&topSpeed, &yearMade, &horsepower, &carAge); 
       continue; 

      //Option 2: Read car details (if created) then return to menu 
      case 2: 
       if(topSpeed == NULL) 
       { 
        printf("\nYou must first create a car, please retry...\n\n"); 
        continue; 
       } 
       printf("\n----Bugatti Veyron----\n"); 
       printf("Top Speed: %i km/h\nYear made: %i\nAge: %i years old\nHorsepower: %i bhp\n", &topSpeed, &yearMade, &horsepower, &carAge); 
       printf("----------------------\n"); 
       continue; 

      //Option 3: Kill program 
      case 3: 
       exit(1);  
     } 
    } 
    return 0; 
} 

//Function: Display menu 
void printMenu(void) 
{ 
    printf("-----------------------------------------\n"); 
    printf("[Bob's Custom Car Creation Complex v1.0]\n"); 
    printf("1. Create Bugatti\n2. Display Bugatti\n3. Exit\n"); 
    printf("-----------------------------------------\n"); 
} 

//Function: Make a car + validate inputs 
void createCar(int *speed, int *year, int *bhp, int *age) 
{ 
    //Prompt user for top speed + validate input 
    printf("Enter the top speed of your Bugatti:"); 
    scanf("%i", &speed), fflush(stdin); 

    while(speed <=0) 
    { 
     printf("You cannot have a top speed of nothing silly :-D\nPlease retry...\n"); 
     scanf("%i", &speed), fflush(stdin); 
    } 
    //Prompt user for year mate + validate input 
    printf("What year is your Bugatti produced?:"); 
    scanf("%i", &year), fflush(stdin); 

    while(year <=0) 
    { 
     printf("You cannot own a Bugatti that is from the future laddy!!\nPlease retry...\n"); 
     scanf("%i", &year), fflush(stdin); 
    } 
    //Calculate age of car 
    age = now - year; 

    //Prompt user for horsepower + validate input 
    printf("How much horsepower does your Bugatti have?:"); 
    scanf("%i", &bhp), fflush(stdin); 

    while(bhp <=0) 
    { 
     printf("A Bugatti with no engine... doesn't sound too promising :-O\nPlease retry...\n"); 
     scanf("%i", &bhp), fflush(stdin); 
    } 
} 
+5

コンパイラの警告を有効にしてください。彼らは基本的にあなたの質問に答えます。また、['fflush(stdin)'は未定義の動作です](http://stackoverflow.com/q/2979209/3425536)。 – emlai

+3

'int * speed'与えられた'&speed'は何ですか?そして 'fflush(stdin);'? –

+3

Andrew Henleが指摘しているように、ポインタに問題があります。 'createCar'の' scanf'にポインタを渡すのではなく、ポインタをポインタに渡しています。あなたの 'while'は無視されていますが、ポインタの値を値の代わりに0(つまり' * year'を使います)と比較しています。また、 'case 2'でも 'topSpeed == NULL'ポインタではありません。ヒント:ポインタを読み上げます。しかしこれは素晴らしい運動です。 – jiveturkey

答えて

0

年齢と年のポインタを逆参照して値を取得/設定する必要があります。

あなたが speedyearbhpはすでにintへのポインタであるため、 createVarscanf()で '&' を削除する必要が
//Calculate age of car 
*age = now - *year; 

コンパイラの警告を有効にして解決すれば、トラブルを避けることができます。

関連する問題