C ++创建彩票猜测程序

时间:2017-02-25 06:23:05

标签: c++

好的,所以项目是创建一个由10个随机正整数组成的抽奖号码,用户可以猜测它直到用户猜出正确的数字。我的所有代码看起来都不错,但是当我运行程序并输入一个数字时,它会给我这个MSVS运行时库错误?我甚至不知道这意味着什么,因为我对编程很新。帮助将非常感谢!

Main.cpp的

#include <iostream>
#include <cmath>
#include <ctime>
#include "Lottery.h"

using namespace std;

int main() {
    const int size = 9; //declare variables
    int win[size];
    int g;
    srand(time(NULL));
    assign(win, size);
    draw(win, size);
    g = entry();
    if (check(win,size,g) == true) {
        cout << "Congradulations! You have won the lottery!" << endl;
    }
    else {
        cout << "Try again!" << endl;
    }
    printOut(g);
}

Lottery.cpp

#include <iostream>
#include <cmath>
#include "Lottery.h"

using namespace std;



int entry() {
    int guess;
    cout << "Enter a number from 0 to 99." << endl;
    cin >> guess;
    return guess;
}

void assign(int w[], int s) {
    for (int i = 0; i < s; i++) {
        w[s] = -1;
    }
}

bool check(int w[], int s, int g) {
    for (int i = 0; i < s; i++) {
        if (g == w[i]) {
            return true;
        }
    }
    return false;
}

void draw(int w[], int s) {
    for (int i = 0; i < s; i++) {
        int tmp = rand() % 100;
        if (check(w, s, tmp)) {
            i--;
        }
        else
            w[i] = tmp;
    }
}

void printOut(int g) {
    cout << "Numbers you have chosen:" << " " << g << endl;
}

Lottery.h

#ifndef LOTTERY_INCLUDED
#define LOTTERY_INCLUDED

void assign(int[], int);
bool check(int[], int, int);
void draw(int[], int);
int entry();
void printOut(int);

#endif //LOTTERY

1 个答案:

答案 0 :(得分:1)

调试教程可在其他地方获得。但如果发生了不好的事情,请不要惊慌失措并寻找指示。

首先,您的运行时错误:

your runtime error

这有一个链接“中断和打开例外设置”链接或“中断”按钮。如果单击它,将会将您带到main的末尾。

详细说明我们在win附近做了一些不好的事。

看看这个:

void assign(int w[], int s) {
    for (int i = 0; i < s; i++) {
        w[s] = -1; //<------Oh oops!
    }
}

我们知道数组的长度是s,即9,并且正在使用w[s],我们明确指出w[i]。 错误中的额外细节告诉您可能的地方。