【问题标题】:How can I use a string from another function? [closed]如何使用来自另一个函数的字符串? [关闭]
【发布时间】: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;
}

【问题讨论】:

  • 任何book 都会很早就拥有这些信息。
  • 我知道,我完全没有一本,因为所有推荐的好书都有点贵。我一直在使用this 网站。
  • 我注意到上一节有关于返回值和参数的信息。

标签: c++ function methods scope main


【解决方案1】:

返回 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
}

【讨论】:

  • 非常感谢!我学习函数的唯一一点是here。您刚刚在这里展示的内容有助于我更好地理解它!到目前为止,我已经基本了解了所有内容,除了在功能方面我有点迷茫。你写的关于称它的第二部分让我有点困惑。我究竟会在哪里做呢?
  • 您可以在main 中调用这两行代码。因为现在,您只需调用函数,然后执行它们,然后所有变量都超出范围。要将某些内容传递给另一个函数,您需要将其返回。
  • 我是在 main 中调用这两个变量还是我仍然在那里调用函数?我不太确定你的意思。我需要在函数或 main 中返回一些东西吗?
  • 最后将你的函数name()修改为return name1。然后在main 中,你可以说string userName = name(),它将获取从name() 返回的任何内容并将其分配给userName。然后,您可以在main 中传递该变量,如果您想在其中使用它,可以将其传递给height()
  • 请看我上面的编辑,希望更清楚。
猜你喜欢
  • 2012-07-20
  • 2022-01-19
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 2016-05-30
  • 2021-03-23
  • 2021-12-16
  • 2017-11-01
相关资源
最近更新 更多