开发者

I'm getting fatal errors... can anyone help me edit my program?

开发者 https://www.devze.com 2023-01-01 12:42 出处:网络
The errors I am getting are: Error1error LNK2019: unresolved external symbol \"double __cdecl getDollarAmt(void)\" (?getDollarAmt@@YANXZ) referenced in function _mainhid.obj

The errors I am getting are:

Error 1 error LNK2019: unresolved external symbol "double __cdecl getDollarAmt(void)" (? getDollarAmt@@YANXZ) referenced in function _main hid.obj Error 2 fatal error LNK1120: 1 unresolved externals

this is my program:

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

}


You declare the function as:

double getDollarAmt();

and then define it as:

double getDollarAmt (float amtExchanged)

and then call it as:

getDollarAmt ();

C++ allows function overloading, so this is not a compilation error. However, at link time, the linker cannot find the version that does not take a parameter.

Also, in the function the line:

amtExchanged = amtExchanged0;

will not change amtExchanged in the calling code. If you wanted to do that you should have passed it by reference. However, I don't think you do want it to do that, because you also return the value.

And one last point, unless you have specific reasons to do otherwise, it's best to use doubles rather than floats - they are more precise and may even be faster!


You've declared getDollarAmt as double getDollarAmt(); but defined it as double getDollarAmt(float amtExchanged);


Neil and Joe already told you the problem, but I just overflew your program and found this line:

if(selection=='A' || selection=='B' || selection=='C' || ....)

Just in case you don't know: if you want to save yourself some time through less typing, you can use ASCII Code instead of the characters. So if you just want to make sure, if the user input ist between 'A' and 'H', the following if would also do the job:

if(selection >= 65 && selection <= 72)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号