我的代码中的Objective C错误

时间:2012-12-13 03:02:38

标签: objective-c xcode

我在Xcode的终端应用程序中创建了这个,但是如何解决错误。 对不起,我是初学者。

有错误
void addToTotal (float cost, int quantity)

有人能帮助我使用我的代码吗?

//
//  main.m
//  ShoppingList


#import <Foundation/Foundation.h>
#include <stdio.h>  //? not sure if this is correct 

//Gobal variables visable from any function

//------VARIABLES
int totalItems = 0;
float totalCost = 0.0;
float salesTax = 0.0925;

//decalre the functions going to be used
// we don't need to declare main() because it's built-in


//------FUNCTIONS
void addToTotal (float cost, int quantity);
float costWithSalesTax (float price);

//------MAIN PROGRAM


int main(int argc, const char * argv[])
{

   @autoreleasepool {

      float budget = 10000.00;

      // make a new line
      printf("\n");

      //set the price for each item
      float laptopPrice = 1799.00;
      float monitorPrice = 499.80;
      float phonePrice = 199.00;

      addToTotal(laptopPrice, 2);
      addToTotal(monitorPrice, 1);
      addToTotal(phonePrice, 4);

      //display a line then the final total

      printf("----------------------\n");
      printf("TOTAL for %i items: $%5.2f\n\n", totalItems, totalCost);

      if(totalCost < budget)
      {
         printf("You came in under your budget!");
      }
      else
      {
         printf("Your're over your budget. Time to talk to finance.\n\n");
      }
   }

// There's an error in here asking me to put ; after this method declaration.
   void addToTotal (float cost, int quantity)
   {

      printf(" Adding %i items of the cost $%5.2f\n", quantity, cost);

      // find the cost for this item by multiple costs by quantity.
      // and get the real costs by applying sales tax.
      float calculatedCost = cost * quantity;
      float realCost = costWithSalesTax(calculatedCost);

      // and this amount to the total, and increase the total number
      // of items purchased

      totalCost = totalCost + realCost;
      totalItems = totalItems + quantity;

      printf("Subtotal for %i items: $%5.2f\n", totalItems, totalCost);
   }
   // There's an error in here asking me to put ; after this method declaration.
   float costWithSalesTax (float price)
   {
      // remember sales tax is a global variable

      float taxAmount = price * salesTax;
      float subTotal = price + taxAmount;

      return subTotal;

   }

   }
    return 0;  // also this has the error "expected identifier or (" error
}

1 个答案:

答案 0 :(得分:1)

您正尝试在addToTotal()的实施中内实施costWithSalesTax()main()个功能。将它们移到收盘}下方。

您的代码末尾还有一个额外的}。具体来说,是return 0;之前的那个。删除它。