I need to finish my assignment but i seem to have several problem and i stuck. Below is the assignment question and also the source code that i wrote

Question

Write an interactive C++ program that will encode or decode a line of text. To encode a line of text, proceed as follows:

a) Convert each character, including blank spaces, to its ASCII equivalent.

b) Generate a positive random integer. Add this integer to the ASCII equivalent of each character. The same random integer will be used for the entire line of text.

c) Suppose that N1 represents the lowest permissible value in the ASCII code, and N2 represents the highest permissible value. If the number obtained in step 2 above (i.e. the original ASCII equivalent plus the random integer) exceeds N2, then subtract the largest possible multiple of N2 from this number, and add the remainder to N1. Hence the encoded number will always fall between N1 and N2, and will therefore always represent some ASCII character.

d) Display the characters that correspond to the encoded ASCII values.

-My problem is to create the

cin>>str1;

o i can enter a text after executing the program.

-I also have problem for question C

-any errors or recomendation please let me know.

coding

#include<iostream.h>
#include<string.h>

void main()
{
   char str1[]="Testing";
   char str2[40];

   strcpy(str2, str1);

   cout<<"str1="<<str1<<endl;
   cout<<"str2="<<str2<<endl;

   int i=0;

   while(str1[i]!='\0')
   {
      cout<<(int)str1[i]<<" ";
      str2[i]=(char)((int)str1[i]+2);
      i++;
   }

   cout<<endl;
   
   i=0;

   while(str2[i]!='\0')
   {
      cout<<(int)str2[i]<<" ";
      i++;
   }

   cout<<endl;

   cout<<str2<<endl;

   i=0;
   
   while(str2[i]!='\0')
   {
      cout<<(char)((int)str2[i]-2);
      i++;
   }

   cout<<endl;
}

You solved (c) by casting the int back into a char, and that works. You use '2' rather than a random number, but 2 works for the testing.

So far so good, what are the problems?

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.