#include <iostream>
#include<fstream>
int decryption(int);
int multiply(int,int[][2]);
usingnamespace std;
main(){
int n;
ifstream inFile;
inFile.open ("out.txt");
while(!inFile.eof()) {
inFile>>n;
if(n<0){
n;
}
}
cout<<"\n\n";
decryption(n);
system("pause");
}
int decryption(int a){
int b[2][2]={{1,a},{0,1}};
int m;
ifstream inFile;
inFile.open ("out.txt");
while(!inFile.eof()) {
inFile>>m;
if(m<0)
break;
//cout <<m<<"\t";
for(int i;i<2;i++){
for(int j;j<2;j++){
cout<<m[i][j]<<"\t";
}
}
//multiply(m,b);
}
}
hi,
I was trying to store numbers read from a text file into 2D array but I am getting the error above.here is where the error occurs:
line 33: cout<<m[i][j]<<"\t";
Your 2D array is 'b'.
So if you want to store 'm' into 'b', you need to do an assignment:
b[i][j] = m;
'cout' will just output something to the console - it will not assign anything.
Also... you need to initialize your loop counters on lines 31 and 32 to zero, otherwise they will start with random garbage. Remember that C++ does not automatically zero new variables for you:
1 2 3 4
for(int i;i<2;i++){
// ^
// |
// Set this to zero!
EDIT:
Also... line 13 does nothing.... which makes that whole if block pointless:
1 2 3
if(n<0){
n; // ??? what are you trying to do here?
}
thanks. my mistake I didn't notice that on line 31 and 32 and it was not giving error when I compile it. what I am aiming to do is to multiply (m) after assigning it to 2D array by (b) matrix in order to decrypt a text file. but the problem is when I assign (m) to z[i][j], and write it inside two for loops, like this:
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
z[i][j]=m;
cout<<z[i][j]<<"\t";
}
}
it repeats the numbers inside the text file 4 times for each number.
#include <iostream>
#include<fstream>
int decryption(int);
usingnamespace std;
main(){
int n;
ifstream inFile;
inFile.open ("out.txt");
while(!inFile.eof()) {
inFile>>n;
if(n<0){
n;
}
}
cout<<"\n\n";
decryption(n);
system("pause");
}
int decryption(int a){
int b[2][2]={{1,a},{0,1}};
int m;
int z[2][2];
ifstream inFile;
inFile.open ("out.txt");
while(!inFile.eof()) {
inFile>>m;
if(m<0){
break;
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
z[i][j]=m;
cout<<z[i][j]<<"\t";
}
}
}
}
here are the codes again after modification. thanks for help.I appreciate it.
note: I haven't finished the compilation yet.I will multiply (b) by (z) after handling the repeatition issue explained above.