调用函数

发布于 2024-10-12 23:18:46 字数 965 浏览 2 评论 0原文

请原谅,这一定是有史以来问过的最愚蠢的问题之一,尤其是因为我已经调用了一个函数。我调用了一个具有一个返回值的函数,并将该返回值设置为等于一个变量,但使用另一个返回 2 个变量的函数;我只想运行该函数并返回值。

我的声明:

string diagraph ( string mono1, string mono2);

调用函数:

cout << diagraph (mono1,mono2);

函数本身:

string diagraph(string mono1, string mono2) {
    string encoded1,encoded2;
    int a,b,c,d,e,f;
    a = 0;
    b = 0;
    while( mono1 != cipherarray[b][c]){
        b++;
        if (b == 5) {
            a = 0;
            b++;
        }
    }
    a = c;
    b = d;


    a = 0;
    b = 0;

    while (mono2 != cipherarray[b][c]){ 
        b++;
        if (b == 5) {
            a = 0;
            b++;
        }
    }

    a = e;
    b = f;
}

错误(与调用函数有关):

C++\expected constructor, destructor, or type conversion before '<<' token 
 expected `,' or `;' before '<<' token 

函数尚未完成,但它将返回 2 个字符串

Pardon me, this must be one of the silliest questions ever asked especially since I've already called one function. I have called one function with one return value and set that return value equal to a variable but with another function that returns 2 variables; I just want to run the function and return the values.

my declaration:

string diagraph ( string mono1, string mono2);

calling the function:

cout << diagraph (mono1,mono2);

The function itself:

string diagraph(string mono1, string mono2) {
    string encoded1,encoded2;
    int a,b,c,d,e,f;
    a = 0;
    b = 0;
    while( mono1 != cipherarray[b][c]){
        b++;
        if (b == 5) {
            a = 0;
            b++;
        }
    }
    a = c;
    b = d;


    a = 0;
    b = 0;

    while (mono2 != cipherarray[b][c]){ 
        b++;
        if (b == 5) {
            a = 0;
            b++;
        }
    }

    a = e;
    b = f;
}

The errors(having to do with calling the function):

C++\expected constructor, destructor, or type conversion before '<<' token 
 expected `,' or `;' before '<<' token 

the function is not finished but it will return 2 strings

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

姜生凉生 2024-10-19 23:18:46

检查上面的代码行cout << diagraph (mono1,mono2); 以确保您没有遗漏尾随分号,或留下括号。

Check the line of code above cout << diagraph (mono1,mono2); to make sure you haven't missed a trailing semicolon, or left a parenthesis open.

任性一次 2024-10-19 23:18:46

首先,我在该函数中没有看到任何 return 语句。其次,函数不能返回两个值。您可以返回单个字符串(正如您的函数定义所示),也可以修改传入的值(只要它们是引用或指针)。

编辑:详细说明

如果您想修改传入的值,它们将需要是引用或指针。这是因为 C++ 中的默认行为是按值传递参数(复制),因此对函数参数的任何更改从函数外部都是不可见的。但是,如果参数是引用/指针,您可以改变它们已经指向的内容(或者如果您想更改原始指针指向的内容,则可以改变指向指针的指针,即不是突变,而是对新值/对象的赋值) 。

First off, I don't see a single return statement in that function. Second, you can't return two values from a function. You can either return a single string (as your function definition says it will) or you can modify passed in values (as long as they are references or pointers).

EDIT: To elaborate

If you want to modify passed in values they will need to be references or pointers. This is because the default behavior in C++ is to pass arguments by value (copy), so any change to the functions parameters will not be visible from outside of the function. However, if the arguments are references/pointers you can mutate what they already point to (or pointers to pointers if you want to change what the original pointer points to, i.e., not a mutation, but an assignment to a new value/object).

煮酒 2024-10-19 23:18:46

尝试编译并运行代码只会给我一个关于非 void 语句结束的警告。

我建议至少添加一个占位符返回值,直到函数完成,例如 return "";

Attempting to compile and run your code only gives me a warning about end of a non-void statement.

I recommend at least adding a placeholder return value until the function is done, something like return "";.

野侃 2024-10-19 23:18:46

看起来不喜欢“cout”,您是否包括名称空间 std?
另外,请在使用函数之前检查是否声明了该函数。

It appear that does not like the 'cout', are you including the namespace std?
Also, check that you declare the function before using it.

樱花细雨 2024-10-19 23:18:46

完整代码

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<limits>

using namespace std;
string getletter(string f = "q", string g = "q", string h = "q", string i = "q", string j = "q", string k = "q", string l = "q" ); 
 string diagraph ( string mono1, string mono2);



char type[81];
char filename[20];
char key [20];
string f = "q";
string g = "q";
string h = "q";
string i = "q";
string j = "q";
string k = "q";
string l = "q";
string mono1; 
string mono2;

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int m = 0;

string cipherarray[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}

};
int main(){









cout<<"Enter the name of a file you want to create.\n";

cin>>filename;

cout<<"enter your codeword(codeword can have no repeating letters)\n"; 

cin>>key;

while (key[a] != '\0' ){

while(b <= 4){
        m++;
cipherarray[b][c] = key[a];

 if (m == 1 ) {
f = cipherarray[b][c];
}

 if ( m == 2 ) {
g = cipherarray[b][c];
}

 if ( m == 3 )
 {
h = cipherarray[b][c];
}

 if ( m == 4  )
 {
i = cipherarray[b][c];
}


 if ( m == 5 ) 
{
j = cipherarray[b][c];
}

 if ( m == 6 )
 {
k = cipherarray[b][c];
}

 if ( m == 7 )
 {
l = cipherarray[b][c];
}


a++;
b++;
if (key[a] == 0)
break; 
}

if (key[a] != 0){
c++;
b = 0;
}
}


// code to copy alphabet from getletter function onto cipherarray array
while ( c <= 4) {
while ( b <= 4) {
 cipherarray[b][c] = getletter(f,g,h,i,j,k,l);
 b++;     
}
b = 0;
c++;
} 









// code to display cipher array onscreen

b = 0;
c = 0;
cout<<endl<<endl<<"                      ";

string getletter(string f, string g , string h  , string i  , string j , string k , string l ) {
string letter;
string cipherarraytemplate[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}
};





while (cipherarraytemplate[d][e] == f || cipherarraytemplate[d][e] == g || cipherarraytemplate[d][e] == h || cipherarraytemplate[d][e] == i ||
 cipherarraytemplate[d][e] == j || cipherarraytemplate[d][e] == k || cipherarraytemplate[d][e] == l){ 
 d++; 
 if (d == 5){
e++;
d = 0;
}
 } 


letter = cipherarraytemplate[d][e];

d++;
if (d == 5){
e++;
d = 0;
}
return letter;
}

string diagraph(string mono1, string mono2) {
string encoded1,encoded2;
int a,b,c,d,e,f;
a = 0;
b = 0;
while( mono1 != cipherarray[b][c]){
b++;
if (b == 5) {
a = 0;
b++;
}
}
a = c;
b = d;


a = 0;
b = 0;

while (mono2 != cipherarray[b][c]){ 
b++;
if (b == 5) {
a = 0;
b++;
}
}

a = e;
b = f;
return "";
}

The full code

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<limits>

using namespace std;
string getletter(string f = "q", string g = "q", string h = "q", string i = "q", string j = "q", string k = "q", string l = "q" ); 
 string diagraph ( string mono1, string mono2);



char type[81];
char filename[20];
char key [20];
string f = "q";
string g = "q";
string h = "q";
string i = "q";
string j = "q";
string k = "q";
string l = "q";
string mono1; 
string mono2;

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int m = 0;

string cipherarray[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}

};
int main(){









cout<<"Enter the name of a file you want to create.\n";

cin>>filename;

cout<<"enter your codeword(codeword can have no repeating letters)\n"; 

cin>>key;

while (key[a] != '\0' ){

while(b <= 4){
        m++;
cipherarray[b][c] = key[a];

 if (m == 1 ) {
f = cipherarray[b][c];
}

 if ( m == 2 ) {
g = cipherarray[b][c];
}

 if ( m == 3 )
 {
h = cipherarray[b][c];
}

 if ( m == 4  )
 {
i = cipherarray[b][c];
}


 if ( m == 5 ) 
{
j = cipherarray[b][c];
}

 if ( m == 6 )
 {
k = cipherarray[b][c];
}

 if ( m == 7 )
 {
l = cipherarray[b][c];
}


a++;
b++;
if (key[a] == 0)
break; 
}

if (key[a] != 0){
c++;
b = 0;
}
}


// code to copy alphabet from getletter function onto cipherarray array
while ( c <= 4) {
while ( b <= 4) {
 cipherarray[b][c] = getletter(f,g,h,i,j,k,l);
 b++;     
}
b = 0;
c++;
} 









// code to display cipher array onscreen

b = 0;
c = 0;
cout<<endl<<endl<<"                      ";

string getletter(string f, string g , string h  , string i  , string j , string k , string l ) {
string letter;
string cipherarraytemplate[5][5]= {
{"a","f","k","p","v"},
{"b","g","l","r","w"},
{"c","h","m","s","x"},
{"d","i","n","t","y"},
{"e","j","o","u","z"}
};





while (cipherarraytemplate[d][e] == f || cipherarraytemplate[d][e] == g || cipherarraytemplate[d][e] == h || cipherarraytemplate[d][e] == i ||
 cipherarraytemplate[d][e] == j || cipherarraytemplate[d][e] == k || cipherarraytemplate[d][e] == l){ 
 d++; 
 if (d == 5){
e++;
d = 0;
}
 } 


letter = cipherarraytemplate[d][e];

d++;
if (d == 5){
e++;
d = 0;
}
return letter;
}

string diagraph(string mono1, string mono2) {
string encoded1,encoded2;
int a,b,c,d,e,f;
a = 0;
b = 0;
while( mono1 != cipherarray[b][c]){
b++;
if (b == 5) {
a = 0;
b++;
}
}
a = c;
b = d;


a = 0;
b = 0;

while (mono2 != cipherarray[b][c]){ 
b++;
if (b == 5) {
a = 0;
b++;
}
}

a = e;
b = f;
return "";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文