首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对句子进行编码

对句子进行编码
EN

Stack Overflow用户
提问于 2016-12-03 14:43:28
回答 2查看 390关注 0票数 1

我正在尝试用C++对一个句子进行编码,我得到的唯一加密是句子中单词的第一个字母,而不是句子中的所有字母。这是我下面的代码,我得到的输出在我的代码下面。下面是我应该有的输出。

代码语言:javascript
复制
#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;}

我的输出:

你的乘数是多少?

4.

你的加法器是什么?

3.

输入您想要加密的句子

管家做到了

加密:

339

这是我应该拥有的代码:

你的乘数是多少?

4.

你的加法器是什么?

3.

输入您想要加密的句子

管家做到了

加密:

339 419 407 131 395 471 467 435 407 459 131 403 423 403 131 423 467

我遗漏了什么?

EN

回答 2

Stack Overflow用户

发布于 2016-12-03 14:49:15

在您的代码中,words是一个char变量,并且只能存储一个字符。相反,您可以将words声明为std::string类型并包含string库。

代码语言:javascript
复制
std::string words;

为了计算每个字符的值,您需要运行一个循环,例如

代码语言:javascript
复制
cout << "Encrypted: " << endl;
for(int i=0;i<words.size();i++)
{
    cout << (int)words[i] * multi + adder << " ";
}
cout << endl;
票数 2
EN

Stack Overflow用户

发布于 2016-12-03 15:07:43

多个问题:

  1. char words -它只需要一个字符。使用getline函数将其更改为包含多个单词的string words
  2. To行

#include #include 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 you want endl“<< endl;getline(cin,words);cout << "Encrypted:”<< endl;for(int i=0;i

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40945039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档