Gee. 2022-12-23 20:41 采纳率: 53.3%
浏览 18
已结题

C++运行出现问题,请问怎样解决?

问题遇到的现象和发生背景

c++运行的时候出现了问题,请问怎样解决?

遇到的现象和发生背景,请写出第一个错误信息

有三个问题
第一个问题是我无法输入k, 就是说还没等到我输入k值,程序就报错了
第二个问题是,就是我明明输入的是abc等字母,怎么运行结果出现这么多乱码(那些中文的字)呢?

img

第三个问题就是图片中显示的问题了

img

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#include<iostream>
#include<fstream>
const int Num = 20;
using namespace std;
void KaiSa(char* str, int k) {
    while (*str) {
        if ((*str + k <= 'z' && *str + k >= 'a') && (*str <= 'z' && *str >= 'a')) {
            *str++ = *str + k;
        }
        else if ((*str + k <= 'Z' && *str + k >= 'A') && (*str <= 'Z' && *str >= 'A')) {
            *str++ = *str + k;
        }
        else if (k >= 0) {
            *str++ = *str + k - 26;
        }
        else if (k < 0) {
            *str++ = *str + k + 26;
        }
    }
}
int main()
{
    char array [20];
    int k;
    int choice;
    char ch;
    int count = 0;
    fstream f1;
    cout << "请输入明文: "<<endl;
    f1.open("file1.txt", ios::out);
    
    do
    {
        ++count;
        cin.get(ch);
        f1 << ch;
    } while (!cin.fail());
    f1.close();

    f1.open("file1.txt", ios::in);
    for (int i = 0; i < count; i++)
    {
        f1 >> array[i];
        cout << array[i];
    }
    f1.close();
    cout << "请输入密钥:"<<endl;
    cin >> k;

    if (k > 25 || k < -25) {
        k %= 26;
    }
    KaiSa(array, k);
    cout << "进行凯撒加密后的密码为:\n";
    cout << array << endl;
    fstream f2;
    f2.open("file2.txt", ios::out);
    for (int i = 0; i < count; i++)
    {
        f2 << array[i];
    }
    f2.close();
    KaiSa(array, -k);
    cout << "进行凯撒解密后的密码为:\n";
    cout << array << endl;
    fstream f3;
    f3.open("file3.txt", ios::out);
    for (int i = 0; i < count; i++)
    {
        f3 << array[i];
    }
    f3.close();
     
    return 0;
}
运行结果及详细报错内容
我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”
  • 写回答

2条回答 默认 最新

  • 会修bug的猫 2022-12-23 20:58
    关注

    修改后如下
    主要存在两个问题:
    1.cin.fail()不要乱用
    2.array没有在末尾添加 \0

    
    #include<iostream>
    #include<fstream>
    const int Num = 20;
    using namespace std;
    void KaiSa(char* str, int k) {
        while (*str) {
            if ((*str + k <= 'z' && *str + k >= 'a') && (*str <= 'z' && *str >= 'a')) {
                *str++ = *str + k;
            }
            else if ((*str + k <= 'Z' && *str + k >= 'A') && (*str <= 'Z' && *str >= 'A')) {
                *str++ = *str + k;
            }
            else if (k >= 0) {
                *str++ = *str + k - 26;
            }
            else if (k < 0) {
                *str++ = *str + k + 26;
            }
        }
    }
    int main()
    {
        char array[20];
        int k;
        int choice;
        char ch;
        int count = 0;
        fstream f1;
        cout << "请输入明文: " << endl;
        f1.open("file1.txt", ios::out);
        do
        {
            cin.get(ch);
            if (ch == '\n')
                break;
            ++count;
            f1 << ch;
        } while (1);
        f1.close();
        f1.open("file1.txt", ios::in);
        for (int i = 0; i < count; i++)
        {
            f1 >> array[i];
            cout << array[i];
        }
        array[count] = '\0';
        cout << endl;
        f1.close();
        cout << "请输入密钥:" << endl;
        cin >> k;
        if (k > 25 || k < -25) {
            k %= 26;
        }
        KaiSa(array, k);
        cout << "进行凯撒加密后的密码为:\n";
        cout << array << endl;
        fstream f2;
        f2.open("file2.txt", ios::out);
        for (int i = 0; i < count; i++)
        {
            f2 << array[i];
        }
        f2.close();
        KaiSa(array, -k);
        cout << "进行凯撒解密后的密码为:\n";
        cout << array << endl;
        fstream f3;
        f3.open("file3.txt", ios::out);
        for (int i = 0; i < count; i++)
        {
            f3 << array[i];
        }
        f3.close();
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月31日
  • 已采纳回答 12月23日
  • 创建了问题 12月23日

悬赏问题

  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,