错误LNK2019:未解析的外部符号,致命错误LNK1120:1个未解析的外部

时间:2013-10-27 03:18:51

标签: c lnk2019

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

#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);
}
}

1 个答案:

答案 0 :(得分:0)

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

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

  1. printf("/n1 - $5.00"); - 它是\n,而不是/n,并且不清楚为什么要将换行符放在行的开头而不是结尾处:{例如,{1}}会给你一些奇怪的文字。

  2. 在谈到哪个printf("/nN – Nuts $1.40"); printf("Enter your delicious selection here: ",*selection);时,你提供printf("Enter your delicious selection here: ",*selection);作为char的参数,但是根据你的格式字符串,它不期望任何参数。应为printf()由于您没有使用换行符将其终止,因此您还应在致电printf("Enter your delicious selection here: ");之前添加fflush(stdout);

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

  4. scanf(" %c", selection); - 二进制逻辑运算符与C中的运算符不同,应为if(selection=='P'||'p')

  5. if ( selection == 'P' || selection == 'p' ) - 与上述第3点相同的问题,加上此处scanf(" %c", &*payment);payment,您告诉double *阅读scanf() 1}}。应为char

  6. 底部的
  7. scanf("%lf", payment); - 正如您对主要问题的评论所指出的,这是一个函数声明,而不是函数调用,并且在这里实际上没有做任何事情。应该是void Menu(char *selection);,尽管它应该只返回true或false,并且调用者应该决定是否再次调用Menu(again)

  8. 在整个代码中的各个地方重复了其中一些错误,我没有列出每个示例。传递指针也是非常奇怪的设计。所有函数都声明为返回Menu() - 如果你更改它们以返回一个值并使用一些局部变量,那么你就可以避免传递任何指针。