将char变量传递给类型字符串函数

时间:2011-01-17 01:14:56

标签: c++ arrays arguments

我让用户输入一个被分解的关键字并放入一个五乘五的数组,然后另一个名为“getletter”的函数按字母顺序填充数组中的其余空格,不包括已包含在内的那些字母关键字。当我尝试将关键字的值传递给getletter函数时,它不起作用。

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

3 个答案:

答案 0 :(得分:1)

我认为问题出在比较中

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

等。问题是这些变量是静态类型的char [2],因此对它们进行的等式比较将比较数组和指针的地址,而不是指向的内容。要解决此问题,您可能需要将其更改为

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

使用string operator==函数进行真正的深度比较。

答案 1 :(得分:1)

目前尚不清楚为什么使用字符串而不是char来表示数组条目和字母变量。 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 ( j != 'q' && k == 'q' )

然后就可以了。

答案 2 :(得分:0)

我只是设置一个变量,每次循环运行时增加一个。