Reputation: 33
So as an assignment I have to get a string from the user and turn it into a list and then check parentheses balance. However, I keep getting `warning: control reaches end of non-void function [-Wreturn-type]
#include<iostream>
#include<string>
#include<stacks>
class Node{
public:
char data;
Node *head;
Node *tail;
Node *next;
Node *node;
void addToList(char);
bool CheckList();
void StringToList(string, Node*);
};
Node* addToList(char data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
};
Node* StringToList(string text, Node* head)
{
head = addToList(text[0]);
Node *CurrentNode = head;
for (int i = 1; i <text.size(); i++){
CurrentNode->next = addToList(text[i]);
CurrentNode = CurrentNode->next;
}
return head;
}
bool CheckList(Node* head)
{
char c;
stack <char> p;
int i = 0;
Node* CurrentNode = head;
while(CurrentNode != NULL){
if('(' || '{' || '['== CurrentNode->data){
p.push(CurrentNode->data);
if(')' == CurrentNode->data){
c= p.top();
p.pop();
if (c == '{' || c == '['){
return false;
}
}
else if('}' == CurrentNode->data){
c= p.top();
p.pop();
if (c == '(' || c == '['){
return false;
}
}
else if('}' == CurrentNode->data){
c= p.top();
p.pop();
if (c == '(' || c == '['){
return false;
}
}
}
}
CurrentNode = CurrentNode->next;
}
int main()
{
string text = "(check)[";
Node *head = NULL;
head = StringToList(text, head);
if(CheckList(head)){
cout<<"MAMA MIA IT WORKED-A!";
}
else
cout<<"IT'S-A STILL WORKING!!!";
return 0;
}
Any help would be appreciated. Thank you again for your time. Also, sorry if my code looks a bit messy but im kind of new to stacks and lists.
Upvotes: 3
Views: 211
Reputation: 27567
Your function:
bool CheckList(Node* head)
doesn't have a return
at the end:
}
CurrentNode = CurrentNode->next;
}
you must return something:
}
CurrentNode = CurrentNode->next;
return /* an appropriate boolean value */;
}
Upvotes: 3
Reputation: 22152
CheckList
claims to return a bool
, but after the while
loop and
CurrentNode = CurrentNode->next;
you don't have a return
statement. The function just ends without returning a value. If a call to a function with non-void
return type ends without a return
statement (or prior exit via exception), the program has undefined behavior. The compiler is warning you about this possibility.
You need to add a return
statement at the end of the function, with the value that you want to return. Given that the other return
statements in the function all return false
, I would assume that you intend to return true
if the whole loop executed without returning false
:
CurrentNode = CurrentNode->next;
return true;
Upvotes: 3