我得到的错误是:
错误1错误LNK2019:函数_main hid.obj中引用的未解析的外部符号“double __cdecl getDollarAmt(void)”(?getDollarAmt @@ YANXZ) 错误2致命错误LNK1120:1个未解析的外部
这是我的计划:
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
double getDollarAmt();
void displayCurrencies();
char getCurrencySelection (float amtExchanged);
bool isSelectionValid(char selection);
double calcExchangeAmt (float amtExchanged, char selection);
void displayResults(double newAmount, float amtExchanged, char selection, char yesNo);
const double russianRubles = 31.168;
const double northKoreanWon = .385;
const double chineseYuan = 6.832;
const double canadianDollar = 1.1137;
const double cubanPeso = 1.0;
const double ethiopianBirr = 9.09;
const double egyptianPound = 5.6275;
const double tunisianDinar = 1.3585;
const double thaiBaht = 34.4;
/******
I changed the variables to global variables so
you don't have to worry about accidentally setting them
to 0 or assigning over a value that you need
********/
float amtEchanged = 0.0;
char selection;
char yesNo;
double newAmount;
int main()
{
float amtExchanged = 0.0;
selection = 'a';
yesNo = 'y';
newAmount = 0.0;
getDollarAmt ();
displayCurrencies();
getCurrencySelection (amtExchanged);
isSelectionValid(selection);/**** you only need to use the selection variable ****/
calcExchangeAmt (amtExchanged, selection);
displayResults(newAmount, amtExchanged, selection, yesNo);
return 0;
}
double getDollarAmt (float amtExchanged)
// promt user for eachange amount and return it to main
{
float amtExchanged0;//created temporary variable to set amtExchanged to
cout<< "Please enter the total dollar amount to exchange: ";
cin>> amtExchanged0;
amtExchanged = amtExchanged0;//setting amtExchanged to the right value
return amtExchanged;
}
void displayCurrencies()
// display list of currencies
{
cout<<"A Russian Ruble"<<endl
<<"B North Korean Won"<<endl
<<"C Chinese Yuan"<<endl
<<"D Cuban Peso"<<endl
<<"E Ethiopian Birr"<<endl
<<"F Thai Baht"<<endl
<<"G Canadian Dollars"<<endl
<<"H Tunisian Dinar"<<endl
<<"I Egyptian Pound"<<endl;
}
char getCurrencySelection (float amtExchanged)
// make a selection and return to main
{
char selection0;//again, created a temporary variable for selection
cout<<"Please enter your selection: ";
cin>>selection0;
selection = selection0;//setting the temporary variable to the actual variable you use
/*****
we are now going to see if isSelectionValid returns false.
if it returns false, that means that their selection was not
character A-H. if it is false we keep calling getCurrencySelection
*****/
if(isSelectionValid(selection)==false)
{
cout<<"Sorry, the selection you chose is invalid."<<endl;
getCurrencySelection(amtExchanged);
}
return selection;
}
bool isSelectionValid(char selection)
// this function is supposed to be called from getCurrencySelection, the selection
// must be sent to isSelectionValid to determine if its valid
// if selection is valid send it back to getCurrencySelection
// if it is false then it is returned to getCurrencySelection and prompted to
// make another selection until the selection is valid, then it is returned to main.
{
/****
i'm not sure if this is what you mean,
all i am doing is making sure
that their selection is A-H
*****/
if(selection=='A' || selection=='B' || selection=='C' || selection=='D' || selection=='E' || selection=='F' || selection=='G' || selection=='H' || selection=='I')
return true;
else
return false;
}
double calcExchangeAmt (float amtExchanged,char selection)
// function calculates the amount of money to be exchanged
{
switch (toupper(selection))
{
case 'A': newAmount =(russianRubles) * (amtExchanged);
break;
case 'B': newAmount = (northKoreanWon) * (amtExchanged);
break;
case 'C': newAmount = (chineseYuan) * (amtExchanged);
break;
case 'D': newAmount = (canadianDollar) * (amtExchanged);
break;
case 'E': newAmount = (cubanPeso) * (amtExchanged);
break;
case 'F': newAmount = (ethiopianBirr) * (amtExchanged);
break;
case 'G': newAmount = (egyptianPound) * (amtExchanged);
break;
case 'H': newAmount = (tunisianDinar) * (amtExchanged);
break;
case 'I': newAmount = (thaiBaht) * (amtExchanged);
break;
}
return newAmount;
}
void displayResults(double newAmount, float amtExchanged, char selection, char yesNo)
// displays results and asked to repeat. IF they want to repeat it clears the screen and starts over.
{
switch(toupper(selection))
{
case 'A': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" Russian Rubles."<<endl<<endl;
break;
case 'B': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" North Korean Won."<<endl<<endl;
break;
case 'C': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" Chinese Yuan."<<endl<<endl;
break;
case 'D': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" Cuban Pesos."<<endl<<endl;
break;
case 'E': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" Ethiopian Birr."<<endl<<endl;
break;
case 'F': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" Thai Baht."<<endl<<endl;
break;
case 'G': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" Canadian Dollars."<<endl<<endl;
break;
case 'H': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" Tunisian Dinar."<<endl<<endl;
break;
case 'I': cout<<"$"<<amtExchanged<<" is "<<newAmount<<" Egyptian Pound."<<endl<<endl;
break;
}
cout<<"Do you wish to continue? (Y for Yes / N for No)";
cin>>yesNo;
if(yesNo=='y' || yesNo=='Y')
{
getDollarAmt();
}
else
{
system("cls");
}
}
答案 0 :(得分:6)
您将该函数声明为:
double getDollarAmt();
然后将其定义为:
double getDollarAmt (float amtExchanged)
然后将其命名为:
getDollarAmt ();
C ++允许函数重载,因此这不是编译错误。但是,在链接时,链接器找不到不带参数的版本。
另外,在函数行中:
amtExchanged = amtExchanged0;
不会在调用代码中更改amtExchanged。如果你想这样做,你应该通过引用传递它。但是,我认为你不希望它这样做,因为你也会返回值。
最后一点,除非你有特别的理由不这样做,否则最好使用双打而不是花车 - 它们更精确,甚至可能更快!
答案 1 :(得分:1)
您已将getDollarAmt
声明为double getDollarAmt();
,但将其定义为double getDollarAmt(float amtExchanged);
答案 2 :(得分:0)
Neil和Joe已经告诉了你这个问题,但我刚刚通过你的程序,发现了这一行:
if(selection=='A' || selection=='B' || selection=='C' || ....)
万一你不知道:如果你想通过减少输入来节省一些时间,你可以使用ASCII代码代替字符。因此,如果您只是想确定,如果用户输入介于'A'和'H'之间,则以下if也可以完成这项工作:
if(selection >= 65 && selection <= 72)