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

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

本文关键字:是否 有效 方法 如果 循环 int 变量      更新时间:2023-10-16
这个

网站上的第一篇文章,请饶我一命。我正在尝试做一个小的加密和解密程序,模仿二战中的恩尼格玛密码/机器(足够的历史课程)

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

#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;
} 

我还被告知,最后 2 行会阻止 .exec 在完成它的事情后关闭。

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

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;
  }
}

听起来您正在尝试检查未知的字符/整数序列,并且需要一个循环来执行检查?

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;