Fixing error in program “Need Help”

Asked By 20 points N/A Posted on -
qa-featured

 

I am getting this error with when I compile. I have to turn this assignment in a few hours. Here is the code:

error message(s)

untitled.cpp:34: error: invalid types ‘int[int]’ for array subscript
untitled.cpp:36: error: invalid types ‘int[int]’ for array subscript

// A basic Casear Cipher Encrytion Device.

include <iostream>

include <iomanip>

using namespace std;

define RINGLEN 62


  1. // NUM OF CHARS IN ALLOWED CHARACTER SET.

int main () { int i=0; // index variables int j=0; char ring[RINGLEN+1]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; int key = 4; const int size = 1000; char word[size+1];


  1. cout << "Please insert the message you would like to encrypt: ";
  2. cin >> word;
  3.  
  4. for (int i=0;i<size;i++)
  5. {
  6. if ((word[i] >='A' && word[i] <='Z') || (word[i]>='a'&&word[i]<='z') ||(word[i]>='0'&&word[i]<='9'))
  7. {
  8. int ring = word[i] + key;
  9.  
  10.  
  11. for (int j=0; j<=RINGLEN;j++)
  12. {
  13. if (word[i] == ring[j])
  14. {
  15. cout << ring[(j+key) % RINGLEN];
  16. break;
  17. }
  18. }
  19. }
  20.  
  21.  
  22. else
  23. {
  24. cout << word[i];
  25.  
  26. }
  27. }
  28. return 0;

}

Please help

 

SHARE
Answered By 5 points N/A #118476

Fixing error in program “Need Help”

qa-featured

Hi Justin,

My solution is straight to the point.

 

 

#include <iostream>
#include <iomanip>
using namespace std;
#define RINGLEN 62
// NUM OF CHARS IN ALLOWED CHARACTER SET.
int main () 
{ int i=0; 
//index variables 
int j=0; 
char arr_ring[RINGLEN+1]= {'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E',
'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W',
'X','Y','Z','0','1','2','3','4','5','6','7','8','9'}; 
int key = 4; const int size = 1000; char word[size+1];
cout << "Please insert the message you would like to encrypt: ";
cin >> word;
 
for (int i=0;i<size;i++)
{
if ((word[i] >='A' && word[i] <='Z') || (word[i]>='a'&&word[i]<='z') ||(word[i]>='0'&&word[i]<='9'))
{
int ring = word[i] + key;
 
 
for (int j=0; j<=RINGLEN;j++)
{
if (word[i] == arr_ring[j])
{
cout << arr_ring[(j+key) % RINGLEN];
break;
}
}
}
 
else
{
cout << word[i];
 
}
}
return 0;
}

 

You got compile time errors because of two reasons:

1. I bet want you want to achieve for char ring[RINGLEN+1] is an array of characters not a single string.

2. You declared another "ring" variable of type int inside the if block. This is what the compiler saw upon compilation not the one you declared above.

 

I hope it helped.

 

 

Related Questions