PS. Have you noticed the code tags <> option? Posting formatted code looks good, like this:
1 2 3 4 5 6 7 8 9 10
void q::p(){
int o=0;
for(int i=0;i<a;i++)
{
for( int j=0; j<b; j++) o = x[0][0]; // set o=x[0][0] b times
if( o < x[i][j] ) o = x[i][j]; // if x[0][0] < x[i][undefined]
}
cout<<"o"<<o;
}
// #include "stdafx.h" // Not needed
#include <iostream>
usingnamespace std;
class q
{
public:
void v();
void p();
staticconstint a=5,b=3;
int y,z,i,j; // Don't define local variables here
int x[5][3];
};
int main()
{ q g;
g.v();
g.p();
cin.get();
cin.get();
}
void q::v()
{ for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
{ cout<<"\t";
cin>>x[i][j];
cout<<"\n";
}
}
void q::p()
{ int o=0;
for(int i=0;i<a;i++)
{ for(int j=0;j<b;j++)
o=x[0][0];
if(o<x[i][j]) // Uses q::j, not loop variable j
o=x[i][j];
}
cout<<"o"<<o;
}
Line 37-38: j is out of scope. j goes out of scope after line 36. Compiler uses q::j instead, which is uninitialized. You might want to use {} for the for loop at line 35.
Line 11: You shouldn't be defining local variables (i,j) here.
programmer007 wrote:
You can't assign a value directly like this you have to use a constructor
Yes you can, if you're using C++14 or if the variables are static.