B
B
biohazardbenzg2020-12-08 13:46:51
C++ / C#
biohazardbenzg, 2020-12-08 13:46:51

How to improve (remake) the currency convector in C++?

Hello.
I'm learning C++ from Barjane Stroustrup's book.

Got a job - Write a program to convert hryvnia, rubles and yuan into dollars.

Wrote this "miracle"

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    // Перевод гривен, рублей, юаней в доллар

    // Использование инструкции "if"

    setlocale(LC_CTYPE, "Russian_Russia.1251");
    double UAH = 28.29, RU = 74.12, CNY = 6.52; // стоимость валют за 1 доллар
    int dollar; // единица для перевода
    char currenty = ' ';
    cout << "Введите валюту и единицу, для перевода\n"; 
    cin >> dollar >> currenty;
    if (currenty == 'u')
    {
        cout << "Стоимость " << dollar << "$ в UAH составит(" << dollar * UAH << ").";
    }
    if (currenty == 'r')
    {
        cout << "Стоимость " << dollar << "$ в RU составит(" << dollar * RU << ").";
    }
    if (currenty == 'c')
    {
        cout << "Стоимость " << dollar << "$ в CNY составит(" << dollar * CNY << ").";
    }
    
}


*It took two evenings to somehow work...

I want to know how to make it more simple and correct, having accordingly my level of knowledge (the basics of the basics), so that I can understand and digest it all.

Thanks in advance for your help and explanation!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Adamos, 2020-12-08
@biohazardbenzg

You are doing nonsense, licking the drill on the operator.
Postpone optimizations and prettiness at least until the end of the tutorial.
Now, with a barely started base, you can only pick up strange ideas, which you will then have to get rid of, relearning.

G
GavriKos, 2020-12-08
@GavriKos

For example, try to get rid of ifs and variables for each currency. Hint - you need to use the correct collection for this case.

A
Antony, 2020-12-08
@RiseOfDeath

As a whole, it will go like this (if the task, as you said, is on ifs).
In general, from criticism / recommendations:
1.

Write a program to convert hryvnia, rubles and yuan into dollars.

Your program does exactly the opposite - it converts from dollars to hryvnia / rubles / yuan.
2. Use international currency codes instead of letters 'r'/'u'/'c' https://ru.wikipedia.org/wiki/ISO_4217
You will need to work with strings. This 100% is somewhere further along the course of the tutorial, so just read on, do not get hung up on this task.
3. Excessive specifically for this task, but useful for practice - Make a currency using classes.
At a minimum, you need methods like converting to a string, overloading << >> operators and arithmetic operators, plus functions like convertTo(std::string currencyName)
Again, 146% that the whole second half of the OOP textbook, so it makes no sense to spend time on this now.
4. Well, just in case, do not use floating point numbers to work with money (or other entities when accuracy is important to you) if you write something serious, otherwise, you know, 0.1 + 0.2 != 0.3. In this program, this is not important, you do not need to redo it, but at least you need to know why this is happening.
A normal textbook, in theory, should explain the features of floating point numbers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question