Rot13

ok so I'm having a few complications, Yes I am new. So here are a list of the complications

1) Having trouble figuring out where my void rot13 should go in the program (ie: inside int main or outside)
2) I got my void fillArray to work however it asks for 2 characters instead of the 50 its supposed to...
3) in bool isalpha I can't get it to accept the characters it gives me a message that says its overloading when I try to compile.

Again I am new to this and really need to try and finish this by today. If you guys can help me I would really appreciate!!!

P.S. I will be working on this all day..

Here is the assignment as my prof instructed

Please write a program that:

1.
Accepts up to 50 characters from the keyboard
2.
Stores these characters in an array
3.
Encrypts the characters in the array
4.
Displays the now-encrypted characters on the screen
5.
Decrypts the characters in the array
6.
Displays the now-decrypted characters on the screen


Submit:

*
the executable, named asgn6
*
A hard copy in class

Follow instructions on the class web site for header information
All functions must have pre/post conditions in the function prototypes.
Your program requires these 4 functions:

pre: none
post: returns true if the parameter is alphabetic, false otherwise
bool alphabetic(char ch)



pre: array has been declared, size has been initialized
post: user has been prompted for input. array has up to fifty characters accepted from keyboard. Size contains the number of characters stored in the array.
void fillArray(char array[], int& size)

pre: array has been declared, size has been initialized to the size of the array
post: array has been encrypted according to this algorithm:

if the character encountered is alphabetic

if the character encountered is in the first half of the alphabet

add 13 to the character

else

subtract 13 from the alphabet

else

do nothing.


void rot13(char array[], int size)

pre: none
post: returns true if the character is from the first half of the alphabet, false otherwise
bool firstHalf(char ch)

Have Fun!!!!!!!!!!!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <cctype>
using namespace std;

//pre: none
//post: returns true if the parameter is alphabetic, false otherwise
bool alphabetic (char ch);

//pre: array has been declared, size has been initialized
//post: user has been prompted for input. array has up to fifty characters accepted from keyboard. Size contains the number of characters stored in the array.
void fillArray(char array[], int& size);



//pre: array has been declared, size has been initialized to the size of the array
//post: array has been encrypted according to this algorithm:
void rot13(char array[], int size);

//pre: none
//post: returns true if the character is from the first half of the alphabet, false otherwise
bool firstHalf(char ch);

const char MAX = 50;

char Array[MAX];

int size = 0;


int main ()
{
bool alphab;
bool isalpha;
int ch;

fillarray (array[MAX] , size);

alphabetic =  alphab (ch);

isalpha = firsthalf(ch);

if (alphabetic)

}

void fillArray (char array[], int& size)
 {
   cout << "Enter " << MAX << " characters.\n";

    for (int ch = 0; ch < MAX; ch++)
   {
        cin >>array[ch];
        size += 1;
    }
  }

bool alphabetic (char ch)
{
bool alpha = true;

  for (int i=0; i<ch; i++)
  {
    if (isalpha(ch))
         return true;
    else
      return false;

  }
  return 0;
}

bool isalpha (char ch)
{
  bool alpha = true;

  for (int i = 0; i<ch;i++)
  {
   if (i >= islower['m'])
      return true;
   
   if  (i >= isupper['M'])
      return true;
   else
     return false;
  }

}

 
void rot13 (array[], int& size)
{
 
  for (int ch = size; ch < MAX; ch++)
    {
     if (isalpha)
        ch + 13;
     else
        ch - 13;
    }
}


1) Having trouble figuring out where my void rot13 should go in the program (ie: inside int main or outside)

Outside int main. It's own function.

2) I got my void fillArray to work however it asks for 2 characters instead of the 50 its supposed to...

Have a look at the cin.getline() function. You can specify a maximum number of characters to accept.

3) in bool isalpha I can't get it to accept the characters it gives me a message that says its overloading when I try to compile.

isalpha() is a C++ function that already exists. That's why your function says call it isalphabetic() for your copy.
Function fillArray(): That's not how you enter a string:
1
2
char str[51]; //One extra character for nul.
std::cin.getline(str,51); //50 characters and nul. 

Line 64: What? The function itself is redundant.
Function isalpha(): What is this supposed to do, anyway?
Lines 81 and 94: Are these supposed to be function calls? islower['m'] isupper['M'] If they are, they're wrong.
Line 93: 'array' has no type.
Function rot13(): The function
Line 96: Think what this for is supposed to do more carefully.
Line 98: You don't call function like that: isalpha(val)
Lines 99 and 101: Wrong: ch+=13
The problem is that I'm not trying to enter a string...

Again I am new so you guys will have to bear with me. As per the instructions given by my prof I am trying to get the characters entered to be stored in an array.
So... You're supposed to enter 50 characters one at a time? Like this?
H<enter>
e<enter>
l<enter>
l<enter>
o<enter>
,<enter>
<enter>
W<enter>
o<enter>
r<enter>
l<enter>
d<enter>
!<enter>
<etc.>
hmm no, I see what you mean... So the string would allow me to enter the characters as words and then pull it into the for statments to incriment through each word? This assignment was to show us how arrays worked so I think he wants us to keep those in there.

oh and check this out for my bool firstHalf statment (which I accidentally named isalpha) I simplified it to have it where if a letter is either in an upper case or lower case then it will be true that it is in the first half of the alphabet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool firstHalf (char ch)
{
  bool alpha = true;

  for (int i = 0; i<ch;i++)
  {
   if ('a'<=i<='m' )
        return true;

   if  ('A'<=i<='M' )
        return true;
   else
     return false;
  }
1. You don't need alpha.
2. You don't need a loop.
3. The entire function can be written in a single line.
4. You don't need a function to make this check.

If you follow carefully the flow of execution, you'll understand.
ok so here we go again I'm thinking that my fillArray is still not where it needs to be... I'm not going to do any strings yet so bear with me... I have it where when I run the program it will repeat the letters I type but it won't cout the rot13 code... I think there is something gunking up the works I've tried to globalize ch but it isn't working... so here is my code so far... I did pull up the bool firstHalf to be about one line I think it makes a lil more sense... in any case here it is!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
include <iostream>
#include <cstdlib>
#include <time.h>
#include <cctype>
using namespace std;

//pre: none
//post: returns true if the parameter is alphabetic, false otherwise
bool alphabetic (char ch);

//pre: array has been declared, size has been initialized
//post: user has been prompted for input. array has up to fifty characters accepted from keyboard. Size contains the number of characters stored in the array.
void fillArray(char array[], int& size);



//pre: array has been declared, size has been initialized to the size of the array
//post: array has been encrypted according to this algorithm:
void rot13(char array[], int size);

//pre: none
//post: returns true if the character is from the first half of the alphabet, false otherwise
bool firstHalf(char ch);

const int MAX = 50;

char array[MAX];

int size = 0;


int main ()
{
char score[MAX];
int size;

fillArray (score,size);

rot13 (score,size);

return 0;
}

void fillArray (char array[], int& size)
{
   char ch;
   cout << "Enter " << MAX << " characters.\n";

    for (int i = 0; i < MAX; i++)
   {
        cin >>array [i];
         ch  = array [i];
       cout<<array[i]<<endl;
   }

  }

bool alphabetic (char ch)
{
   if (('a'<=ch<='z')||('A'<=ch<='Z'))
  return true;
   else
     return false;


}

void rot13 (char array[], int size)
{
char ch;

for(int i =0;i < size && alphabetic(array[i]);i++)
          {
            if(firstHalf(array[i]))
             {
              ch= array[i]+13;
             }
            else
             {
              ch= array[i]-13;
             }
   cout<<ch;
}


}

In fillArray, you do not need ch for anything. Remove lines 46 and 52.

The size parameter of the fillArray function does not need to be passed by reference.

In alphabetic, you want to see that ch is in the range 'a'..'z' or 'A'..'Z'. Here is an example of how that can be checked:
1
2
return ( ( 'a' <= ch ) && ( 'z' >= ch ) )
    || ( ( 'A' <= ch ) && ( 'Z' >= ch ) );


firstHalf is not defined (in the latest snippet).

The global array is not needed.

The global size and the size defined in main are both not needed. MAX should be passed to fillArray and rot13 in main.

The only include that you need is iostream; and be sure that the include statement begins with a #.
Last edited on
my bad here is firstHalf I have it in the program but its too long to just copy and paste in one neat line and I missed it (using pico to edit...)

1
2
3
4
5
6
7
8
9
bool firstHalf (char ch)
{
   if (('a'<=ch<='m')||('A'<=ch<='M'))
        return true;
   else
     return false;


}


yah I'm going to whittle away some of the stuff I don't need sorry been working on this program for the last 5 days straight and I've been trying different things so some of those where when I was using isalpha or isupper and whatnot.
Last edited on
Topic archived. No new replies allowed.