gpt4 book ai didi

c++ - Int 作为随机 7 位数字组合返回

转载 作者:行者123 更新时间:2023-11-28 02:19:19 25 4
gpt4 key购买 nike

我正在尝试为我的 C++ 类编写程序,但遇到了一个奇怪的问题。由于某种原因,返回 int "dice"总是选择数字和字母 c 的随机组合。起初我以为我的随机数生成器是错误的,但即使我设置 dice=5 它也返回相同的相似随机字符。这是完整的代码,但据我所知,相关部分只是 int dice 和 main。

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;
int score, devilScore, tempScore, devilTempScore;
char input() {
char c;
bool valid=0;

while (!valid) {
cout << "[p]ass, [r]oll, or [f]orfeit: ";
cin >> c;
if (c == 'p' || c == 'r' || c == 'f') valid = true;
else cout << "Input invalid\n";
}
return c;
}

int dice() {
int dice;
dice = (rand() % 6) + 1;
return dice;
}


void printScreen() {
switch(1){
case 1: cout << " Player Devil \n";
case 2: cout << " 100 100 \n";
case 3: cout << " "; if (score <= 99 && score >= 95) { cout << score << ">"; }
else { cout << " "; }cout << "95 95"; if (devilScore <= 99 && devilScore >= 95) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 4: cout << " "; if (score <= 94 && score >= 90) { cout << score << ">"; }
else { cout << " "; }cout << "90 die 90"; if (devilScore <= 94 && devilScore >= 90) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 5: cout << " "; if (score <= 89 && score >= 85) { cout << score << ">"; }
else { cout << " "; }cout << "85 ___ 85"; if (devilScore <= 89 && devilScore >= 85) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 6: cout << " "; if (score <= 84 && score >= 80) { cout << score << ">"; }
else { cout << " "; }cout << "80 |" << dice << "| 80"; if (devilScore <= 84 && devilScore >= 80) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 7: cout << " "; if (score <= 79 && score >= 90) { cout << score << ">"; }
else { cout << " "; }cout << "75 ___ 75"; if (devilScore <= 79 && devilScore >= 75) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 8: cout << " "; if (score <= 74 && score >= 70) { cout << score << ">"; }
else { cout << " "; }cout << "70 70"; if (devilScore <= 74 && devilScore >= 70) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 9: cout << " "; if (score <= 69 && score >= 65) { cout << score << ">"; }
else { cout << " "; }cout << "65 65"; if (devilScore <= 69 && devilScore >= 65) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 10: cout << " "; if (score <= 64 && score >= 60) { cout << score << ">"; }
else { cout << " "; }cout << "60 60"; if (devilScore <= 64 && devilScore >= 60) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 11: cout << " "; if (score <= 59 && score >= 55) { cout << score << ">"; }
else { cout << " "; }cout << "55 55"; if (devilScore <= 59 && devilScore >= 55) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 12: cout << " "; if (score <= 54 && score >= 50) { cout << score << ">"; }
else { cout << " "; }cout << "50 50"; if (devilScore <= 54 && devilScore >= 50) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 13: cout << " "; if (score <= 49 && score >= 45) { cout << score << ">"; }
else { cout << " "; }cout << "45 45"; if (devilScore <= 49 && devilScore >= 45) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 14: cout << " "; if (score <= 44 && score >= 40) { cout << score << ">"; }
else { cout << " "; }cout << "40 40"; if (devilScore <= 44 && devilScore >= 40) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 15: cout << " "; if (score <= 39 && score >= 35) { cout << score << ">"; }
else { cout << " "; }cout << "35 35"; if (devilScore <= 39 && devilScore >= 35) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 16: cout << " "; if (score <= 34 && score >= 30) { cout << score << ">"; }
else { cout << " "; }cout << "30 30"; if (devilScore <= 34 && devilScore >= 30) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 17: cout << " "; if (score <= 29 && score >= 25) { cout << score << ">"; }
else { cout << " "; }cout << "25 25"; if (devilScore <= 29 && devilScore >= 25) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 18: cout << " "; if (score <= 24 && score >= 20) { cout << score << ">"; }
else { cout << " "; }cout << "20 20"; if (devilScore <= 24 && devilScore >= 20) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 19: cout << " "; if (score <= 19 && score >= 15) { cout << score << ">"; }
else { cout << " "; }cout << "15 15"; if (devilScore <= 19 && devilScore >= 15) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 20: cout << " "; if (score <= 14 && score >= 10) { cout << score << ">"; }
else { cout << " "; }cout << "10 10"; if (devilScore <= 14 && devilScore >= 10) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 21: cout << " "; if (score <= 9 && score >= 5) { cout << score << ">"; }
else { cout << " "; }cout << " 5 5 "; if (devilScore <= 9 && devilScore >= 5) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }
case 22: cout << " "; if (score <= 4 && score >= 0) { cout << score << ">"; }
else { cout << " "; }cout << " 0 0 "; if (devilScore <= 4 && devilScore >= 0) { cout << "<" << devilScore << " \n"; }
else { cout << " \n"; }

}






}


int main() {
srand (time(NULL));
int score = 30;
int devilScore = 25;

cout << dice << endl;
}

最佳答案

您正在打印函数 int dice() 的地址;如果你想调用它,应该加上括号:

cout << dice() << endl;

关于c++ - Int 作为随机 7 位数字组合返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33076519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com