简体   繁体   中英

Calling a function

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

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.

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, ie, not a mutation, but an assignment to a new value/object).

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 ""; .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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