Invalid operands to binary expression ('ifstream' (aka 'basic_ifstream<char>') and 'char (*)[26]'

heres my 1st part of programme

#include <iostream>
#include <fstream>
using namespace std;

void getKey (char m[][26], int size, char* filename);
void encrypt (char m[][26], int s, char* msg);
void decrypt (char m[][26], int s, char* msg);

int main() {

char map[10][26];
char fname []="key.txt";
char c;
char msg[100];
getKey(map,10,fname);
cin >> c;
cin.getline(msg, 100);
if (c=='e')
encrypt(map,10,msg);
else if (c=='d')
decrypt(map,10,msg);
cout << msg << endl;
return 0;
}

void getKey (char m[][26], int size, char* filename){
ifstream fin;
char map[10][26];
fin.open("key.txt");
fin >> map;
fin.close();

}

the error Invalid operands to binary expression ('ifstream' (aka 'basic_ifstream<char>') and 'char (*)[26]')
occurred in the line fin >> map
i was wondering whats wrong in my programme please help thanksss
Last edited on
The insertion operator (>>) has no overload that matches an ifstream on one side and a char[10][26] on the other. You either need to supply that overload or perhaps read into map a line at a time. It depends on what exactly is in key.txt.
@tpb thank you so much for ur reply. In my key.txt file, it has 10 rows of characters, each row 26 characters. My task was to decrypt some messages according to the key.txt

did u mean that i should fin >> line by line, like fin >> s1....... until s10? thx
Something like this might do. Since map is maybe not a good name in C++ (which has a map type in the standard library) I changed it to key.
1
2
3
4
5
    char key[10][26];
    for (int i = 0; i < 10; i++) {
        std::cin.getline(line, sizeof line);
        strncpy(key[i], line, 26);   // include <cstring> for this
    }

Note that you can't treat the rows of key as strings since they are not zero-terminated. But as long as you are always accessing it by index that may be okay.
Last edited on
@tpb thankssss it really helps !!!
Topic archived. No new replies allowed.