繁体   English   中英

调用函数

[英]Calling a function

请原谅,这肯定是有史以来最愚蠢的问题之一,尤其是因为我已经调用了一个函数。 我已经调用了一个具有一个返回值的函数,并将该返回值设置为等于一个变量,而另一个函数则返回了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个字符串

检查cout << diagraph (mono1,mono2); 上方的代码行cout << diagraph (mono1,mono2); 以确保您没有错过分号或遗漏括号。

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

编辑:详细说明

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

尝试编译和运行代码只会给我有关非空语句结束的警告。

我建议至少在功能完成之前添加一个占位符返回值,例如return "";

似乎不喜欢'cout',您是否包括名称空间std? 另外,在使用该函数之前,请检查是否已声明该函数。

完整代码

#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 "";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM