-1

クラス用の暗号化プログラムを書いています。文字列内のスペースの位置を追跡できるようにする必要があります。また、文字列の各文字のASCII値に13を追加する必要があります。46行目でエラーが発生し続けていますが、何が間違っているのかわかりません。

/*
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);
}
4

2 に答える 2

2

スペースを検索する必要がある場合は、を使用できますstring::find_first_of(string)。あなたはただ`を書く必要があります

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

`次に、イテレータを使用して文字列全体を反復処理する必要があります。


また、長さがゼロの場合、なぜ配列をクリアしないのですか?単純なstd::stringクラスを使用するだけです。許可されていない場合は、標準find_first_of(....)ライブラリで使用可能な関数を使用してください。基本的に、イテレータを最初に一致する要素に戻します。

于 2013-03-08T15:49:21.950 に答える
1
int spacePosition[0];  
string message[0], encrypt[0];

おそらくそれらを1にしたい...または配列をまったく使用しないでください。ただし、長さが0の配列を宣言しているので、それらに書き込もうとすると、セグメンテーション違反が発生します。

于 2013-03-08T15:48:23.363 に答える