-4
好的,项目是创建一个由10个随机正整数组成的彩票号码,用户可以猜测它,直到用户猜到正确的号码。我所有的代码看起来不错,但是当我运行程序并输入一个数字时,它会给我这个MSVS运行时库错误?我甚至不知道它是什么意思,因为我对编程相当陌生。帮助将非常感激!C++创建彩票猜测程序
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
在较少的狙击手方面,当Visual Studio停止程序时,它通常会提供打破和使用调试器来查看发生了什么的选项。我强烈建议这样做。比交上罐装答案更有教育意义。 – user4581301
不尝试创建错误。试图解决一个!标题中有拼写错误。 –
偏题:建议在'main'的顶部附近调用'srand'来为随机数生成器播种。 – user4581301