You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

【求助】C++随机字符串程序忽略cin输入且输出异常

问题描述

我写了一段C++代码,想从字符串数组里随机生成字符串。预期逻辑是:输出随机字符串,killcount变量自增1,询问用户是否继续执行,根据输入输出计数数值,若选择停止则将again布尔变量设为false。但实际运行时程序直接忽略所有cin输入,输出异常内容后退出,运行输出如下:

Toaster Bathinal death total is:
C:\Users\MH\source\repos\suicidefun\x64\Debug\suicidefun.exe (process 25852) exited with code 0。

原代码:

// imports
#include <iostream>
using namespace std;

//var
string methods[21] = { "Hanging", "Shooting", "Poisoning", "Drinking Bleach", "Toaster Bath", "something idk", "Eating something you are allergic to", "Fall damage", "Mr. Hands", "Stabbing", "Shoving a pointy object in you", "Thinking you are in a dream and doing something stupid","Drunk driving","Overdose","Drowning","Fire","Starving or dehydrating","Driving off a cliff","Driving into a family of five's ford fiesta","Eat glass","Suicide by cop" };
time_t current_time = time(NULL);
int random;
bool again = true;
int killcount = 0;
string x = "N/A";
int check = 0;
bool valid = false;
char ans = 'n';


void thing(){   
    srand((unsigned)time(NULL));
    int random = rand() % 20;
    cout << methods[random];
    killcount++;
    while (valid = false) {
        cout << "Kill yourself again? (y/n)";
        cin >> x;
        if (x != "y" || "n" || "Y" || "N") {
            cout << "invalid answer";
        }
        else {
            valid = true;
            break;
        }
    }
    if (x == "y") {
        cout << "Current death total is:" + killcount;
    }
    else if (x == "Y") {
        cout << "Current death total is:" + killcount;
    }
    else {
        again = false;
        cout << "Final death total is:" + killcount;
    }
}

int main() {
    thing();
    return 0;
}
错误分析
  • 循环条件错误:while (valid = false)是赋值操作而非逻辑判断,导致循环直接跳过,根本不会触发用户输入步骤。应改为while (!valid)或while (valid == false)。
  • 输入校验逻辑错误:if (x != "y" || "n" || "Y" || "N")写法完全错误,"n"这类字符串字面量在布尔语境下永远为真,所以该条件始终成立,所有输入都会被判定为无效。正确写法是if (x != "y" && x != "n" && x != "Y" && x != "N")。
  • 字符串与整数拼接错误:"Current death total is:" + killcount是将字符串字面量的指针与整数相加,导致内存偏移,输出乱码。正确做法是用cout的流式输出:cout << "Current death total is:" << killcount;。
  • 随机数范围不匹配:数组methods包含21个元素,但rand() % 20只会生成0-19的随机数,最后一个元素永远无法被选中,应改为rand() % 21。
  • 冗余全局变量:current_time、全局的random、check、ans均未使用,属于无效代码,可直接删除。
修正后的代码
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

string methods[21] = { 
    "Hanging", "Shooting", "Poisoning", "Drinking Bleach", "Toaster Bath", 
    "something idk", "Eating something you are allergic to", "Fall damage", 
    "Mr. Hands", "Stabbing", "Shoving a pointy object in you", 
    "Thinking you are in a dream and doing something stupid","Drunk driving",
    "Overdose","Drowning","Fire","Starving or dehydrating",
    "Driving off a cliff","Driving into a family of five's ford fiesta",
    "Eat glass","Suicide by cop" 
};
bool again = true;
int killcount = 0;
string x;
bool valid = false;

void thing(){   
    srand((unsigned)time(NULL));
    int random = rand() % 21;
    cout << methods[random] << endl;
    killcount++;
    
    valid = false;
    while (!valid) {
        cout << "Kill yourself again? (y/n): ";
        cin >> x;
        if (x != "y" && x != "n" && x != "Y" && x != "N") {
            cout << "Invalid answer, please enter y/n" << endl;
        }
        else {
            valid = true;
        }
    }
    
    if (x == "y" || x == "Y") {
        cout << "Current death total is: " << killcount << endl;
        thing();
    }
    else {
        again = false;
        cout << "Final death total is: " << killcount << endl;
    }
}

int main() {
    thing();
    return 0;
}

内容的提问来源于stack exchange,提问作者Thomas Hall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 10:10:54