1

I am a new relatively new programmer and I've decided to code in C++. Currently, I am working on a converter app project. I want to convert from money to weight to temperatures and if possible, even more, things. I have written a good block of code and I wanted to check if it worked. For some reason, the code did compile but all the application did be give me a blank screen with no area to enter a value or anything. It did not display the instructions I wrote or anything.

Here is the code:

#include "std_lib_facilities.h"

int a = 0;
int b = 0;
int c = 0;

int money()
{
    cout << "This will convert CAD to either USD or EUR\n";
    cout << "Please input USD or EUR followed by an enter keystroke for conversion\n";
    string a;
    while(cin >> a){
        if( a == "USD"){
            cout << "If you would like to convert USD into CAD, enter 1.\n";
            cout << "If you would like to convert CAD into USD, enter 2.\n";
            int x;
            cin >> x;
            if( x == 1){
                cout << "Please enter the amount you wish to convert.\n";
                double j;
                cin >> j;
                double k = j*1.29;
                cout << j << "USD is " << k << "CAD.\n";

            }
            if ( x == 2){
                cout << "Please enter the amount you wish to convert.\n";
                double o;
                cin >> o;
                double p = o*0.77;
                cout << o << "CAD is " << p << "USD.\n";
            }
        }
        if( a == "EUR"){
            cout << "If you would like to convert EUR into CAD, enter 1.\n";
            cout << "If you would like to convert CAD into EUR, enter 2.\n";
            int y;
            cin >> y;
            if(y == 1){
                cout << "Please enter the amount you wish to convert.\n";
                double g;
                cin >> g;
                double h = g*1.46;
                cout << g << "EUR is " << h << "CAD.\n";

            }
            if(y == 2){
                cout << "Please enter the amount you wish to convert.\n";
                double z;
                cin >> z;
                double x = z*0.69;
                cout << z << "CAD is " << x << "EUR.\n";
            }

        }
    }

}

int weight()
{
    cout << "This will convert pounds to kilograms.\n";
    cout << "Please input the amount you wish to convert.\n";
}

int temperature()
{

}

int setup()
{
    cout << "Please enter either a,b or c for the corresponding conversions.\n";
    cout << "(Where a is for currency, b is for weight, and c is for temperature.\n";
    string a;
    while ( cin >> a){
        if( a == "a"){
            int money();
            }
        if( a == "b"){
            int weight();
        }
        if( a == "c"){
            int temperature();
        }
    }
}

int main()
{
    cout << "Welcome to the ultimate converter app.\n";
    int setup();
    return 0;
}

Any help would be appreciated.

EDIT: SORRY FOR THE TROUBLE EVERYONE. THANKS TO EVERYONE WHO ANSWERED I JUST REALIZED I DECLARED A FUNCTION INSTEAD OF CALLING IT.

KDX
  • 61
  • 2
  • 7
  • Where do you convert to weight? What is the conversion rate between money and temperature or money and weight? – Thomas Matthews Oct 05 '17 at 23:26
  • You could save a lot of repetition by asking for the amount to convert before asking the *direction* to convert. – Thomas Matthews Oct 05 '17 at 23:27
  • BTW, you need to review your favorite C++ reference about calling functions. For example, you don't have a return type when you call a function. looks like you are declare functions in `setup` and `main`. – Thomas Matthews Oct 05 '17 at 23:29
  • You `money` function lies. The signature says it returns an `int`, but I don't see any `return` statement. If you don't return a value, declare the return type as `void`. Likewise with `weight`, `temperature` and `setup`. – Thomas Matthews Oct 05 '17 at 23:30
  • 1
    You have a *shadow* or *hiding* issue. You declare `a` globally as an `int`. In the `money` function, you declare `a` as a string. The preference is to use different variable names for new variables. – Thomas Matthews Oct 05 '17 at 23:33
  • Your global variables, `b` and `c`, are not used in your program. You may want to delete them. – Thomas Matthews Oct 05 '17 at 23:35
  • Also, search the internet for "c++ const correctness". If you don't change a variable after declaration, like `k`, you should declare it as `const`: `const double k = j * 1.29;` – Thomas Matthews Oct 05 '17 at 23:36

2 Answers2

3

Replace int setup(); to setup(); in main().

int setup(); just declares function, but don't call it.

Also you have to correct setup():

void setup()
{
    cout << "Please enter either a, b or c for the corresponding conversions." << endl;
    cout << "Where a is for currency, b is for weight, and c is for temperature." << endl;

    string str;
    while (cin >> str) {
        if (str == "a") {
            money();
        }
        else if (str == "b") {
            weight();
        }
        else if (str == "c") {
            temperature();
        }
        else {
            cout << "Invalid input." << endl; 
        }
    }
}

money(), weight() and temperature() must return void type too.

boriaz50
  • 860
  • 4
  • 17
  • The OP needs to have the `return` values match the return type of the function. Presently, none of the functions, except `main`, return a value. – Thomas Matthews Oct 05 '17 at 23:38
0

I think when you call a function in C++, you only type the name (given that it's defined), you don't write a type before function name, try this...

    #include <iostream>
using namespace std;

int a = 0;
int b = 0;
int c = 0;

int money()
{
    cout << "This will convert CAD to either USD or EUR\n";
    cout << "Please input USD or EUR followed by an enter keystroke for conversion\n";
    string a;
    while(cin >> a){
        if( a == "USD"){
            cout << "If you would like to convert USD into CAD, enter 1.\n";
            cout << "If you would like to convert CAD into USD, enter 2.\n";
            int x;
            cin >> x;
            if( x == 1){
                cout << "Please enter the amount you wish to convert.\n";
                double j;
                cin >> j;
                double k = j*1.29;
                cout << j << "USD is " << k << "CAD.\n";

            }
            if ( x == 2){
                cout << "Please enter the amount you wish to convert.\n";
                double o;
                cin >> o;
                double p = o*0.77;
                cout << o << "CAD is " << p << "USD.\n";
            }
        }
        if( a == "EUR"){
            cout << "If you would like to convert EUR into CAD, enter 1.\n";
            cout << "If you would like to convert CAD into EUR, enter 2.\n";
            int y;
            cin >> y;
            if(y == 1){
                cout << "Please enter the amount you wish to convert.\n";
                double g;
                cin >> g;
                double h = g*1.46;
                cout << g << "EUR is " << h << "CAD.\n";

            }
            if(y == 2){
                cout << "Please enter the amount you wish to convert.\n";
                double z;
                cin >> z;
                double x = z*0.69;
                cout << z << "CAD is " << x << "EUR.\n";
            }

        }
    }

}

int weight()
{
    cout << "This will convert pounds to kilograms.\n";
    cout << "Please input the amount you wish to convert.\n";
}

int temperature()
{

}

int setup()
{
    cout << "Please enter either a,b or c for the corresponding conversions.\n";
    cout << "(Where a is for currency, b is for weight, and c is for temperature.\n";
    string a;
    while ( cin >> a){
        if( a == "a"){
             money();
            }
        if( a == "b"){
             weight();
        }
        if( a == "c"){
             temperature();
        }
    }
}

int main()
{
    cout << "Welcome to the ultimate converter app.\n";
    setup();
    return 0;
}
Kareem
  • 259
  • 1
  • 3
  • 13
  • Where are the `return` statements for returning values (except `main`)? You may want to add those corrections (or change the return types to `void`). – Thomas Matthews Oct 05 '17 at 23:39
  • @ThomasMatthews This code is the author's code, I just made it compile successfully :) – Kareem Oct 06 '17 at 02:11
  • Thank you all, I just realized I declared a function instead of calling it... I'm dumb. – KDX Nov 11 '17 at 17:09