#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;
}
I am receiving no more compiler errors, but there is a logic problem in my program when it produces an output. Please help me track down the illogical error in my program.
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 how to fix it!
It looks like you're new here. If you want to get involved, click one of these buttons!