In: Computer Science
Please fix these errors C++
In file included from main.cpp:14:0: Inventory.h:1:0: error: unterminated #ifndef #ifndef INVENTORY_H_ main.cpp: In function ‘int main()’: main.cpp:82:35: error: ‘adds’ was not declared in this scope noun= adds(noun); ^ main.cpp:88:32: error: ‘display’ was not declared in this scope display(noun); ^ In file included from Inventory.cpp:4:0: Inventory.h:1:0: error: unterminated #ifndef #ifndef INVENTORY_H_
----------------------------------------------------------------------------------
CODE: main.cpp
#include<iostream>
#include<string>
#include<vector>
#include "Inventory.h"
using namespace std;
int main()
{
Node * noun=NULL;
Node * verb=NULL;
int choice=0;
while(choice!=8)
{
cout<<"1. Push Noun\n";
cout<<"2. Pop Noun\n";
cout<<"3. Push Verb\n";
cout<<"4. Pop Verb\n";
cout<<"5. Concatenate\n";
cout<<"6. Add an s\n";
cout<<"7. Display Both Stacks\n";
cout<<"8. Exit\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"Enter a noun: ";
string n;
cin>>n;
noun = push(noun,n);
}
break;
case 2:
{
cout<<"NOUN POPPED: ";
Node * t = pop(&noun);
if(t!=NULL)
cout<<t->data<<endl;
}
break;
case 3:
{
cout<<"Enter a verb: ";
string n;
cin>>n;
verb = push(verb,n);
}
break;
case 4:
{
cout<<"Verb POPPED: ";
Node * t = pop(&verb);
if(t!=NULL)
cout<<t->data<<endl;
}
break;
case 5: {
cout<<"Top two words on noun stack concatenated\n";
noun = concat(noun);
}
break;
case 6:
{
cout<<"Add s to the top word on noun stack\n";
noun = adds(noun);
}
break;
case 7:
{
cout<<"NOUN STACK\n";
display(noun);
cout<<"VERB STACK\n";
display(verb);
}
case 8:
{
}
break;
default: cout<<"Enter a valid choice\n";
}
}
return 0;
}
CODE: Inventory.cpp
#include<iostream>
#include<string>
#include<vector>
#include "Inventory.h"
using namespace std;
//display
void display(Node * head)
{
if(head==NULL)
{
return;
}
vector<string> stack;
Node * cur = head;
while(cur!=NULL)
{
stack.push_back(cur->data);
cur=cur->next;
}
for(int i = stack.size()-1;i>=0;--i)
{
cout<<stack.at(i)<<" ";
}
cout<<endl;
}
Node * adds(Node *head)
{
Node * top1 = pop(&head);
if(top1!=NULL)
{
//add s
string sol= top1->data + "s";
head=push(head,sol);
}
return head;
}
CODE: inventory.h
#ifndef INVENTORY_H_
#define INVENTORY_H_
#include<iostream>
using namespace std;
class Node
{
public:
string data;
Node * next;
Node()
{
data="";
next=NULL;
}
Node(string data)
{
data=data;
next=NULL;
}
};
Node * push(Node * head, string data)
{
if(head==NULL)
{
head=new Node();
head->data=data;
return head;
}
Node * cur = head;
while(cur->next!=NULL)
{
cur=cur->next;
}
Node * temp = new Node();
temp->data=data;
cur->next=temp;
return head;
}
Node * pop(Node ** h)
{
Node * head = *h;
if(head==NULL)
return NULL;
if(head->next==NULL)
{
Node * temp = head;
*h = NULL;
return temp;
}
Node * cur = head;
Node * prev = head;
while(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
prev->next=NULL;
return cur;
}
Node * concat(Node * head)
{
Node * top1 = pop(&head);
Node * top2 = pop(&head);
if(top1!=NULL && top2!=NULL)
{
string solution = top1->data + top2->data;
head= push(head,solution);
}
return head;
}
CODE:
main.cpp :
Inventory.cpp :
Inventory.h :
OUTPUT:
RAW CODE:
main.cpp :
#include<iostream>
#include<string>
#include<vector>
#include "Inventory.h"
#include "Inventory.cpp" // add it
using namespace std;
int main()
{
Node * noun=NULL;
Node * verb=NULL;
int choice=0;
while(choice!=8)
{
cout<<"1. Push Noun\n";
cout<<"2. Pop Noun\n";
cout<<"3. Push Verb\n";
cout<<"4. Pop Verb\n";
cout<<"5. Concatenate\n";
cout<<"6. Add an s\n";
cout<<"7. Display Both Stacks\n";
cout<<"8. Exit\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"Enter a noun: ";
string n;
cin>>n;
noun = push(noun,n);
}
break;
case 2:
{
cout<<"NOUN POPPED: ";
Node * t = pop(&noun);
if(t!=NULL)
cout<<t->data<<endl;
}
break;
case 3:
{
cout<<"Enter a verb: ";
string n;
cin>>n;
verb = push(verb,n);
}
break;
case 4:
{
cout<<"Verb POPPED: ";
Node * t = pop(&verb);
if(t!=NULL)
cout<<t->data<<endl;
}
break;
case 5: {
cout<<"Top two words on noun stack concatenated\n";
noun = concat(noun);
}
break;
case 6:
{
cout<<"Add s to the top word on noun stack\n";
noun = adds(noun);
}
break;
case 7:
{
cout<<"NOUN STACK\n";
display(noun);
cout<<"VERB STACK\n";
display(verb);
}
case 8:
{
}
break;
default: cout<<"Enter a valid choice\n";
}
}
return 0;
}
Inventory.cpp :
#include<iostream>
#include<string>
#include<vector>
#include "Inventory.h"
using namespace std;
//display
void display(Node * head)
{
if(head==NULL)
{
return;
}
vector<string> stack;
Node * cur = head;
while(cur!=NULL)
{
stack.push_back(cur->data);
cur=cur->next;
}
for(int i = stack.size()-1;i>=0;--i)
{
cout<<stack.at(i)<<" ";
}
cout<<endl;
}
Node * adds(Node *head)
{
Node * top1 = pop(&head);
if(top1!=NULL)
{
//add s
string sol= top1->data + "s";
head=push(head,sol);
}
return head;
}
Inventory.h:
#ifndef INVENTORY_H_
#define INVENTORY_H_
#include<iostream>
using namespace std;
class Node
{
public:
string data;
Node * next;
Node()
{
data="";
next=NULL;
}
Node(string data)
{
data=data;
next=NULL;
}
};
Node * push(Node * head, string data)
{
if(head==NULL)
{
head=new Node();
head->data=data;
return head;
}
Node * cur = head;
while(cur->next!=NULL)
{
cur=cur->next;
}
Node * temp = new Node();
temp->data=data;
cur->next=temp;
return head;
}
Node * pop(Node ** h)
{
Node * head = *h;
if(head==NULL)
return NULL;
if(head->next==NULL)
{
Node * temp = head;
*h = NULL;
return temp;
}
Node * cur = head;
Node * prev = head;
while(cur->next!=NULL)
{
prev=cur;
cur=cur->next;
}
prev->next=NULL;
return cur;
}
Node * concat(Node * head)
{
Node * top1 = pop(&head);
Node * top2 = pop(&head);
if(top1!=NULL && top2!=NULL)
{
string solution = top1->data + top2->data;
head= push(head,solution);
}
return head;
}
#endif // add it
NOTE:
If You have any doubts feel free to comment in comment
section.
DO VOTE(LIKE).