Solved

palindrome

Posted on 2004-09-22
1
1,866 Views
Last Modified: 2008-01-09
how can compile with makefile without using util.h .cpp, life.h...life.cpp

This code stops at palidrome....how can this code be fixed to run properly?????????????  I am stuck!!!!!!

// main.cpp
#include <stack>
#include <string>
#include <iostream>

using namespace std;

#define SIZE 100
typedef char item_t;

typedef struct {
     item_t Items[SIZE];
     int top;
}Stack_t;

typedef struct{
     int count,front, rear;
     item_t Item[SIZE];
}Queue_t;

void InitialiseStack (Stack_t *S)
{     S->top=0;     }

int emptystack (Stack_t S)
{     return (S.top==0);     }

int fullstack (Stack_t S)
{     return (S.top==SIZE);     }


void push (char x,Stack_t *S)
{
     if(fullstack(*S))
          printf("stack overflow\n");
     else{
               S->Items[S->top]=x;
               ++(S->top);
     }
}

void pop (Stack_t *S, char *x)
{
     if(emptystack(*S))
          printf("stack empty\n");
     else{
          --(S->top);
          *x=S->Items[S->top];
     }
}


template <class T> void stackMenu(T data);
void stackMenu();

int main()
{
//   int option;
 
   Stack_t S;
     // Queue_t Q;
   item_t input[SIZE] = { 0 };
   item_t Q_output[SIZE]="",S_output[SIZE]="";
     int i,length;

     InitialiseStack(&S);
     //InitialiseQueue(&Q);

     printf("Enter a words to reverse> ");
     for(i=0,input[i]=getchar();input[i]!='\n';i++,input[i]=getchar())
     {
          // insert(input[i],&Q);
          push(input[i],&S);
     }

     length=strlen(input)-1;   // we don't need final '/n'

     for(i=0;i<length;i++)
     {
          //while(!emptystack(S)){
               // Remove(&Q,&Q_output[i]);
               pop(&S,&S_output[i]);
          // }
     
          if(input[i]!=S_output[i])
          {
               printf("\nThe words are not palindrome\n");
               return 0;
          }

          else /* if(Q_output[i]==S_output[i]) */
               cout << S_output[i];
     }
     
     putchar('\n');
     return 0;
}

void stackoptions()
{
   cout << "1: Push\n";
   cout << "2: Pop\n";
   cout << "3: Top\n";
   cout << "4: Quit\n\n";
   cout << "Enter Option: ";
}


template <class T> void stackMenu(T data)
{
   stack<T> mystack;  // create the right stack
   stackoptions();
   int option;
   cin >> option;

   while (option != 4)
   {
      if (option == 1)
         {
            cout << "enter data";
            cin >> data;
            mystack.push(data);
            cout << "Stack Added Value " << data << " to Top\n";
         }
      if (option == 2)
         {
            if (!mystack.empty())
         {
               mystack.pop();
               cout << "Stack Successfully Poped\n";
            }
            else
               cout << "Stack is Empty, Cannot Pop\n";
         }
      if (option == 3)
         {
            if (!mystack.empty())
               cout << "Value at Top is: " << mystack.top() << "\n";
            else
               cout << "Stack is Empty, Cannot Top\n";
         }
      if (option > 3 || option < 1)
         {
            cout << "Invalid Option\n";
         }
      stackoptions();
      cin >> option;
   }
}

output:

[edeloss2@pegasus lab2]$ a.out
Enter a words to reverse> del

The words are not palindrome
[edeloss2@pegasus lab2]$

I need to enter a word, have it come out reversed then I have to enter a series of number starting w/ -1 1 2 3 4 5 6 -1
and then gives me a total of the added number.  



0
Comment
Question by:edelossantos
1 Comment
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
ID: 12127819
You are returning from main after "\nThe words are not palindrome\n".

               printf("\nThe words are not palindrome\n");
               return 0;   // <- This returns from main() and therefore quite the program
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question