2017-02-25 35 views
-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 
+2

在較少的狙擊手方面,當Visual Studio停止程序時,它通常會提供打破和使用調試器來查看發生了什麼的選項。我強烈建議這樣做。比交上罐裝答案更有教育意義。 – user4581301

+0

不嘗試創建錯誤。試圖解決一個!標題中有拼寫錯誤。 –

+0

偏題:建議在'main'的頂部附近調用'srand'來爲隨機數生成器播種。 – user4581301

回答

1

調試教程可以在其他地方。但是如果發生不好的事情,請不要驚慌並尋找指示。

首先,你的運行時錯誤:

your runtime error

這有一個鏈接「打破和開放的例外設置」鏈接或「中斷」按鈕。如果你點擊它,將會將你帶到主體的盡頭。

細節說我們在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]。 錯誤中的額外細節告訴你一個可能的地方。