2017-03-04 6 views
0

このコードをコンパイルするとエラーが発生します。 Zは、最低数のコインを使用することを目指して変更するために必要なコインの最終数です。私はint Z = 0を最上部の近くに定義しました。私はint zをもう一度追加して、print文でタイプをfに変更しようとしましたが、運が悪いです。CS50 PSET1 Greedy - 割り当てエラーとモジュロ(C)を使用

error: format specifies type 'int' but the argument has type '<dependent type>' [-Werror,-Wformat] 
greedy.c:77:16: error: use of undeclared identifier 'z' 
printf("%i\n", z); 

はここに私のコードです:

はここでエラーです。私は初心者ですので、どんな提案や修正も歓迎されます。

#include <stdio.h> 
#include <cs50.h> 
#include <math.h> 

int main(void) 
{ 
    //prompt user for amount of change owed 

    float f; 
    int num; //amount of change 

    do 
    { 
     printf("O hai! How much change is owed?:\n"); 
     f = get_float(); 
    } 
    while (f < 0); //verify input is positive 

    num = round(f * 100); //rounds float 

    //commence making change 

    do{ 

     int c, e, i, j; 

     int z = 0; //z = coins 

     if (num % 25 == 0) // verifies that num is divisible by 25 so only 25c coins necessary 
     { 
      z = (num/25) + z; // updates final number of 25c coins 
     } 
     else if (num % 25 > 0) 
     { 

      i = num/25; 
      j = num % 25; 

     } 

     else if ((num/25 < 0) || (num >=10 && num < 25)) //this means that f is less than 25 cents so has to be divided by 10 cent coins 
     { 

      num = c; 
      c = j + c; //add remainder of num to the number to start with 
     } 

     if (c % 10 == 0) // c is less than 25c but divisible by 10c pieces 
     { 
      z = (c/10) + z; //d is the final number of 10c coins 

     } 

     else if (c /10 < 1) //this means it's less than 10c 
     { 
      c = e; // Then c must be less than 10c 
     } 

     else if (e % 5 == 0) // divisible by 5c pieces 
     { 
      z = (e/5) + z; // g is the number of 5 cent pieces 

     } 

     else if (e % 5 < 0) 
     { 
     z = (e/1) + z; //h is the number of pennies   
     } 

    } 
    while (num > 0); //num is rounded float 

    printf("%i\n", z); 
} 

答えて

1

まず、インデントを使用してコードを適切にフォーマットすることをお勧めします。

次に、doループに関連付けられたブロック内でzが宣言され、printf("%i\n", z);が範囲外であるというエラーが発生します。 このエラーを取り除くには、printf()コール(たとえば、doループの直前)から表示される場所にzを宣言します。識別子の名前はC.

0

で大文字と小文字が区別されますので、int Z = 0を宣言すると、動作しないこと

はノートのように、すでにあなたが-whileループ内のzを宣言している、と述べたので、それだけでループ内で目に見えるのです。

ループが開始する前に宣言して、ループが終了した後で使用できるようにする必要があります。このように:

int z = 0; //z = coins 
do{ 
***YOUR CODE*** 
} while(); 
関連する問題