Skip to content

[FEAT] first solution for stack problem in cpp#52

Open
AndreaOrlando23 wants to merge 5 commits into
tomorrowdevs-projects:mainfrom
AndreaOrlando23:solution/stack-cpp
Open

[FEAT] first solution for stack problem in cpp#52
AndreaOrlando23 wants to merge 5 commits into
tomorrowdevs-projects:mainfrom
AndreaOrlando23:solution/stack-cpp

Conversation

@AndreaOrlando23

Copy link
Copy Markdown
Contributor

My first attempt for the stack problem in cpp :)

@AndreaOrlando23

AndreaOrlando23 commented Oct 9, 2021

Copy link
Copy Markdown
Contributor Author

I have refactored the code according to Gabriele's advices :)

int x = 0;
if(isEmpty()) {
cout << "Stack is Empty." << endl;
return -1; //we have to reurn some value couse of the int function

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in cpp the int value is also negative from −2,147,483,648 to 2,147,483,647
it means that -1 is a possible result.
So this code isn't correct, you need to find another way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've tried to implement a workaround to manage this issue. Thanks!

}

bool isEmpty() {
if(myStack.size() == 0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a way to use one line code here something like: return myStack.size() == 0 or similar

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

}

void display() {
if (isEmpty())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove:

  if (isEmpty())
            cout << "|None";

when you display values, you don't need to specify that it is empty

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

int x = 0;
if(isEmpty()) {
cout << "Stack is Empty. The values in stack are: ";
return 0; //we have to return some value couse of the int function

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 is part of the int so it is a valid value.
You need to find another way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I’ve tried to find a workaround using different cout << but, as you suggest in the next comment, I think that the problem is here: I need to remove all the cout << from my class

return myStack.at(myStack.size() - 1);
}
else
cout << "Stack is Empty. The values in stack are: ";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have to remove all the cout << in your class.
The output print is very slow and has nothing to do with the class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I’ll work on it. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants