我不明白为什么我的输出是 1,1 如何将其更改为 "type name" 和 "parameters" ?蒂亚

I can't understand why my output is 1,1 how can I change it to "type name" and "parameters"? TIA

本文关键字:type name 蒂亚 parameters 我的 为什么 明白 输出      更新时间:2023-10-16

我只是 c++ 的新手,我正在阅读有关函数的信息,但现在我正在尝试使用字符串,但我真的无法理解它。 如何显示输出中的"类型名称"和"参数"?这是我的代码:

#include <iostream>
using namespace std;
string f1 (string a)
{
    string b;
    b = " type name ";
    return (b);
}
string f2 (string c)
{
    string d;
    d = " parameters ";
    return (d);
}
int main ()
{
    string x,y;
    x = f1(" type name ");
    y = f2(" parameters ");
    cout<<"Functions is a group of statements that executed"<<endl;
    cout<<"when it is called. It is consist of"<< x <<","<< y <<endl;
    cout<<"and a statement. It can be called to some some point of the program."<<endl;
    return 0;
   }
cout<<"when it is called. It is consist of"<< f1 <<","<< f2 <<endl;

f1f2是函数。你不想输出函数,你想输出调用函数的结果

x = f1(" type name ");
y = f2(" parameters ");

您已将调用结果存储在 xy 中,因此请使用它们。

cout<<"when it is called. It is consist of"<< x <<","<< y <<endl;
//                                            ^         ^

更改此内容:

cout<<"when it is called. It is consist of"<< f1 <<","<< f2 <<endl;

对此:

cout<<"when it is called. It is consist of"<< x <<","<< y <<endl;

但是,您的代码中有冗余。

你想做什么,

string f1 (string a)
string f2 (string c)

接受一个字符串,但你没有对它做任何事情,主要是用你从未在内部使用的字符串调用函数。

您是否尝试使用,

string f1 ()
string f2 ()

并使用它

cout<<"when it is called. It is consist of"<< x <<","<< y <<endl;

您正在尝试打印字符串而不是函数