【发布时间】:2020-10-18 23:35:10
【问题描述】:
我正在制作一个 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);
}
【问题讨论】:
-
您应该先将结果存储在变量中,然后在您的 cout 中使用它(变量)。
-
而不是使用 int htInches; cout > htInches;我想使用变量中的数据并将其应用于 Covert(output); cout