Int作为随机7位组合返回

时间:2015-10-12 08:29:58

标签: c++ random dice

我试图为我的C ++课程编写一个程序而且我遇到了一个奇怪的问题。返回int"骰子"由于某种原因,总是选择数字和字母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;
}

2 个答案:

答案 0 :(得分:1)

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

cout << dice() << endl;

答案 1 :(得分:1)

替换(打印功能&#39; s地址)

cout << dice << endl;

with(调用函数和打印结果)

cout << dice() << endl;

其余代码似乎正常。您还应该将NULL替换为nullptr - 一种表达空指针的现代方法。这也会删除编译器警告。

BTW:未使用printScreen功能。在提出问题时,您应该给我们一个最小的工作示例。这不是最小的。