开发者

Passing char variables to a type string function

开发者 https://www.devze.com 2023-02-04 08:36 出处:网络
I have the user enter a keyword which is broken up and put into a five by five array, then another f开发者_运维百科unction called \"getletter\" which fills up the rest of the spaces in the array in al

I have the user enter a keyword which is broken up and put into a five by five array, then another f开发者_运维百科unction called "getletter" which fills up the rest of the spaces in the array in alphabetical order excluding those letters already included in the keyword. When I try to pass the values of the keyword to the getletter function it doesn't work.

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<limits>

using namespace std;
string getletter();  
char type[81];
char filename[20];
char key [5];

char f[2] = "q";
    char g[2] = "q";
    char h[2] = "q";
    char i[2] = "q";
    char j[2] = "q";
    char k[2] = "q";
    char l[2] = "q";

int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;

int main(){



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

};





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){
cipherarray[b][c] = key[a];

 if (  f == "q" ) {
 cipherarray[b][c] = f;
}

 if ( f != "q" && g == "q"  )
 {
cipherarray[b][c] = g;
}

 if ( g != "q" && h == "q" )
 {
cipherarray[b][c] = h;
}

 if ( h != "q" && i == "q"  )
 {
cipherarray[b][c] = i;
}


 if ( i != "q" && j == "q" ) 
{
cipherarray[b][c] = j;
}

 if ( j != "q" && k == "q" )
 {
cipherarray[b][c] = k;
}

 if ( k != "q" && l == "q" )
 {
cipherarray[b][c] = l;
}
a++;
b++;
if (key[a] == 0)
break; 
}

if (key[a] != 0){
c++;
b = 0;
}
}

while ( c <= 4) {
while ( b <= 4) {
 cipherarray[b][c] = getletter();
 b++;     
}
b = 0;
c++;
} 










b = 0;
c = 0;

while ( c <= 4) {
while ( b <= 4) {
 cout<<cipherarray[b][c]<<" ";
 b++;     
}
cout<<endl;
b = 0;
c++;
} 






 cout<<"now enter some text."<<endl<<"To end this program press Crtl-Z\n";


ofstream outFile;
outFile.open(filename);
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
cin.ignore(std::numeric_limits<int>::max(),'\n');

while(!cin.fail()){

 cin.getline(type,81);

outFile<<type<<endl;
}

outFile.close();
}





string getletter() {
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"}
};


if (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++; 
 } 
 else {
letter = cipherarraytemplate[d][e];
}
d++;
if (d == 5){
e++;
d = 0;
}
return letter;
}


I think the problem is in the comparisons

if ( j != "q" && k == "q" )

etc. The problem is that these variables are statically-typed as char [2], and so equality comparisons made on them will be comparing the addresses of the arrays and the pointers, not the contents of what's pointed at. To fix this, you might want to change this to read

if ( j != string("q") && k == string("q") )

which uses the string operator== function to do a true deep comparison.


It is not clear why you are using string rather than char for the array entries and the letter variables. E.g.

char f = 'q';
char getletter();  
char 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'}

    };

If you change all the string literals to single character literals and so on, you will avoid having to understand pointer semantics for this problem. Expressions such as:

if ( j != 'q' && k == 'q' )

will then just work.


I just set a variable that increases by one each time the loop runs through.

0

精彩评论

暂无评论...
验证码 换一张
取 消