c++ - 如何使用来自另一个函数的字符串?

标签 c++ function methods scope program-entry-point

<分区>

在我的程序中(我将在下面包含代码),我有一个函数来确定用户的姓名和高度。我首先使用 name 函数 void name(),然后是紧随其后的函数 void height()(当然 main 是最后一个)。

我想做的是在整个程序中显示用户名。在我的第二个函数中,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;
}

最佳答案

返回一个 string 而不是 void

string 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;
    return name1;
}

height() 相同,例如,它应该返回一个 int。还可以在 height 函数中获取名称。

int height(string name1)
{
    // cout stuff about name
    return userHeight;
}

然后你可以这样调用它:

int main()
{
    string userName = name();  // takes the return from name and assigns to userName
    int userHeight = height(userName);   // passes that string into height()
    return 0;
}

更多使用函数和返回东西的例子:

int add(int a, int b)
{
    int total = a + b;   // the variable total only exists in here
    return total;
}

int add4Numbers(int w, int x, int y, int z)
{
    int firstTwo = add(w, x);  // I am caling the add function
    int secondTwo = add(y,z);  // Calling it again, with different inputs
    int allFour = add(firstTwo, secondTwo);   // Calling it with new inputs
    return allFour;
}   // As soon as I leave this function, firstTwo, secondTwo, and allFour no longer exist
    // but the answer allFour will be returned to whoever calls this function

int main()
{
    int userA = 1;
    int userB = 7;
    int userC = 3;
    int userD = 2;

    int answer = add4Numbers( userA, userB, userC, userD )  // this grabs the value from allFour from inside the add4Numbers function and assigns it to my new variable answer
    return answer;  // now equals 13
}

关于c++ - 如何使用来自另一个函数的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107856/

相关文章:

python - Python 类中的数学方法

java - 编写计算复利的方法

c++ - boost::asio lib 的那些 async_* 函数是否由操作系统并行执行

c++ - CGAL - boost 创建无向图

function - 如何找到所有用户定义的(非扩展相关的)函数?

C++ 继承不起作用

java - 如何防止方法被调用两次?

java - 访问第二类中另一个类的 Swing 组件

c++ - 简洁可以保存哪些错误?

c++ - C++中定义一个行数未知的数组