1,105,585 Community Members

string manipulation with stacks

Member Avatar
ace8957
Light Poster
27 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey Everyone!

So I'm a super beginner trying to teach myself the ins and outs of stacks (ever so slowly). Specifically, I'm trying to work out a programming challenge in which the objective is to write a function to tell if a string in the format of word1$word2 satisfies the condition of word2 being the reverse of word 1.

I believe that I have a decent enough stack class (I haven't implemented the exceptions properly yet, but that shouldn't be an issue with the test case I'm trying currently), as well as a function to perform the operations on the strings. I'm including both of these files in a main.cpp file.

The issue I am having is that, when I got to compile this program, I am hit with an error saying "multiple definition of 'stringcomp(std::string)'". Seeing as how I only declare the function stringcomp(string) in the file where I define it, I don't see how this is possible.

Does anyone have any input as to what I did wrong in this case? (files included below) I am a beginner, so I'm sorry if it seems painfully obvious or stupid.

stackA.h:

#include <string>
using namespace std;
typedef char stacktype;

class Stack{
    private:
    int top;
    int size;
    stacktype *arr;

    public:
    Stack(int size) {
        if (size <= 0) {
            throw string("Invalid stack size.");
        }
        arr = new stacktype[size];
        top = -1;
    }//end copy constructor

    void push(stacktype value) {
        if(top==size) {
            throw string("Stack storage overflow");
        }
        top++;
        arr[top]=value;
    }//end push

    stacktype peek() {
        if(top == -1) {
            throw string("Stack empty.");
        }
        return arr[top];
    }//end peek

    void pop() {
        if(top == -1) {
            throw string("Stack empty");
        }
        top--;
    }//end pop

    bool isEmpty() {
        return (top == -1);
    }//end isEmpty

    int getTop() {
        return top;
    }//end getTop


    ~Stack() {
        delete[] arr;
    }//end destructor

};//end stack class

stringrec.cpp (contains the stringcomp() function)

#include <iostream>
#include <string>
#include "stackA.h"
using namespace std;

void stringcomp(string str) {
    int i;
    bool inlang;//tells if the string is in the language
    Stack mystack(str.size());
    while(str[i] != '$')
    {
        mystack.push(str[i]);
        i++;
    }//end while loop

    //increment i to skip $
    i++;

    //match reverse of w as stipulated
    inlang = true;
    while(inlang && (i<str.size()))
    {
       // try
       // {
            mystack.pop();
            if(mystack.getTop() == str[i])
            {
                i++;
            }
            else {
                inlang = false;
            }
        //}//end try

       /* catch StackException
        {
            inlang=false;
        }//end catch

    */

    }//end while

    if(inlang && mystack.isEmpty()) {
        cout << "The string is in the language." << endl;
    }
    else {
        cout << "The string is not in the language." << endl;
    }

}//end function

main.cpp:

#include <iostream>
#include <string>
#include "stringrec.cpp"
using namespace std;

int main() {

	string mystring;
	mystring = "ABC$CBA";
	stringcomp(mystring);

	return 0;
}
Member Avatar
myk45
Posting Pro in Training
417 posts since Sep 2010
Reputation Points: 48 [?]
Q&As Helped to Solve: 50 [?]
Skill Endorsements: 0 [?]
 
0
 

First: Its never a good idea to include a c/cpp file.

This is better:-

say
file.h is the header. The corresponding C++ file will be file.cpp

So, you include the header file and the C++ file is included in the project.

The files are as follows:

1)main.cpp

#include <iostream>
#include <string>
#include "st.h"
using namespace std;

int main() {

	string mystring;
	mystring = "ABC$CBA";
	stringcomp(mystring);

	return 0;
}

2)st.h

#ifndef STH
#define STH

#include <string>
using namespace std;
typedef char stacktype;

class Stack{
    private:
    int top;
    int size;
    stacktype *arr;

    public:
    Stack(int size) {
        if (size <= 0) {
            throw string("Invalid stack size.");
        }
        arr = new stacktype[size];
        top = -1;
    }//end copy constructor

    void push(stacktype value) {
        if(top==size) {
            throw string("Stack storage overflow");
        }
        top++;
        arr[top]=value;
    }//end push

    stacktype peek() {
        if(top == -1) {
            throw string("Stack empty.");
        }
        return arr[top];
    }//end peek

    void pop() {
        if(top == -1) {
            throw string("Stack empty");
        }
        top--;
    }//end pop

    bool isEmpty() {
        return (top == -1);
    }//end isEmpty

    int getTop() {
        return top;
    }//end getTop


    ~Stack() {
        delete[] arr;
    }//end destructor

};//end stack class

/////////////////////////////////////////////////////
void stringcomp(string str);// Prototype added here.
////////////////////////////////////////////////////

// sorry for making it shabby. just wanted to draw your attention to that :)

#endif

3)st.cpp

#include <iostream>
#include <string>
#include "st.h"
using namespace std;

void stringcomp(string str) {
    int i;
    bool inlang;//tells if the string is in the language
    Stack mystack(str.size());
    while(str[i] != '$')
    {
        mystack.push(str[i]);
        i++;
    }//end while loop

    //increment i to skip $
    i++;

    //match reverse of w as stipulated
    inlang = true;
    while(inlang && (i<str.size()))
    {
       // try
       // {
            mystack.pop();
            if(mystack.getTop() == str[i])
            {
                i++;
            }
            else {
                inlang = false;
            }
        //}//end try

       /* catch StackException
        {
            inlang=false;
        }//end catch

    */

    }//end while

    if(inlang && mystack.isEmpty()) {
        cout << "The string is in the language." << endl;
    }
    else {
        cout << "The string is not in the language." << endl;
    }

}//end function
Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

try putting void stringcomp(string str); right before "int main()".
Also I hope you got a "stringrec.h" that has the prototype of the functions defined in "stringrec.cpp"

Member Avatar
ace8957
Light Poster
27 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks guys. I made changes to reflect what you guys said and it appears that I did something incorrectly either with that or somewhere else, as it now tells me "error: 'stringcomp' was not declared in this scope" at the call inside the main function.

Member Avatar
ace8957
Light Poster
27 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Looks like I've found the issues! Thanks again, I would have been really lost without you guys.

Question Answered as of 3 Years Ago by firstPerson and myk45
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: