问题描述
我正在制作一个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";
计算结果,将其存储在变量中,然后然后打印。