Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8069493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:04:11+00:00 2026-06-05T13:04:11+00:00

I received an assignment for my C++ class last week. I think some of

  • 0

I received an assignment for my C++ class last week. I think some of you will find it interesting! I managed to get most of the code down but I’m stuck and cannot figure this out for the life of me… Below are the guidelines for the encrypting process I must put into code:

The message sender inputs a four letter word, CCCC, and another four letter word,
XXXX.

The message sender then inputs the message to be encrypted.

The program scans the message one char at a time and each char is pushed in a stack until
either the scanned character is in the word CCCC or the end of the message is
encountered.

When the scanned character is one of the chars in CCCC, print that char and continue
to print and pop the chars at the top of the stack until either the stack is empty or the
char at the top of the stack is one of the chars in XXXX. When the end of the
message is encountered, print the character at the top of the stack and continue to pop
and print from the top of the stack until the stack is empty.

Here is a hint: “GOOD” “LUCK“, it “SOUNDS SIMPLE TO ME“, or as
your program would say: “OSDNOT EEM LPMIS SU“

So that is the actual assignment.

What I am having trouble with is the last bit:

When the end of the
message is encountered, print the character at the top of the stack and continue to pop
and print from the top of the stack until the stack is empty.

now here is the code I have so far:

#include <string>
#include <iostream>
using namespace std;
class Stack
{
   private:
   char Chars[50];
   int top;
   public:
   int push(char);
   char pop();
   bool isEmpty();
   bool isFull();
   Stack()
   {
      top = 0;
   }
};

int main()
{
Stack theStack;
   char word1[4];
   char word2[4];
   for(int i=0; i < 4; i++){
      word1[i] = ' ';
      word2[i] = ' ';
   }
   char message[500];   
   cout << "Please enter a 4 letter word: ";
   cin >> word1;
   while(word1[4] || !word1[3])
   {
      cout << "Word must be 4 chars long. Try again: ";
      cin >> word1;
   }
   cout << "Please enter another 4 letter word: ";
   cin >> word2;
   while(word2[4] || !word2[3])
   {
      cout << "Word must be 4 chars long. Try again: ";
      cin >> word2;
   }
   cout << "Please enter the phrase to be encrypted (50 chars max): ";
   cin.ignore(1000, '\n');
   cin.getline(message,500);
   int length = strlen(message);
   int count = 0;
   char finalMsg[length];
   //scanner
   for(int i = 0; i < length; i++)
   {
      if(message[i] == word1[0] ||
         message[i] == word1[1] ||
         message[i] == word1[2] ||
         message[i] == word1[3])
      {
         finalMsg[count] = message[i];
         count++;
         if(message[i-1] != word2[0] ||
            message[i-1] != word2[1] ||
            message[i-1] != word2[2] ||
            message[i-1] != word2[3])
         {
            finalMsg[count] =  message[i-1];
            count++;
         }
      }
      else
      {
         theStack.push(message[i]);
      }
   }
   cout << finalMsg << endl;
return 0;
}

int Stack::push(char data)
{
   Chars[top] = data;
   top++;
return top;
}

char Stack::pop()
{
   char ret = Chars[top-1];
   top--;
return ret;
}

bool Stack::isEmpty()
{
   if(top <= 0)
      return true;
   else return false;
}

bool Stack::isFull()
{
   if(top >= 50)
      return true;
   else return false;
}

When compiled, the final output gives me “OSDNOT” which is in the example provided by my professor, so I know I’m heading down the right track.. Any help would be great, I don’t even know where to begin to examine the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:04:12+00:00Added an answer on June 5, 2026 at 1:04 pm

    Here is the corrected code. You didn’t code the algorithm right. I have commented the changes i have made in the code.
    first of all, you didn’t pop out the elements of the stack when you encountered a character present in CCCC while scanning. Also at the end of scanning, you didn’t empty the stack. Include cstring instead of string. As pointed out in the comments, your declaration for word1 and word2 is incorrect.

       char finalMsg[200];
       //scanner
       for(int i = 0; i < length; i++)
       {
          if(message[i] == word1[0] ||
             message[i] == word1[1] ||
             message[i] == word1[2] ||
             message[i] == word1[3])
          {
             finalMsg[count] = message[i];
             count++;
    
             //pop out elements from the stack till it is empty or an character of XXXX is encountered
             while(!theStack.isEmpty())     
             {
                 char tmp=theStack.pop();
                 if(tmp==word2[0] ||
                    tmp==word2[1] ||
                    tmp==word2[2] ||
                    tmp==word2[3])
                    {
                        theStack.push(tmp);
                        break;
                    }
                finalMsg[count++]=tmp;
             }
    
          }
          else
          {
             theStack.push(message[i]);
          }
       }
    
      //empty the stack
       while(!theStack.isEmpty())
       {
           finalMsg[count++]=theStack.pop();
       }
       finalMsg[count++]=0;
       cout << finalMsg << endl;
    

    PS: Better use std::stack and std::string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have received an consulting assignment where I will update a Fortran IV program
We received this piece of code for an assignment, and after spending a lot
I recently received an assignment in my Java programming class to analyze a (what
I've just received an assignment to upgrade an old Delphi 3 project that I
So I finished my first C++ programming assignment and received my grade. But according
I received this error on following code.Is this + sign is not available in
I received the error while trying to display some variable like so: echo id
For a class assignment, I'm required to write an object to a file. Our
I'm working on a bit of code for my C++ class, and I'm a
I received an assignment to develop a community to a club with about 1,000

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.