#include <iostream>
#include <cmath>
#include <cstring>
usingnamespace 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);
elsereturn 0;
}
}
int MyClass::Factorial()
{
if (num >= 0 )
{
int result = 1;
for (int a = 1; a <= num; a++)
result = result * a;
return result;
}
elsereturn 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.
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.
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?
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.