Problem with this program

Pages: 12
I have been studying classes from a few weeks and now I want to apply the theory of what I studied so far.I made this program .CAn someone tell me what is wrong with it and how can I make it workable.


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;
class Reverse
{
    int c;
    int b;
    
int result=0,k;
   int a=0,i;
public :
int reverse(unsigned int b);
}




int Reverse::reverse(unsigned int b)
{
   
  b=a;
   for(;c/10!=0;c/=10)   
       a++;

   for(;b/10!=0;b/=10,a--)
     {
        k=b%10;
        for(i=0;i<a;i++)
          {
             k*=10;
          }
         result+=k;
     }
result = result+b%10;
   return result;

}

int main()
{
    int input;
    char ch;


do
{
    Reverse funct;

    cout<<"enter numner";
    cin>>input;
    cout<<"the reverse is "<<funct.reverse(input);
    cout<<endl;
    cout<<"enter c to continue and q to quit the program";
cin>>ch;
    } while (ch=='c');
    return 0;
}
Initializing data members is only allowed in constructor unless static constant.
closed account (S6k9GNh0)
I recently ran into some code that had this problem as well. You can declare members that are made when you have a class and then you can assign member values when you construct the class itself. This is very easily done through Initializer Lists.

1
2
3
4
5
6
7
8
9
10
class Reverse
{
    int c;
    int b;
    
   int result, k;
   int a ,i;
public :
   Reverse(unsigned int) : result(0), a(0) {} //NOTICE: int reverse isn't valid for a constructor
}

Also notice that I change the function int reverse. The reason being is because this is your constructor which 1) Does not return a value 2) Is required.
Last edited on
@ kevinchkin and compterquip !!!
So you mean I should just declare the variables in private section of the class not initialize it .
what else should I do ? Tactful criticism is very welcome.


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
using namespace std;
class Reverse
{
    int c;
    int b;
    
   int result, k;
   int a ,i;
public :
   Reverse(unsigned int) : result(0), a(0); 
}



int Reverse::reverse(unsigned int b)
{
   
  b=a;
   for(;c/10!=0;c/=10)   
       a++;

   for(;b/10!=0;b/=10,a--)
     {
        k=b%10;
        for(i=0;i<a;i++)
          {
             k*=10;
          }
         result+=k;
     }
result = result+b%10;
   return result;

}

int main()
{
    int input;
    char ch;


do
{
    Reverse funct;

    cout<<"enter numner";
    cin>>input;
    cout<<"the reverse is "<<funct.reverse(input);
    cout<<endl;
    cout<<"enter c to continue and q to quit the program";
cin>>ch;
    } while (ch=='c');
    return 0;
}
Last edited on
Yes if you want to initialize a data member then use initializer list as mentioned by computerquip.

Right now there are few problems in your code.

Invalid constructor int Reverse(unsigned int b); // This is constructor and constructor does not have a return type.

No semicolon after class declaration.

And initializer list can be used while providing definition of constructor and not while declaring it. Unless ofcourse, you are defining it at the same place you declared it.

When line 45 will be executed it will throw an error because you have not declared a default constructor. There is only one constructor in class right now which is Reverse (unsigned int); . So technically line 45 should be
Reverse funct(1); // since parameterized constructor
However, if you want to declare new object like you did right now then add the default constructor too i.e. constructor without any parameters Reverse() : result(0), a(0){}

Other than that, I don't know how you want to initialize 'result' and 'a'. Right now I don't see any use of the parameterized constructor in your code, well, unless you want to initialize 'result' and 'a' with the same value like this Reverse (unsigned int v) : result(v), a(v) { } This will assign result and a both to v, (this v you will supply while creating the object)

Or other use could be Reverse (unsigned int a, unsigned int b) : result(a), a(b) { }
This will initialize result to a and a to b.

Hope this helps !
Last edited on
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
using namespace std;
class Reverse
{
    int c;
    int b;
    
   int result, k;
   int a ,i;
public :
reverse(){};

   Reverse(unsigned int) : result(0), a(0)
}



Reverse::reverse(unsigned int b)
{
   
  b=a;
   for(;c/10!=0;c/=10)   
       a++;

   for(;b/10!=0;b/=10,a--)
     {
        k=b%10;
        for(i=0;i<a;i++)
          {
             k*=10;
          }
         result+=k;
     }
result = result+b%10;
  

}

int main()
{
    int input;
    char ch;


do
{
    Reverse funct;

    cout<<"enter numner";
    cin>>input;
    cout<<"the reverse is "<<funct.reverse(input);
    cout<<endl;
    cout<<"enter c to continue and q to quit the program";
cin>>ch;
    } while (ch=='c');
    return 0;
}



Is it better ?
NO. Well, I am not sure what you are trying to do. Did you read my previous post completely? because I don't see the changes I mentioned before.
I don't know what to tell you kevin - I think you said it pretty clear. His emailed me about this program X amount of times. I've given him the solution and he still has no idea about it. So I'd gather he is still unsure of classes.

You should go read up about them on the tutorials on this site or cprogramming.com.
mythois !!
Last edited on
hahahahaha...

poor Mythios...

i have been to this before.. :(
closed account (z05DSL3A)
masiht, I have made some comments in your original code:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
using namespace std;

class Reverse
{
    int c;
    int b;
    
    int result /* =0 */,k; // result=0 can not asign values to non static
    int a /* =0 */,i;      // members in the declaration
public :
    int reverse(unsigned int b);
}; // was missing an ;


int Reverse::reverse(unsigned int b)  //this function just does not work...
{   
    b=a;  // what b is this? the member var or the function input?
          // a is never asigned a value
    
    for(; c/10 != 0; c/= 10) // c is not asigned a value  
       a++;

    for(; b/10!=0; b/=10, a--) // what b is this?
    {
        k=b%10;
        for(i=0; i<a; i++)
        {
            k*=10;
        }
        result+=k;
    }
    result = result + b % 10;
   
    return result;
}

int main()
{
    int input;
    char ch;
    
    do
    {
        Reverse funct;

        cout<<"enter numner";
        cin>>input;
        cout << "the reverse is " << funct.reverse(input) << endl;

        cout<<"enter c to continue and q to quit the program";
        cin>>ch;

    } while (ch=='c');
    
    return 0;
}


Here is some code to do the sort of thing you are attempting, hope you find it useful:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<iostream>

/*****************************************************************************/
class Reverse
{
public :
    static int reverse(unsigned int);
};

/*---------------------------------------------------------------------------*/
int Reverse::reverse(unsigned int input)
{   
    int digitCount = 1;
    int remainder  = 0;
    int result     = 0;
        
    for (unsigned int c = input; c/10 != 0; c/= 10)
    {
       ++digitCount;
    }

    for (int i = 0; i < digitCount; ++i) 
    {
        result   *= 10;
        remainder = input % 10;
        result   += remainder;
        input    /= 10;
    }

    return result;
}

/*****************************************************************************/
class UserNumber
{
public:
    // Constructor(s)
    UserNumber(unsigned int number = 0);
    
    // Accessor(s)
    inline unsigned int  number() const { return m_Number; }
    inline unsigned int &number()       { return m_Number; }

    // Method(s)
    int reverse();
private:
    unsigned int m_Number;
};

/*---------------------------------------------------------------------------*/
UserNumber::UserNumber(unsigned int number)
: m_Number(number)
{
}
/*---------------------------------------------------------------------------*/
int UserNumber::reverse()
{
    int digitCount = 1;
    int remainder  = 0;
    int result     = 0;
    int input      = m_Number;
        
    for (unsigned int c = input; c/10 != 0; c/= 10)
    {
       ++digitCount;
    }

    for (int i = 0; i < digitCount; ++i) 
    {
        result   *= 10;
        remainder = input % 10;
        result   += remainder;
        input    /= 10;
    }

    return result;
}
/*****************************************************************************/
int main(int argc, char *argv[])
{
    using std::cout;
    using std::cin;
    using std::endl;

    int input;
    char ch;
    
    do
    {
        cout << "Please enter a numner: ";
        cin >> input;

        cout << "The reverse is " << Reverse::reverse(input) << endl;

        UserNumber userNumber(input);
        cout << "The reverse of " << userNumber.number() 
             << " is "<< userNumber.reverse() << endl;
    
        userNumber.number() = userNumber.reverse();

        cout << "The reverse of " << userNumber.number() 
             << " is "<< userNumber.reverse() << endl;

        
        cout << "enter c to continue and q to quit the program ";
        cin >> ch;

    } while(ch == 'c');
    
    return 0;
}

/* End of file ***************************************************************/


Last edited on
Thank you writetonsharma ! for what you said about me .
Can you give any comments on my original program ?


@grey wolf !
you made my program a little weired .I dont think you need two classes for this program and your output is very very strange.

@ kevinchkin !! thanks for your comments,
I tried exactly what you told me to do.I dont know how to do some of the things that you wrote in the comments.

Last edited on
closed account (z05DSL3A)
masiht, you don't need the two classes for the program. The reason two classes was to illustrate two approaches to the solution to your needs.

The first class (Reverse) is more like your original code, the second class stores some data for you, operates on that data.
masiht:

i am not following this post as some good people are already helping you. Adding something will lead to confusion and hence i am good being silent. :)
closed account (S6k9GNh0)
Here's how I would do it:
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
#include<iostream>
using namespace std;

reverse(unsigned int b)
{
   //Code for reversing.
}

int main()
{
   	int input;
    	char ch;


	do
	{

    		cout << "Please enter a number.";
    		cin >> input;
    		cout << "The reverse of " << input << " is " << reverse(input);
    		cout << endl;
    		cout << "Enter c to continue: ";
		cin >> ch;
    	} 
	while (ch=='c');
    	return 0;
}

C-style but seriously, you don't needed classes for everything.
Last edited on
closed account (z05DSL3A)
...you don't needed classes for everything


no but the OP has"...been studying classes from a few weeks and now I want to apply the theory", so giving a non class based example is pointless. Admittedly the example is not best suited to a class, but such is life...
Last edited on
closed account (S6k9GNh0)
Well this is more of a function of a class or just a global function. Try creating something more object like. A number wrapper or a string wrapper or something similar. What I'm trying to explain is that basing an ENTIRE CLASS on one function isn't something that should be done. All of the members of the class used above should actually be declared inside of the Reverse function itself since they aren't needed to be kept except maybe result. But then again result can be the return value of a global function. It's not class specific so in the end you'll just be left with a constructor.
Last edited on
closed account (z05DSL3A)
Errr...hell, it must be international can't be bothered to read posts properly day or something.
Actually, OP has been "studying" (ie, posting questions and letting people write the code for him/her) for at least 4-5 months now.
Yes ! I have no idea what OP is trying to do . By the way, who is this OP and what is the full form of his name ?


@gery wolf !
Here is how your program should look like.


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
using namespace std;

class UserNumber       //Use one class for simplicity.
{
private:
    unsigned int m_Number;
public:
    static int reverse(unsigned int);
    UserNumber(unsigned int number = 0);
    inline unsigned int  number() const { return m_Number; }
    inline unsigned int &number()       { return m_Number; }
    int reverse();

};
int UserNumber::reverse(unsigned int input)
{   
    int digitCount = 1;
    int remainder  = 0;
    int result     = 0;
        
    for (unsigned int c = input; c/10 != 0; c/= 10)
    {
       ++digitCount;
    }

    for (int i = 0; i < digitCount; ++i) 
    {
        result   *= 10;
        remainder = input % 10;
        result   += remainder;
        input    /= 10;
    }

    return result;
}
UserNumber::UserNumber(unsigned int number)
: m_Number(number)
{
}

int main()   // make the program easy to understand.
{
   int input;
    char ch;
    
    do
    {
        cout << "Please enter a numner: ";
        cin >> input;

        cout << "The reverse is " << UserNumber::reverse(input) << endl;  //Don't confuse the user.
        cout << "enter c to continue and q to quit the program ";
        cin >> ch;

    } while(ch == 'c');
    
    return 0;
}
Last edited on
Pages: 12