I'm not sure how to get started with this. Here's the assignment and what i've done so far, but I'm not sure if i'm going the right direction with this. The assignment says to have the user enter input one character at a time. That means one letter or space or punctuation mark, then have them hit enter, then another character, they hit enter, and so on.. right?

Programming Assignment Specifications:

You have been sending email to your friends – and you have decided to be clever and insert a second message inside your first (two for the price of one)! However, you are afraid that your friends won’t be able to read the hidden second message, so you have decided to use your new skills at C++ programming to write a program to decode the message that you type in – so that your friends can clearly read it (or so you hope!).

Allow the user to enter in a paragraph – character by character. Write a program to read the paragraph, character by character, and display the second message using the following rules:

  1. The first letter of each word comprises the hidden message
  2. However, if the second letter of a word is the SAME as the first letter, then the first letter is NOT used – and the 3rd (if it exists) is used.
  3. If there are only two letters in the word – and they are the same – then it means that the new message should have a period. If the duplicate characters are actually punctuation (.,;!?), then use that same punctuation for your hidden message.
  4. If there are two (or more) spaces in a row – between words – then there should be a space between words in the hidden message.

Every word following a period in your new message should be capitalized

Things you should know...as part of your program:

  1. Experiment with writing your own function. Don’t use global variables.
  2. Allow the user to enter in another paragraph after decoding the message, or quitting.
  3. You SHOULD NOT echo the original message. You will not like the results.
  4. MAKE SURE to read one character at a time. This programming assignment is not designed to be used with strings or arrays (we haven’t learned them yet). Therefore, they MAY NOT be used in this assignment!

Here's what I started doing:

char ch1;  //Space entered by user
char ch2;  //First letter or space entered by user
char ch3;  //Second letter entered by user
char ch4;  //Third letter entered by user
char x;    //Character used in hidden message


#include <iostream.h>


void decode();


int main()
{
cout <<"Please enter each word of the message, and hit the enter "
<<endl;
cout <<"key after each word entered, Include punctuation "
<<endl;
cout <<"marks and spaces between words. Type in the "
<<endl;
cout <<"spaces at the beginning of the word: ";
cin.get(ch1);
cin.get(ch2);
cin.get(ch3);
cin.get(ch4);


decode ();


return 0;
}


void decode()
{
if (ch1 == '#')
x = '#';
else if (ch2 != ch3)
x = ch2;
else if (ch1 == ' ' && ch2 == ' ')
x = ' ';
else if (ch2 == ch3)
x = ch4;
else
x = '.';
}

Any help is much appreciated, thanks

Recommended Answers

All 7 Replies

No way.You will go crazy if you process on char at a time.Just read a whole line into a char array and then encode it.I will give you a very simple encryption algorithm called XOR.

void XOR(char *message,char *pass)
{
    int p=0;
 
    for(int i=0;message[i]!=0;i++)
    {
           message[i] ^= pass[p++];
 
          if(pass[p]==0)p=0;
    }
}

ok I was having some fun.I will post some ideas as soon as I have thought them up.Man ,that question sure limits your scope.For me it's like trying to fit on a shoe 4 sizes too small(hope you understand).

I think you do not obey the homework rules because you're using global variables. You're also reading only for characters, but this is not a very big problem since you can use a do-while loop.But first of all I think you should re-write youre code so that it doesn't use global variables. In order to do this you should parametrize the decode function. You could say,for instance, decade(ch1,ch2,ch3,ch4).In that way you could avoid using global variables.
I hope this would be helpfull.

Program specifies that I'm not allowed to use strings or arrays... (not that i know how to use those anyway)

I'm going to keep trying to do it by reading in one word at a time, as it is the only thing I can think of that might work if i get it right :-| (am brand new to programing)

suggestions?

I think you do not obey the homework rules because you're using global variables. You're also reading only for characters, but this is not a very big problem since you can use a do-while loop.But first of all I think you should re-write youre code so that it doesn't use global variables. In order to do this you should parametrize the decode function. You could say,for instance, decade(ch1,ch2,ch3,ch4).In that way you could avoid using global variables.
I hope this would be helpfull.

thanks! will try using do-while loop and function with parameters

Here's some progress I've made... still not sure how i'm going to make this work. Any more help really appreciated. Also, was getting errors when trying to write the function with variable declarations inside it -declared them in the int main() instead and got no errors. Am i still using global variables? If so how what changes do i need to make.

#include <iostream.h>

char decode(char ch1, char ch2, char ch3, char ch4);

 int main()
{
      char ch1;
      char ch2;
      char ch3;
      char ch4;

      cout <<"Please enter each word of the message, and hit the enter "
           <<endl;
      cout <<"key after each word entered.  Include punctuation "
           <<endl;
      cout <<"marks and spaces between words. Type in the "
           <<endl;
      cout <<"spaces at the beginning of the word.  When finished, type "
           <<endl;
      cout <<"the pound sign (#) and hit enter." <<endl;
      cout <<"enter word: ";

      cin.get(ch1);
      cin.get(ch2);
      cin.get(ch3);
      cin.get(ch4);

      decode (ch1, ch2, ch3, ch4);

      return 0;
      }

//function to figure out what character to use for hidden message
char decode(char ch1, char ch2, char ch3, char ch4)
{ 

      char x; //character used for hidden message

      if (ch1 == '#') //for when user is finished entering words
        x = '#';
      else if (ch2 != ch3) //using first letter of word
        x = ch2;   
      else if (ch1 == ' ' && ch2 == ' ') //when two spaces entered, use space
        x = ' ';   
      else if (ch2 == ch3) //when 1st and 2nd letter are same, use 3rd
        x = ch4;
      else 
        x = '.'; //none of the above conditions satisfied, use '.' (hoping this works for when the word isn't long enough to have a 'ch4' value)
}

this is due very soon, so any help greatly appreciated. fell asleep so haven't made any more progress.

Did some more work, but this doesn't work. Help!

#include <iostream.h>

char decode(char ch1, char ch2, char ch3, char ch4);

 int main()
{
      char ch1;  //Space entered by user
      char ch2;  //Space or letter or punctuation entered by user
      char ch3;  //Letter or punctuation entered by user
      char ch4;  //Letter or punctuation entered by user (may not exist)
      char hm;   //Character of hidden message
      char response;  //y or n answer to if user wishes to decode more

      cout <<"Please enter each word of the message, and hit the enter "
           <<endl; 
      cout <<"key after each word entered.  Include punctuation "
           <<endl; 
      cout <<"marks and spaces between words. Type in the "
           <<endl;
      cout <<"spaces at the beginning of the word.  When finished, type "
           <<endl;
      cout <<"the pound sign (#) and hit enter." <<endl;
      do
      {
      do
      {
      cout <<"enter word: ";

      cin.get(ch1);
      cin.get(ch2);
      cin.get(ch3);
      cin.get(ch4);

      hm = decode (ch1, ch2, ch3, ch4);

      cout <<"hidden message: "<<hm<<endl;

      } while (hm != '#');

      cout <<"Would you like to decipher another message? (y/n): ";
      cin  >>response;

      } while (response == 'y' || response == 'Y');

      cout <<"Thanks for using this message decoding program!"
           <<endl;

      return 0;
      }

char decode(char ch1, char ch2, char ch3, char ch4)
{

      char x; //character used for hidden message

      if (ch1 == '#')
        x = '#';
      else if (ch2 != ch3)
        x = ch2;
      else if (ch1 == ' ' && ch2 == ' ')
        x = ' ';
      else if (ch2 == ch3)
        x = ch4;
      else
        x = '.';

      return x;
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.