我的文字是这样的
55 <space>
用户输入句子;
我通过下面的代码获得了55,并通过atoi
将其分配给一个int变量std::string stringKey = userInput.substr(0, 2);
但是如何获取字符串的其余部分并将其分配给字符串变量????下面的代码
这是完整的代码(尚未完成)
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
using namespace std;
//FUnction Prototypes
string encrypt(string, int);
string decrypt(string source, int key);
int main(int argc, char *argv[])
{
string source;
string userInput;
int key;
int choice;
cout << "To encode a message type 1, to decode a message type 2: ";
cin >> choice;
if (choice == 1)
{
cout << "Enter the message to encode: ";
cin >> userInput;
std::string stringKey = userInput.substr(0, 2);
key = atoi(stringKey.c_str());
std::string source = userInput.substr(3);
encrypt(source, key);
}
/*decrypt(source, key);
cout << "Source: ";
getline(cin, source);
cout << "Key: ";
cin >> key;
cout << "Encrypted: " << decrypt(source, key) << endl;*/
system("pause");
}
string encrypt(string source, int key)
{
string Crypted = source;
for (int Current = 0; Current < source.length(); Current++)
Crypted[Current] = ((Crypted[Current] + key) - 32) % 95 + 32;
return Crypted;
}
string decrypt(string source, int key)
{
string Crypted = source;
for (int Current = 0; Current < source.length(); Current++)
Crypted[Current] = ((Crypted[Current] - key) - 32 + 3 * 95) % 95 + 32;
return Crypted;
}
答案 0 :(得分:0)
与第一部分相同。查看std::string substr
string second = userInput.substr(3);
---------------- EDIT ------------------
如果您输入55<space>The User Input Sentence
作为输入,则只会读取第一个字符串。
使用getline
方法读取整个输入,包括空格