-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