为什么这段代码不影响输出?

Why is this code not affecting the output?

本文关键字:影响 输出 代码 段代码 为什么      更新时间:2023-10-16

这应该接受输入并将每个字母向右移动1。暂停会阻止它做任何事情吗?

我怎样才能改变它,使它不只是输出用户输入的内容?

这是Visual Studio Community 2013中的c++代码:

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
#include <cctype>

int _tmain(int argc, _TCHAR* argv[])
{
    string cyphertext;
    cout << "Paste your cyphertext and press enter to shift right 1: ";
    cin >> cyphertext;
    void encrypt(char * cyphertext, unsigned int offset);
    for (int i = 0; cyphertext[i] != 0; i++) {
        char firstLetter = islower(cyphertext[i]) ? 'a' : 'A';
        unsigned int alphaOffset = cyphertext[i] - firstLetter;
        int offset = 0;
        unsigned int newAlphaOffset = alphaOffset + offset;
        cyphertext[i] = firstLetter + newAlphaOffset % 26;
        cout << "" << "Right One: " << cyphertext;
        system("pause");
        return 0;
    }
}

您的pause在'加密'循环内。它需要在外面。循环中的return将终止程序;这也需要在循环之外。

请注意,当代码在正统的布局中缩进时,更容易看到这样的错误,比如现在的问题。使用粗糙的布局使得很难发现许多问题,而这些问题在代码布局整齐时是很明显的。

你还声明了一个从未使用过的函数encrypt();不要那样做。在函数内部声明函数通常不是一个好主意。鉴于没有定义encrypt()函数,没有'void函数',所以我为您更改了问题标题。