C++ 如果语句不工作,则输入无法识别(C+;+;)
我的程序运行没有问题,编译没有错误 我的if语句的目的是选择字母是否左/右移位,这是我用if语句尝试实现的,该语句导致两个函数,但是无论从C++ 如果语句不工作,则输入无法识别(C+;+;),c++,if-statement,encryption,caesar-cipher,C++,If Statement,Encryption,Caesar Cipher,我的程序运行没有问题,编译没有错误 我的if语句的目的是选择字母是否左/右移位,这是我用if语句尝试实现的,该语句导致两个函数,但是无论从cin>>z输入它将始终使用密码。有人能帮我弄清楚吗 //Start of Document #include <WinUser.h> //Gives access to windows form library #include "StdAfx.h" //Standard prefix for vb to save
cin>>z输入代码>它将始终使用密码。有人能帮我弄清楚吗
//Start of Document
#include <WinUser.h> //Gives access to windows form library
#include "StdAfx.h" //Standard prefix for vb to save
#include <iostream> //Enables I/O stream cin/cout
#include <string> //Ensures that string data can be assigned
#include <Windows.h> //Gives access to windows library Included libraries, above <winuser.h>
using namespace std;
//Variables
char cipherR(char);
char cipherL(char);
int y;
const string LEFT = "1";
const string RIGHT = "0";
//Main Code
int main()
{
string input; //Declares string variable for temp storage
//MessageBox
int WinUserMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType); //defines MessageBox parameters
const int message = MessageBox(NULL, L"Is your location secure?", L"Warning", MB_YESNOCANCEL); //sets up listener for messagebox response
//Switch Response
switch (message) //Initialise case study
{
case IDYES: //Runs this if Yes is selected
//cipher code block
do {
cout << "Enter text to be ciphered/deciphered." << endl;
cout << "Enter blank line to quit." << endl;
getline(cin, input); //Prompts for User Input, stores into temporary var
string output = ""; //Checks for blank input
cout << "Shift Left(1) or Right(0)? \n";
string z; //L or R definer
std::cin >> z;
cout << "Enter number to shift\n";
cin >> y;
if (z == LEFT)
{
for (int x = 0; x < input.length(); x++) // Loops until string.length is reached.
{
output += cipherR(input[x]);
}
} //Adds the value of expression to the value of a variable and assigns the result to the variable.
else if (z == RIGHT)
{
for (int x = 0; x < input.length(); x++) // Loops until string.length is reached.
{
output += cipherL(input[x]); //Adds the value of expression to the value of a variable and assigns the result to the variable.
}
};
cout << output << endl; // If input is blank will end process
} while (!input.length() == 0); //Loops while input.length is NOT equal to 0
break;
//alternate message responses
case IDNO: //Runs this if No is selected
MessageBox(NULL, L"Agents are converging on your location now.", L"Run!", NULL);
return 0;
break;
case IDCANCEL: //Runs this if cancel is selected
MessageBox(NULL, L"Why open this is you're gonna back out?", L"Alert", NULL);
return 0;
break;
}
}
//functions
char cipherR(char c) //function caesar, called into main
{
if (isalpha(c)) //checks if is part of the alphabet
{
c = toupper(c); //ignores casing of input for universal input
c = (((c - 65) + y) % 26) + 65; //performs caesar cipher with algebraic equation
return c; //returns encrypted data
}
if (!isalpha(c)) //if is not alphabetic will make blank
{
return 0; //returns blank
}
// if c isn't alpha, just send it back
}
char cipherL(char c) //function caesar, called into main
{
cout << "This Is left";
if (isalpha(c)) //checks if is part of the alphabet
{
c = toupper(c); //ignores casing of input for universal input
c = (((c - 65) - y) % 26) + 65; //performs caesar cipher with algebraic equation
return c; //returns encrypted data
}
if (!isalpha(c)) //if is not alphabetic will make blank
{
return 0; //returns blank
}
// if c isn't alpha, just send it back
}
//end of sheet
//文档的开头
#include//允许访问windows窗体库
#包括“StdAfx.h”//vb要保存的标准前缀
#include//启用I/O流cin/cout
#include//确保可以分配字符串数据
#include//提供对上述windows库中包含的库的访问权限
使用名称空间std;
//变数
字符密码(char);
字符密码(char);
int-y;
常量字符串LEFT=“1”;
常量字符串RIGHT=“0”;
//主代码
int main()
{
字符串输入;//声明临时存储的字符串变量
//消息框
int WinUserMessageBox(HWND-HWND,LPCTSTR-lpText,LPCTSTR-lpCaption,UINT-uType);//定义MessageBox参数
const int message=MessageBox(NULL,L“您的位置安全吗?”,L“警告”,MB_YESNOCANCEL);//设置MessageBox响应的侦听器
//开关响应
开关(消息)//初始化案例研究
{
如果选择“是”,case idies://将运行此操作
//密码块
做{
我相信你已经把你的功能互换了
if (z == LEFT)
{
for (int x = 0; x < input.length(); x++) // Loops until string.length is reached.
{
output += cipherR(input[x]);
}
} //Adds the value of expression to the value of a variable and assigns the result to the variable.
else if (z == RIGHT)
{
for (int x = 0; x < input.length(); x++) // Loops until string.length is reached.
{
output += cipherL(input[x]); //Adds the value of expression to the value of a variable and assigns the result to the variable.
}
};
我相信你已经完成了你的职能
if (z == LEFT)
{
for (int x = 0; x < input.length(); x++) // Loops until string.length is reached.
{
output += cipherR(input[x]);
}
} //Adds the value of expression to the value of a variable and assigns the result to the variable.
else if (z == RIGHT)
{
for (int x = 0; x < input.length(); x++) // Loops until string.length is reached.
{
output += cipherL(input[x]); //Adds the value of expression to the value of a variable and assigns the result to the variable.
}
};
看起来你认为-1%26
等于25。
它不是-它不是1就是-1,这取决于实现
在您的实现中,这显然是积极的,因为两个班次的结果相同
不能将模数用于左移。
相反,使用重复添加:
c = c - y;
while (c < 'A')
{
c += 26;
}
c=c-y;
而(c<'A')
{
c+=26;
}
您还需要查看为每个案例调用的函数,因为您已将它们切换。看起来您认为-1%26
是25。
它不是-它不是1就是-1,这取决于实现
在您的实现中,这显然是积极的,因为两个班次的结果相同
不能将模数用于左移。
相反,使用重复添加:
c = c - y;
while (c < 'A')
{
c += 26;
}
c=c-y;
而(c<'A')
{
c+=26;
}
您还需要查看每个案例调用的函数,因为您已将它们切换。问题是它总是使用密码
,而不是它总是使用与他要求的相反的函数。不敢相信我犯了这样一个新手错误,但我确实已将这些函数切换过来,感谢所有的支持帮助人员:)@Barmar我相信他说它总是使用右边,因为他总是尝试左边。问题是它总是使用cipher
,并不是说它总是使用与他要求相反的密码。不敢相信我犯了这样一个新手错误,但我确实已经将功能切换过来了,谢谢所有的帮助人员:)@Barmar我相信他是说它总是使用右边,因为他总是尝试左边。