-1

我已经在这个网站上查看了这两个错误,并尝试更改我的代码以适应建议的更改,但它们不适用于我的代码,所以这是我的代码,我希望你们能帮助我。它不完整,因为我只完成了一半的任务,但我看不到到目前为止我做了什么,因为它不会正确构建。谢谢!

#include <stdio.h>


//int CheckMoney(double *payment, double item_cost); //compares the amount the user has deposited to the price of item selected. It returns 1 if the amount is at least enough to cover the cost, 0 if there is not enough.
//void GetMoney(double *payment, double item_cost, char selection); //calls CoinMenu function to collect money from the user and CheckMoney function to keep comparing the deposited amount to the item cost. 
//void GetChange(double *payment, double item_cost, double *change); //calculates the amount of change to be returned
void CoinMenu(double *payment);
void Quit(char *again);
void GetCost(char selection, double *item_cost);
void Menu(char *selection);

// Displays the list of snack items and prompts for the user’s choice
void Menu(char *selection)
{
printf("Welcome to the AAA vending machine, where your wishes could come true for less than $2");
printf("/nWhich one of our delicious snacks would you like to sink your teeth into?");
printf("/nP – Potato Chips      $1.25");
printf("/nS - Snickers Bar      $1.35");
printf("/nT – Pop Tart          $0.95");
printf("/nC – Cookies           $1.50");
printf("/nB – Brownie           $1.75");
printf("/nN – Nuts              $1.40");
printf("Enter your delicious selection here: ",*selection);
scanf(" %c", &*selection);

//determine cost of selection

GetCost(*selection, 0);
}

//sets the cost of the purchase based on value in selection
void GetCost(char selection, double *item_cost)
{

if(selection=='P'||'p')
{
    *item_cost=1.25;
}
else if(selection=='S'||'s')
{
    *item_cost=1.35;
}
else if(selection=='T'||'t')
{
    *item_cost=0.95;
}
else if(selection=='C'||'c')
{
    *item_cost=1.50;
}
else if(selection=='B'||'b')
{
    *item_cost=1.75;
}
else if(selection=='N'||'n')
{
    *item_cost=1.40;
}
else
{
    printf("That is not a valid selection, have a nice day!");
    return;
}
}

//displays menu of coins and gets user input of the coins deposited
void CoinMenu(double *payment)
{
printf("Please deposit your money by the following numbers:");
printf("/n1 - $5.00");
printf("/n2 - $2.00");
printf("/n3 - $1.00");
printf("/n4 - $0.25");
printf("/n5 - $0.10");
printf("/n6 - $0.05");
printf("/n7 - $0.01");
printf("/nAmount deposited: ");
scanf(" %c", &*payment);
}




void Quit(char *again)
{
printf("Would you like to buy another snack?");
printf("/nEnter Y for yes or N for no: ", *again);
if(*again=='N'||'n')
{
    return;
}
else if(*again=='Y'||'y')
{
    void Menu(char *selection);
}
}
4

1 回答 1

0

对于您的问题中提到的错误-您没有main()功能,并且在编写之前没有完整的C程序。您的链接器正在寻找它,并在找不到它时给您一个错误。

这里还有很多其他错误,包括:

  1. printf("/n1 - $5.00");- 它是\n,不是/n,并且不清楚为什么你将换行符放在行的开头,而不是结尾:printf("/nN – Nuts $1.40"); printf("Enter your delicious selection here: ",*selection);例如,这会给你一些看起来很奇怪的文本。

  2. 说到这,printf("Enter your delicious selection here: ",*selection);-您提供 achar作为参数printf(),但根据您的格式字符串,它不需要任何参数。应该是printf("Enter your delicious selection here: ");由于您没有用换行符终止它,因此您还应该fflush(stdout);在调用scanf().

  3. scanf(" %c", &*selection);- 为什么取消引用一个指针只是为了再次获取它的地址?有一些很好的理由不这样做,包括它相当于获取非法临时值的地址这一事实。你已经有一个指针,所以使用它 - 应该是scanf(" %c", selection);

  4. if(selection=='P'||'p')- 二元逻辑运算符在 C 中不像那样工作,应该是if ( selection == 'P' || selection == 'p' ).

  5. scanf(" %c", &*payment);- 与上面第 3 点相同的问题,加上这里payment是 a double *,你告诉scanf()要阅读 a char。应该scanf("%lf", payment);

  6. void Menu(char *selection);在底部 - 正如对您的主要问题的评论中指出的那样,这是一个函数声明,而不是函数调用,并且在这里实际上什么都不做。应该是Menu(again),虽然实际上它应该只返回 true 或 false,并且调用者应该决定是否Menu()再次调用。

这些错误中有几个在您的代码中的不同位置重复出现,我没有列出每个示例。这种指针的传递也是非常奇怪的设计。您的所有函数都被声明为返回void- 如果您将它们更改为返回一个值并使用一些局部变量,那么您将能够完全避免传递任何指针。

于 2013-10-27T03:38:31.053 回答