【发布时间】:2014-07-29 06:14:33
【问题描述】:
在我的程序中(我将在下面包含代码),我有一个函数来确定用户的姓名和身高。我首先使用名称函数void name(),然后使用函数void height()(当然主要是最后一个)。
我要做的是在整个程序中显示用户名。在我的第二个函数中,void height() 是询问用户他们有多高:
cout << " How tall are you?" << endl;
我想问“你有多高,name1?” ,但字符串name1 未在范围内声明。关于如何使它工作/我做错了什么的任何想法?谢谢你。此外,如果您发现任何其他问题或我可以做些什么来使事情变得更容易/替代方法,请告诉我! (我是新人!)
#include <iostream>
#include <string>
using namespace std;
void name()
{
cout << "Welcome ________ ... uhmmmm, what was your name again? ";
string name1;
cin >> name1;
cout << " " << endl;
cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
}
void height()
{
//feet and inches to inches
cout << " How tall are you?" << name1 << endl;
cout << " " << endl;
cout << " " << endl;
cout << " Enter feet: ";
int feet;
cin >> feet;
cout << " " << endl;
cout << " Enter inches: ";
int inches;
cin >> inches;
int inchesheight;
inchesheight = (feet * 12) + inches;
cout << " " << endl;
cout << " Your height is equal to " << inchesheight << " inches total." << endl;
if (inchesheight < 65 )
{
cout << " You are shorter than the average male." << endl;
}
else if (inchesheight > 66 && inchesheight < 72)
{
cout << " You are of average height." << endl;
}
else
{
cout << " You are taller than average." << endl;
}
}
int main()
{
name();
height();
return 0;
}
【问题讨论】:
标签: c++ function methods scope main