0

Background: So I basically googled projects for beginners for C++ and one recommendation was currency converter. So I decided to do this, at first I tried to do it by creating a class, objects etc but I got lost with constructors and header files etc but that's a story for another day. I am very new to programming and this is my first "project".

So I decided to go the simple route and did this:

#include <iostream>

int main(){
    double euro;
    //creating all the variables and conversions
    double eur2dol = euro * 1.13;
    double eur2pound = euro * 0.84;
    double eur2rup = euro * 84.49;
    double eur2yuan = euro * 7.2322769;
    double eur2btc = euro * 0.0000237;
    double eur2eth = euro * 0.00029956;
    // creating an array with the name of the currency i am converting to
    std::string curname[6] = { " Dollars\n", " Pounds\n", " Rupees\n", " Yuan\n", " Bitcoin\n", " Etherium\n"};
    // creating an array with the conversions i want to do
    double currencies[6] = { eur2dol, eur2pound, eur2rup, eur2yuan, eur2btc, eur2eth };
    //accepting input from the user
    std::cout << "Amount in euros: " << std::endl;
    std::cin >> euro;
    // for loop to loop through every item in the currencies array and the corresponding name of the currency from the curname array
    for(int i = 0; i < 5; i++){
        std::cout << euro << " Euro is " << currencies[i] << curname[i];
    }; 
    return 0;
}

Now, I know there is no point in doing this other than practicing because the values constantly change but I am getting the same result no matter the value I type in and in scientific form.

How can I fix it so i get the correct result and what is the problem?

hyde
  • 60,639
  • 21
  • 115
  • 176
  • 5
    Compiling with most warnings enabled and elevated to errors produces a message that `euro` is uninitialized when used in line 5 (`double eur2dol = euro * 1.13;`). Turn on warnings in your compiler and set a switch to elevate them to errors. – Eric Postpischil Dec 29 '21 at 19:17
  • 1
    `double eur2dol = euro * 1.13;`, etc. You have not gotten `euro` from the user yet, so all those calculations are invalid. – 001 Dec 29 '21 at 19:17
  • Do remember to [turn on your compiler's warnings](https://godbolt.org/z/x6Pazxda5). – Drew Dormann Dec 29 '21 at 19:50

2 Answers2

1

A typical case of uninitialized variable. Here you are operating on euro variable before asking(cin) value for it, The correct implementation would be something like:

#include <iostream>

int main()
{
    double euro = 0; //always put some values beforehand
    //accepting input from the user
    std::cout << "Amount in euros: " << std::endl;
    std::cin >> euro;
    //creating all the variables and conversions
    double eur2dol = euro * 1.13;
    double eur2pound = euro * 0.84;
    double eur2rup = euro * 84.49;
    double eur2yuan = euro * 7.2322769;
    double eur2btc = euro * 0.0000237;
    double eur2eth = euro * 0.00029956;
    // creating an array with the name of the currency i am converting to
    std::string curname[6] = { " Dollars\n", " Pounds\n", " Rupees\n", " Yuan\n", " Bitcoin\n", " Etherium\n"};
    // creating an array with the conversions i want to do
    double currencies[6] = { eur2dol, eur2pound, eur2rup, eur2yuan, eur2btc, eur2eth };
    
    // for loop to loop through every item in the currencies array and the corresponding name of the currency from the curname array
    for(int i = 0; i < 5; i++){
        std::cout << euro << " Euro is " << currencies[i] << curname[i];
    }; 
    return 0;
}

Note: Uninitiazed variable is a bad practice. If you initialized the variable like double euro = 0; earlier, it will alteast not return arbitrary values even for the wrong code.

Aadil Hoda
  • 592
  • 7
  • 17
0

You can format your floating-point values with std::fixed and std::setprecision.

Apart from that:

  • Make sure you don't use euro before reading it.
  • Try and declare the variables as close to the place where you first use them as possible.
  • currencies array would probably better be called conversion_rates or something like that.

[Demo]

#include <iomanip>  // setprecision
#include <ios>  // fixed
#include <iostream>

int main() {
    // creating all the variables and conversions
    double eur2dol = 1.13;
    double eur2pound = 0.84;
    double eur2rup = 84.49;
    double eur2yuan = 7.2322769;
    double eur2btc = 0.0000237;
    double eur2eth = 0.00029956;
    // creating an array with the name of the currency i am converting to
    std::string currency_name[6] = { "Dollar", "Pound", "Rupee", "Yuan", "Bitcoin", "Etherium" };
    // creating an array with the conversions i want to do
    double conversion_rates[6] = { eur2dol, eur2pound, eur2rup, eur2yuan, eur2btc, eur2eth };
    // accepting input from the user
    std::cout << "Amount in euros: " << std::endl;
    double euro{};
    std::cin >> euro;
    // for loop to loop through every item in the currencies array and
    // the corresponding name of the currency from the curname array
    for (int i = 0; i < 6; i++) {
        std::cout << std::fixed << std::setprecision(2) << euro << " Euro is "
                  << std::setprecision(5) << euro * conversion_rates[i] << " " << currency_name[i] << "\n";
    }; 
    return 0;
}

You could as well slightly improve your solution and get started with concepts such as structs and STL containers just by:

  • defining a Currency struct, where you hold the the currency name and rate conversion from euros,
  • using a constant vector of currencies,
  • walking the vector with a range for loop.

[Demo]

#include <iomanip>  // setprecision
#include <ios>  // fixed
#include <iostream>  // cout
#include <string>
#include <vector>

struct Currency
{
    std::string name{};
    double from_euro_rate{};
};

int main() {
    const std::vector<Currency> currencies{
        {"Dollar", 1.13},
        {"Pound", 0.84},
        {"Rupee", 84.49},
        {"Yuan", 7.2322769},
        {"Bitcoin", 0.0000237},
        {"Etherium", 0.00029956}
    };
    
    // accepting input from the user
    std::cout << "Amount in euros: ";
    double euro{};
    std::cin >> euro;
    std::cout << euro << "\n\n";
    
    // for loop to loop through every item in the currencies array and
    // the corresponding name of the currency from the curname array
    for (auto& currency : currencies) {
        std::cout << std::fixed << std::setprecision(2) << euro << " Euro is "
                  << std::setprecision(5) << euro * currency.from_euro_rate << " " << currency.name << "\n";
    }; 
}
rturrado
  • 7,699
  • 6
  • 42
  • 62