-1

I'm writing an encryption program for class. I need to be able to track the position of spaces in a string, as well as adding 13 to the ASCII value of each character of the string. I keep getting an error in line 46, and I don't see what I'm doing wrong.

/*
program 4

use an array to store the string
convert decimal values to ASCII, add 13 to the ASCII values, 
convert back to decimal
the character for the space shoud be determined by the character 
that came before it.
The character before it should have 4 added to it and 
placed after itself to act as the space.
*/

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

using namespace std;

int valueChange(string array), length(string array);
int spacePosition[0]; 
string message[0], encrypt[0];


int main()
{

cout << "Please enter your message."; //start with this
cin >> message[0];

int value = length(message[0]);
int count=0;

/*

store message as an array

loop through array, adding 13 to each value, use tolower() on each value
if value is 

*/
for (int i=0; i < value; i++)
{
    valueChange(message[i]);

    if (message[i] == ' ') //checks for spaces in the string
    {
        spacePosition[count] = i + 1; //records the placement of spaces
                                      //in the string

    //have the array cast the i value to a new int value
    //store space positions in an array

    count++;
    }
 }

 cout << "&";
 cout << "Message encrypted and transmitted."; //final message

 getch();
 return 0;
}

int valueChange(int array[])
{
array[0] += 13;

if (array[0] > 122)
{
 array[0] - 122;
 }

 return (array[0]);
}

int length(string array)
{
return (array.length() - 1);
}
Brandon
  • 103
  • 2
  • 2
  • 10
  • 8
    `int spacePosition[0]; string message[0], encrypt[0];` dafuq? – stefan Mar 08 '13 at 15:44
  • 4
    And line 46 is...? And the error you are getting is...? – indiv Mar 08 '13 at 15:46
  • Line 46 is: if (message[i] == ' ') //checks for spaces in the string – Brandon Mar 08 '13 at 15:49
  • no match for 'operator==' in 'message[i] == ' '' – Brandon Mar 08 '13 at 15:57
  • @BrandonHoutzer That's because `message` is an array of `std::string`s of length 0, not a string itself. Thus `message[0]` is a std::string (and a segmentation fault, because your array is empty) and a std::string doesn't have a comparison to a char defined. – stefan Mar 08 '13 at 16:04
  • I don't get how to fix it. We haven't gone over std::string or anything that looks like that. How are you supposed to create an array length that's just long enough to store the string? If you start the array off at zero and then change the size later on with another variable and then feed it the string, wouldn't that work? – Brandon Mar 08 '13 at 16:12

2 Answers2

2

If you need to search for spaces you can use string::find_first_of(string). You just need to write `

std::string yourstring; 
yourstring.find_first_of(" ");

` then you need to iterate through the whole string using iterator.


And why are you declearing array if zero length? You can simply use simple std::string class. If you are not allowed to then simply use find_first_of(....) function available in standerd library. It basically returns the iterator to the first matching element.

deeiip
  • 3,319
  • 2
  • 22
  • 33
1
int spacePosition[0];  
string message[0], encrypt[0];

Probably want those to be 1's... or not use an array at all. But you are declaring arrays with 0 length, so yeah, you're going to segfault when you try writing to them.

djechlin
  • 59,258
  • 35
  • 162
  • 290