I am having issues getting this function to work. This is a currency conversion program that gives the user choices of input of three other currencies other than USD and converts it. It is supposed to be a value returning function, not a void function. The program compiles, runs fine, but only gives me the number "1" as the output. Any help in the write direction is appreciated. Thank you.

using namespace std;

double calculate (double dollars, const double toEuros, const double toYen, const double toRupees);

const double toEuros = .826925;     // conversion rates
const double toYens = 118.511;
const double toRupees = 44.1650;
const double euros = 1.20185;     
const double yens = .00840795;
const double rupees = 0226501;

string symbol1 = "USD", symbol2 = "USD";

int main()
{


    double dollars, result;

    cout << "This is a conversion program for U.S. dollars into euros,yens, and rupees or vice versa" << endl;

    // get input    
    cout << "Please enter the three-letter symbol of the original currency: ";
    cin >> symbol1;
    cout << endl;

    cout << "Please enter the three-letter symbol of the resulting currency: ";
    cin >> symbol2;
    cout << endl;

    cout << "Please enter the amount to be converted: ";
    cin >> dollars;

     cout << calculate << endl;


    return 0;



}
double calculate (double dollars, const double toEuros, const double toYen, const double toRupees)
    {
    double result;

        if (symbol1 == "USD" || symbol1 == "usd") 
    {
       if (symbol2 == "EUR" || symbol2 == "eur")
       {
       result = dollars * toEuros;
       cout << dollars << " USD"  << setw(20) << result << " EUR"<< endl;
       }
       else if (symbol2 == "JPY" || symbol2 == "jpy")
       {
       result = dollars * toYens;
       cout << dollars << " USD"  << setw(20) << result << " JPY"<< endl;
       }
       else if (symbol2 == "INR" || symbol2 == "inr")
       {
       result = dollars * toRupees;
       cout << dollars << " USD"  << setw(20) << result << " INR"<< endl;
       }
    }
    else if (symbol1 == "EUR" || symbol1 == "eur")
    {
    result = dollars * euros;
        cout << dollars << " EUR"  << setw(20) << result << " USD"<< endl;  
    }
    else if (symbol1 == "JPY" || symbol1 == "jpy")
    {
        result = dollars * yens;
        cout << dollars << " JPY"  << setw(20) << result << " USD"<< endl;
    }
    else if (symbol1 == "INR" || symbol1 == "inr")
    {
    result = dollars * rupees;
    cout << dollars << " INR"  << setw(20) << result << " USD"<< endl;
    }
    else
    cout << endl << "You did not enter a valid currency symbol";
}

1) Try adding a return statement in double calculate(.....).
2) If you don't have an list of header files in your program, you should have.
3) When you call the function called calculate from the function called main, you should a list of arguments enclosed in parenthesis after the name of the function.
4) If this truly compiled, get a better compiler.

Hi ,
You didn't invoked that function(calculate) properly .

I have the header files. I must have missed them when I copied the code. I added a return statement and it didn't change anything on the way it runs. I added the arguments when I called the function calculate and still not working. I don't know, maybe I am just not cut out for computer programming, I sit and stare at my text book and it all seems to go right over my head, even though I have read every word on every page through the chapter this problem is associated with. I don't know what else to do with this program to get it to work.

can you post the updated program ?
Also specify exactly what problem you are facing .

Here is the latest code I have gotten down. I have gotten past most of my problems, but the assignment is as follows from my instructor. The problem I am coming across now is that I have the functions written, and seem to execute, at least the first one "void convert()" but it is not outputting the data for the conversion. The program when run, asks for the first currency, second currency, then the amount to be converted, then the program terminates without outputting any of the calculations in the function "double Calculate". I have posted the new code, as I have it right now, for this assignment. I am not looking for a handout, just a point in the right direction with this problem.

Use one function, named calculate, for all of the conversions. This function will take two parameters: the first will be the amount to be converted; and the second will be the conversion rate. You will use the function in the nested selection structure that is used to determine the type of conversion. Note that the function will be called each time a conditional statement evaluate to true with the corresponding rate. You will also write a function named convert that includes the rest of the code in main. This function has no return type; therefore, use the reserved word void as the return type. So, the main function will now consist of one function call (except for a possible introductory message) and a return statement. The function call will have the following syntax: convert();. Include function prototypes at the beginning of your program.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

double Calculate (double dollars, double rate);
void convert();

string symbol1 = "USD", symbol2 = "USD";

int main()
{
    convert();

    return 0;
}
void convert()
    {

    ofstream outFile;
    double dollars, result, rate;
    {
    cout << "This is a conversion program for U.S. dollars into euros,yens, and rupees or vice versa" << endl;

    // get input    
    cout << "Please enter the three-letter symbol of the original currency: ";
    cin >> symbol1;
    cout << endl;

    cout << "Please enter the three-letter symbol of the resulting currency: ";
    cin >> symbol2;
    cout << endl;

    cout << "Please enter the amount to be converted: ";
    cin >> dollars;

     Calculate(dollars, rate);

    // set up output
    outFile.open("conversion.out");
    outFile << fixed << showpoint;
    outFile << setprecision(2); 
    outFile << "         Conversion Results " << endl << endl;


       cout << endl;
    outFile.close();
     }

    }




double Calculate (double dollars, double rate)

    {
    double result = 0;
    const double toEuros = .826925;     // conversion rates
const double toYens = 118.511;
const double toRupees = 44.1650;
const double euros = 1.20185;     
const double yens = .00840795;
const double rupees = 0226501;
         // calculate conversion based on input choices 
    if (symbol1 == "USD" || symbol1 == "usd") 
    {
       if (symbol2 == "EUR" || symbol2 == "eur")
       {
       result = dollars * toEuros;
       }
       else if (symbol2 == "JPY" || symbol2 == "jpy")
       {
       result = dollars * toYens;
       }
       else if (symbol2 == "INR" || symbol2 == "inr")
       {
       result = dollars * toRupees;
       cout << dollars << " USD"  << setw(20) << result << " INR"<< endl;
       }
    }
    else if (symbol1 == "EUR" || symbol1 == "eur")
    {
    result = dollars * euros;
    }
    else if (symbol1 == "JPY" || symbol1 == "jpy")
    {
        result = dollars * yens;
    }
    else if (symbol1 == "INR" || symbol1 == "inr")
    {
    result = dollars * rupees;
    }
    else
    cout << endl << "You did not enter a valid currency symbol";

    return result;
    }

By the way, when I completely remove the convert function from the program and put all the code from that function directly into the main function, the program runs exactly as it is supposed to. The problem comes from using the convert function, in which I have used a function call to the Calculate function, but I am not sure if this is acceptable in C++ and cannot find an answer to whether you can call a function in another function either in my text or online anywhere. Thank you all for helping my poor lost mind with this.

Hi koldsoul ,

Calculate(dollars, rate);

In that function I didn't came across any usage of "rate" variable at all.

mainly , you didn't catched the returned value of calculate() function.

cannot find an answer to whether you can call a function in another function

you can call functions with in another function but you can't define a function with in another function.

you can call functions with in another function but you [B]can't define a function with in another function.

What I mean is :

void func1() 
 {
        call_fun2(); // legal
 }

void func1()
 {
         void fun2() // illegal
          {
            ...............
           }
}

I finally figured it out. Thank you to all who posted here. All your comments kept pushing me in the right direction and would never have gotten this done without you. Here is the finished code if you are wondering.

double Calculate (double dollars, double rate);
void convert();

string symbol1 = "USD", symbol2 = "USD";

int main()
{
    convert();

    return 0;
}


void convert()
    {
     ofstream outFile;
    double dollars, result, rate;
    {
    cout << "This is a conversion program for U.S. dollars into euros,yens, and rupees or vice versa" << endl;

    // get input    
    cout << "Please enter the three-letter symbol of the original currency: ";
    cin >> symbol1;
    cout << endl;

    cout << "Please enter the three-letter symbol of the resulting currency: ";
    cin >> symbol2;
    cout << endl;

    cout << "Please enter the amount to be converted: ";
    cin >> dollars;

         // set up output
    outFile.open("conversion.out");
    outFile << fixed << showpoint;
    outFile << setprecision(2); 
    outFile << "         Conversion Results " << endl << endl;


       cout << endl;
    outFile.close();

     cout << dollars << "  "  << symbol1 << setw(20) <<  Calculate(dollars, rate) << "  " << symbol2 << endl;

}}



double Calculate (double dollars, double rate)

    {
    double result = 0;
    const double toEuros = .826925;     // conversion rates
const double toYens = 118.511;
const double toRupees = 44.1650;
const double euros = 1.20185;     
const double yens = .00840795;
const double rupees = 0226501;
         // calculate conversion based on input choices 
    if (symbol1 == "USD" || symbol1 == "usd") 
    {
       if (symbol2 == "EUR" || symbol2 == "eur")
       {
       result = dollars * toEuros;
       }
       else if (symbol2 == "JPY" || symbol2 == "jpy")
       {
       result = dollars * toYens;
       }
       else if (symbol2 == "INR" || symbol2 == "inr")
       {
       result = dollars * toRupees;
       cout << dollars << " USD"  << setw(20) << result << " INR"<< endl;
       }
    }
    else if (symbol1 == "EUR" || symbol1 == "eur")
    {
    result = dollars * euros;
    }
    else if (symbol1 == "JPY" || symbol1 == "jpy")
    {
        result = dollars * yens;
    }
    else if (symbol1 == "INR" || symbol1 == "inr")
    {
    result = dollars * rupees;
    }
    else
    cout << endl << "You did not enter a valid currency symbol";

    return result;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.