Hello cplusbeginner2222,
In "main" you have:
B.is_divisible_by("302",10);
. This calls a function that returns a "bool", but you do not capture the returned value or make use if it.
In your function:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
bool is_divisible_by(string s, int x)
{
char n = s.size();
if (n < x)
{
return 1;
}
else
{
return 0;
}
}
|
On line 3 you define a "char" and set it to the value of the ".size()" function. The problem is that ".size()" returns a "size_t" variable type. This could be an "unsigned int" or an "unsigned long". It depends on how your header files define "size_t". In the end you are setting a "char" to an "unsigned integer" type.
See
https://en.cppreference.com/w/c/types/size_t
In line 5 you are comparing a char to an int. Not a good idea. It may not return the result that you want.
What you need to do is convert the string to an int then use "% 10". if it returns (0)zero than it is dividable be 10 or what ever value "x" is.
"n", "x" cute, but give these variables a proper name that describes what it is. You will be doing your-self a big favor now and in the future. If you make your code easy to understand it helps when looking for errors.
Between the close of the class and the beginning of "main" you have to many blank lines. 1 is sufficient.
Your class is in dire need of some blank lines to make it easier to read.
As an idea:
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
|
class Base4int
{
private: // <--- This section is private by default until changed.
Node *head;
Node *tail;
public:
Base4int()
{
head = NULL;
tail = NULL;
}
void generate(string s)
{
char n = s.size();
for (int i = 0; i < n; i++)
{
Node* newnode = new Node();
newnode->data = s[i];
if (head == NULL)
{
head = newnode;
newnode->next = NULL;
}
Node* itr = head;
while (itr->next != NULL)
{
itr = itr->next;
}
itr->next = newnode;
newnode->next = NULL;
}
}
void show()
{
Node *itr = head;
while (itr != NULL)
{
cout << "[" << itr->data << "->" << itr->next << "]";
itr = itr->next;
}
cout << endl;
}
bool is_divisible_by(string s, int x)
{
char n = s.size();
if (n < x)
{
return 1;
}
else
{
return 0;
}
}
};
|
And watch your indenting.
Andy