Logic error in my program. Member function containing string not valid?

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
114
115
116
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
class MyClass
{
private:
int num = 0;
std::string description = 0;

public:
MyClass();
MyClass(int n1, std::string const& n) : num(n1), description(n){}  
int GetNum();
void SetNum(int* Pointer);
string GetDesc();
void SetDesc(string* Pointer2);
int Square();
double SquareRoot();
int Factorial();
bool IsNegative();
};

MyClass::MyClass(){
//nothing
}

    int MyClass::GetNum()
        {
            return num;
        }
        void MyClass::SetNum(int* Pointer)
            {
                *Pointer = num;
            }
                    string MyClass::GetDesc()
            {
            return description;
                }
                    void MyClass::SetDesc(string* Pointer2)
                { 
                    *Pointer2 = description;
                }
                        int MyClass::Square()
                            {
                                return(num * num);
                            }
                            double MyClass::SquareRoot()
                            {
                                {
                                    if (num > 0)
                                    return sqrt(num);

                                    else
                                    return 0;
                                }
                            }

                                int MyClass::Factorial()
                                 {
                                   if (num >= 0 )
                                   {
                                      int result = 1;
                                      for (int a = 1; a <= num; a++)
                                      result = result * a;
                                      return result;
                                    }
                                     else 
                                     return 0;
                                      }


                                        bool MyClass::IsNegative()
                                        {return (num < 0);
                                        }   
void Display(MyClass b)
{
cout << b.GetNum() << endl;
cout << b.GetDesc() << endl;
cout << b.Square() << endl;
cout << b.SquareRoot() << endl;
cout << b.Factorial() << endl;
cout << b.IsNegative() << endl;
}

int main () 
{ 
bool flag = false;
char str1[100], str2[100];

cin >> str1 >> str2;
for (size_t i=0, len = strlen(str1); i < len; i++)
{
    if (i==0 && str1[i] == '-')
        continue;
    if (!isdigit(str1[i]))
    {
        flag = true;
        break;
    }
 }

if (!flag)
{
    MyClass b(atoi(str1), str2); 
    Display(b);
}
else
{
{
    MyClass b; 
    Display(b);
}

return 0;
}





ok So I changed my program but there is another problem. Here is my next sample input: NONE Modern. But I received a message: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid Abort. The sample output is supposed to be: 0 0 0 1 0 There might be a problem with my string statement. Im not sure.
Last edited on
std::string description = 0;
This makes no sense. Try
std::string description;



Line 89. Why are you using char arrays? I know you know what a C++ string is, because I can see you using them in your code. So why are you making this hard for yourself by using char arrays?

Arrays are for advanced programmers. You should be using strings, not char arrays.
Last edited on
Ok when I changed it to std::string description; I received a compiler error 'MyClass::description' should be initialized in the member initialization list [-Weffc++]
MyClass::MyClass(){

I thought I initialized description but putting std::string?
Never mind! I had to initialize description to std::string description = "";
Thank you so much for your help!
I received a compiler error 'MyClass::description' should be initialized in the member initialization list [-Weffc++]
MyClass::MyClass(){


That's a compiler warning. You should still listen to it, but there's a big difference between a warning and an error. In this case, the warning is wrong.

The default constructor of std::string creates a sensible string object in a good state. std::string description = ""; is not necessary. if it makes you feel better, go ahead, but don't code by simply guessing what to do until the compiler stops warning you.

See this SO post, in which people discuss the exact case of this warning about a std:string: https://stackoverflow.com/questions/14002454/can-i-ignore-the-gcc-warning-foom-bar-should-be-initialized-in-the-member-i#14002558
Last edited on
Topic archived. No new replies allowed.