what is the probelm with this code?

It does not run and I cannot find the problem. Why does not it run?

#include "stdafx.h"
#include <iostream>
using namespace std;
class q {
public:
void v();
void p();
const int a=5,b=3;
int y,z,i,j;
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])
o=x[i][j];
}
cout<<"o"<<o;}
Last edited on
What do you mean by "does not run"?


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;
}
Thanks,
When I run ,I receive "unable to start program". Why do you write x[i][undefined]?
Can you write the correct form of the program?
Can any one help me?
here's your code in codetags
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "stdafx.h"
#include <iostream>
using namespace std;
class q {
public:
void v();
void p();
const int a=5,b=3;
int y,z,i,j;
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])
o=x[i][j];
}
cout<<"o"<<o;}


here's how to do it in future
http://www.cplusplus.com/articles/jEywvCM9/

your first problem is in line 8

You can't assign a value directly like this you have to use a constructor

Hope you can get through now
Some reasonable indentation helps to see problems.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//  #include "stdafx.h"     //  Not needed
#include <iostream>
using namespace std;

class q 
{
public:
    void v();
    void p();
    static const int 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.
Last edited on
5
6
7
8
9
10
11
12
13
class q 
{
public:
    ...
    ...
    static const int a=5,b=3; // constants are declared
    ... 
    int x[5][3];              // and then ignored
};

The code should make use of the constants throughout. Also, give them meaningful names such as rows, cols or similar.
Topic archived. No new replies allowed.