对句子进行编码 我试图用C++来编码一个句子,我唯一得到的加密是句子中的第一个字母,而不是句子中的所有字母。下面是我的代码,我得到的输出在我的代码下面。下面是我应该得到的输出 #include <iostream> #include <string> using namespace std; int main () { int multi; int adder; char words; cout << "What is your multiplier?" << endl; cin >> multi; cout << "What is your adder?" << endl; cin >> adder; cout << "Enter in a sentence you want encrypted" << endl; cin >> words; cout << "Encrypted: " << endl; cout << (int)words * multi + adder << endl; return 0;} #包括 #包括 使用名称空间std; int main() { int-multi; 加法器; 字符词; cout-multi; cout加法器; 不能说话; cout
在您的代码中,对句子进行编码 我试图用C++来编码一个句子,我唯一得到的加密是句子中的第一个字母,而不是句子中的所有字母。下面是我的代码,我得到的输出在我的代码下面。下面是我应该得到的输出 #include <iostream> #include <string> using namespace std; int main () { int multi; int adder; char words; cout << "What is your multiplier?" << endl; cin >> multi; cout << "What is your adder?" << endl; cin >> adder; cout << "Enter in a sentence you want encrypted" << endl; cin >> words; cout << "Encrypted: " << endl; cout << (int)words * multi + adder << endl; return 0;} #包括 #包括 使用名称空间std; int main() { int-multi; 加法器; 字符词; cout-multi; cout加法器; 不能说话; cout,c++,string,encryption,C++,String,Encryption,在您的代码中,words是一个char变量,只能存储一个字符。相反,您可以将words声明为std::string类型,并包含string库 std::string words; 要计算每个字符的值,需要运行循环,例如 cout << "Encrypted: " << endl; for(int i=0;i<words.size();i++) { cout << (int)words[i] * multi + adder << "
words
是一个char
变量,只能存储一个字符。相反,您可以将words
声明为std::string
类型,并包含string
库
std::string words;
要计算每个字符的值,需要运行循环,例如
cout << "Encrypted: " << endl;
for(int i=0;i<words.size();i++)
{
cout << (int)words[i] * multi + adder << " ";
}
cout << endl;
cout多个问题:
charwords
——只需要一个字符。
将其更改为字符串
getline
函数
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int multi;
int adder;
string words;
cout << "What is your multiplier?" << endl;
cin >> multi;
cin.clear();
cout << "What is your adder?" << endl;
cin >> adder;
cin.ignore();
cout << "Enter in a sentence you want encrypted" << endl;
getline(cin, words);
cout << "Encrypted: " << endl;
for(int i=0;i<words.length();i++)
{
cout << (int)words[i] * multi + adder << " ";
}
cout << endl;
system("PAUSE");
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int-multi;
加法器;
字符串;
cout-multi;
cin.clear();
cout加法器;
cin.ignore();
CUT你是C还是C++的初学者?我想你应该从如何表示字符串和使用循环的基础开始。编码和加密是不同的事情。打败我。我唯一不同的是使用基于范围的。有一个投票。多个问题:(1)<代码>使用命名空间STD < /C>是一个可怕的习惯。(2)system(“PAUSE”)
是您永远不应该使用的代码。(3)没有像:-
这样的标点符号。您可以使用冒号或破折号,而不是两者都挤在一起。(4)要将代码块放入编号列表中,您可以将每行缩进4个空格,总共8个空格。(我已经解决了后两个格式问题,但在其他方面没有处理代码。)