如何在循环中更改 int 变量,如果可以,是否有更有效的方法?

How do I change the int variable in a cycle and if so is there a more efficient way?

第一个 post 在这个网站上所以请饶了我的命。 我正在尝试做一个模仿二战中的 Enigma cipher/machine 的小加密和解密程序(足够的历史课程)

所以我正在尝试输入一个数字和一个字母,如下所示:1h 2e 3l 4l 5o ; 因为我不使用循环,所以我需要为每个要添加数字的变量编写,但是如果我的字母少于变量,我该怎么办?我能想到的唯一方法是使用一个循环来检查是否输入是字母或 not.That 为什么我编写的当前代码只能用于特定数量的数字和字母...

#include <iostream>
#include <limits>
using namespace std;
int main()
{

int opt;
cout<<"Enter 1 for encryption and 2 for decryption. "<<endl;
cin>>opt;
if(opt == 1)
  {
  int setting1,setting2,setting3,setting4;
  char a,b,c,d;
  cout<<"_________________________________________________"<<endl;
  cin>>setting1>>a>>setting2>>b>>setting3>>c>>setting4>>d;
    a+=setting1;
    b+=setting2;
    c+=setting3;
    d+=setting4;
  cout<<(char)a<<" "<<(char)b<<" "<<(char)c<<" "<<(char)d<<endl;

  }
if(opt == 2)
  {
  int setting1,setting2,setting3,setting4;
  char a,b,c,d;
  cout<<"_________________________________________________"<<endl;
  cin>>setting1>>a>>setting2>>b>>setting3>>c>>setting4>>d;
    a-=setting1;
    b-=setting2;
    c-=setting3;
    d-=setting4;
  cout<<(char)a<<(char)b<<(char)c<<(char)d<<endl;

  }
  if(opt !=1 && opt !=2)cout<<"ERROR!"<<endl;



  std::cout << "Press ENTER to continue..."<<endl;
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(),'\n');
  return 0;
} 

我还被告知最后两行会阻止 .exec 在完成后关闭。

我建议在循环(循环)中输入数据。但在此之前,我建议使用带有重载流提取运算符的结构(或 class)。

struct Number_Letter
{
  int number;
  char letter;
  friend std::istream& operator>>(std::istream& inp, Number_Letter& nl);
};
std::istream& operator>>(std::istream& inp, Number_Letter& nl)
{
  inp >> nl.number;
  if (inp)
  {
    inp >> nl.letter;
    if (!isalpha(nl.letter))
    {
      // Error recovery for not reading a letter
    }
  }
  else
  {
    // Error recovery for failing to read number.
  }
  return inp;
}

你的输入循环是这样的:

Number_Letter nl;
while (std::cin >> nl)
{
  // process the data
}

对于密码学,您可能希望将数据保留为字符串:

std::string message;
getline(cin, message);
for (unsigned int i = 0; i < message.length(); i += 2)
{
  if (!isdigit(message[i]))
  {
    cerr << "Invalid message type at position " << i << endl;
    break;
  }
  if (i + 1 > message.length())
  {
    cerr << "Missing letter at end of message.\n";
    break;
  }
  if (!isalpha(message[i+1]))
  {
    cerr << "Invalid message type at position " << i << endl;
    break;
  }
}

听起来您正在尝试检查 character/integers 的未知序列并且需要循环来进行检查?

int opt;
cout<<"Enter 1 for encryption and 2 for decryption. "<<endl;
cin>>opt;

if(opt != 1 && opt != 2)
{
  cout << "Error" << endl;
  return -1;
}

int integer;
char character;
char again;

do
{
    cout<<"_________________________________________________"<<endl;    

    cin>>integer>>character;

    if(opt == 1) {
        character+=integer;
    } else if(opt == 2) {
        character-=integer;
    }

    cout << character <<endl;

    cout << "Again (Y)?: ";
    cin>>again;
}while(again == 'Y' || again == 'y');

std::cout << "Press ENTER to continue..."<<endl;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(),'\n');
return 0;