微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何将cout的输出存储到变量中

如何解决我如何将cout的输出存储到变量中

我正在制作一个C ++程序,根据您的父母估算您的身高。我要制作它,以便可以同时输出英尺和英寸的高度。我不知道如何将cout的输出存储在

if (boygirl == "boy") {
    cout <<"Your estimated height is " << (mom *13/12 + dad) / 2  << " inches";
} and   else if (boygirl == "girl") {
    cout <<"Your estimated height is " << (dad+12/13 + mom) / 2   <<" inches";

放入变量中,这样我就可以从变量中获取数据并使用它,而不是在上一步中询问英寸的结果。

您可能需要运行代码以了解我的意思。 如果您不明白我的意思,请随时发表评论

#include <iostream>
#include <string>

using namespace std;

void Convert(int inch) {
    int feet,inches;
    inches = inch % 12;
    feet = inch / 12;
    cout << "\n\t\tThe height in feet is " << feet << "\'" << inches << "\" " << endl;
}

int main() {
    int i = 0;
    do {
        float mom;
        float dad;
        string doyouwish;
        string boygirl;

        cout << " \n\nWELCOME TO THE C++ HEIGHT PREDICTION PROGRAM";
        cout << "\n\n INPUT GENDER TO BEGIN boy/girl: ";
        cin >> boygirl;

        cout << "How tall is your mother in inches: ";
        cin >> mom;
        cout << "How tall is your father in inches: ";
        cin >> dad;

        if (boygirl == "boy") {
            cout << "Your estimated height is " << (mom * 13 / 12 + dad) / 2 << " inches";
        } else if (boygirl == "girl") {
            cout << "Your estimated height is " << (dad + 12 / 13 + mom) / 2 << " inches";
        }

        int htInches;
        // Ask height from user
        cout << "\n\ntEnter height in Inches from the prevIoUs results: ";
        cin >> htInches;

        Convert(htInches);
        cout << "\n\n\n";

        ++i;
    } while (i < 10);
}

解决方法

您在寻找这样的东西吗?

int htInches = 0;
if (boygirl == "boy") {
  htInches = (mom * 13 / 12 + dad) / 2;
} else if (boygirl == "girl") {
  htInches = (dad + 12 / 13 + mom) / 2;
}
cout << "Your estimated height is " << htInches << " inches";

计算结果,将其存储在变量中,然后然后打印。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。