好吧,上次我寻求有关此程序的帮助,因为我无法将字符转换为DEC并添加到DEC中。归功于我给出的一些建议,我终于使它能够正常工作。
#include <iostream>
using namespace std;
int main()
{
char word[128];
int x = 0;
int v;
int shift;
int sv;
cin >> shift;
cin >> word;
while (word[x] !='\0') // While the string isn't at the end...
{
v = int(word[x]);
sv = v + shift;
x++;
cout<< static_cast<char>(sv);
}
return 0;
}
但是我不知道如何使用它来接受空格
isspace
你们能帮我吗?
答案 0 :(得分:0)
getline
可能是您的朋友。这是您的代码,但已固定使用。
#include <string>
int rotMain()
{
//char word[128];
string word;
int x = 0;
int v;
int shift;
int sv;
cin >> shift;
getline(cin, word);
while (word[x] != '\0') // While the string isn't at the end...
{
v = int(word[x]);
sv = v + shift;
x++;
cout << static_cast<char>(sv);
}
return 0;
}
您正在做一些其他的事情,这里有些奇怪,例如您没有使用任何字符的模数,因此,例如,将“ Z”旋转1表示为“ [”,但这可能是设计使然?还建议使用标准迭代器对字符串进行迭代,但是,如果您只是在学习,现在就不必担心其中的任何一个!