基于用户输入的计算(c ++学校项目)

时间:2017-12-04 21:27:10

标签: c++ type-conversion currency

大家好我正在创建一个简单的c ++程序,除了计算收到的信息外,还可以将用户输入写入文本文件。到目前为止,我正在尝试将美元兑换成日元,但出于某种原因,我遇到了障碍,我无法弄清楚阻止该程序编制信息的原因。

//Dollars to Yen Project

#include <cstring>
#include <climits>
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

class A
{
private:
    float dollars, yen;

public:
    void store(float);
    float convert();
};

void A::store(float dollars1)

{
    dollars = dollars1;
}

float A::convert() //mutators used

{
    yen = dollars*0.0089;
    return yen;
}

int main()

{

    float dollars;
    float yen;
    cout << "Welcome to Japan, you will need to convert Dollars into Yen to spend money here\n" << endl;
    string b;
    while (1)

    {

        cout << "Enter the amount in dollars to be converted\n";
        cin >> b;
        system("pause");
        if (b.find_first_not_of("1234567890.-") != string::npos) //input check validity
        {

            cout << "invalid number:" << b << endl;

        }

        else

        {

            break;

        }
    }

    float r = atof(b.c_str());
    A a;
    a.store(r);
    float c = a.convert();

    stringstream ss;
    ss << c;

    string s1 = ss.str();
    ofstream prog;

    prog.open("money.txt");
    prog << "The required amount of Yen is " + s1;
    prog.close();
    return 0;

}

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

首先为类'A'创建对象,然后调用方法。

<Rectangle Fill="Black" Visibility="{x:Bind IsSelected, Mode=OneWay, Converter={StaticResource BoolToVis}}" />

然后调用方法,如,     a.store(R); a.convert();