如果语句不工作,输入无法识别(c++)

If Statement not working, input unrecognised (C++)

本文关键字:识别 c++ 输入 语句 工作 如果      更新时间:2023-10-16

我的程序运行没有问题,编译没有错误。

我的if语句的目的是选择字母是否向左/向右移动,我试图通过导致2个函数的if语句实现,但是无论cin >> z;的输入如何,它都将始终使用CipherR。有人能帮我弄明白吗?

//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 shiftn";
            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

我相信你已经交换了你的函数。在

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

当你进入"left" if语句时,当你应该调用cipherL()时,你正在调用cipherR()。您还需要修复"正确",因为您正在调用cipherL()。固定版本:

if (z == LEFT)
{
    for (int x = 0; x < input.length(); x++)        // Loops until string.length is reached.
    {
        output += cipherL(input[x]);
        //              ^ L here
    }
}                       //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 += cipherR(input[x]);        //Adds the value of expression to the value of a variable and assigns the result to the variable.
        //              ^ R here
    }
};

看起来你认为-1 % 26是25。
它不是-它要么是1要么是-1,取决于实现。

在你的实现中,这显然是积极的,因为你得到了相同的结果,两个班次。

不能对左移使用模数
相反,使用重复加法:

c = c - y;
while (c < 'A')
{
    c += 26;
}

您还需要查看您在每种情况下调用的函数,因为您将它们交换了。