【发布时间】:2013-02-24 05:17:45
【问题描述】:
我正在为班级编写一个加密程序。我需要能够跟踪字符串中空格的位置,以及将 13 添加到字符串每个字符的 ASCII 值。我在第 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);
}
【问题讨论】:
-
int spacePosition[0]; string message[0], encrypt[0];dafuq? -
第 46 行是...?你得到的错误是......?
-
第 46 行是: if (message[i] == ' ') //检查字符串中的空格
-
'message[i] == '''中的'operator=='不匹配
-
@BrandonHoutzer 那是因为
message是一个长度为0 的std::strings 数组,而不是字符串本身。因此message[0]是一个 std::string (和一个分段错误,因为你的数组是空的)并且 std::string 没有与定义的 char 进行比较。