簡體   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