2011-01-20 66 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 ""; 
}