2011-01-20 62 views
1

對不起,這一定是有史以來最s questions的問題之一,尤其是因爲我已經調用了一個函數。我用一個返回值調用了一個函數,並將返回值設置爲等於一個變量,但使用另一個返回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個字符串

+2

在哪裏返回值? – James 2011-01-20 21:53:57

+0

我沒有完成,但它應該返回編碼1,編碼2.我看到你的觀點,但它沒有給我一個錯誤,因爲沒有完成該功能,只爲調用它。 – 2011-01-20 21:56:22

+0

C++沒有多重返回值支持,如果這就是你要求的。你不能做返回(a,b)並期望得到任何有用的東西。 (當然,忽略可能重載的逗號操作符的行爲。) – ChrisV 2011-01-20 21:58:24

回答

0

首先,我沒有看到該函數中的單個返回語句。其次,你不能從函數返回兩個值。你可以返回一個單一的字符串(就像你的函數定義說的那樣),或者你可以修改傳入的值(只要它們是引用或指針)。

編輯:爲了闡述

如果你想在他們將需要引用或指針值可修改通過。這是因爲C++中的默認行爲是通過值(副本)傳遞參數,所以對函數參數的任何更改都不會從函數外部可見。然而,如果參數是引用/指針,你可以改變他們已經指向的內容(或者如果你想改變原始指針指向的指針,也就是指向指針,也就是說,不是變異,而是賦值給新的值/對象) 。

0

試圖編譯和運行你的代碼只會給我一個關於非void語句結束的警告。

我推薦至少添加一個佔位符返回值,直到函數完成爲止,如return "";

1

檢查的代碼以上cout << diagraph (mono1,mono2);線,以確保你沒有錯過一個尾隨分號,或左括號開。

0

它似乎不喜歡'cout',你是否包括命名空間std? 另外,請檢查您在使用之前是否聲明瞭該功能。

0

完整的代碼

#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 ""; 
} 
相關問題