2016-04-24 8 views
-1

Ich bin neu in C++ und versuche, ein einfaches Programm zu erstellen, das mit der Benutzereingabe zum Fortfahren eine zufällige links oder rechts generieren wird. Ich hatte das Programm richtig arbeiten, bis ich in das Array hinzugefügt, um zu versuchen und jedes Element zu speichern, da ich sie so bald ausgeben muss und der Benutzer die Schleife beenden möchte. Das Programm scheint gut zu kompilieren, aber zur Laufzeit erhalte ich "Unhandled exception at 0x012B1CA9" Jede Hilfe würde sehr geschätzt werden.Unbehandelte Ausnahme bei 0x012B1CA9

#include <iostream> 
#include <ctime> 
using namespace std; 

int main() 
{ 

int userSelection = 1; 
const int MAX = '100'; 
int randNum(0); 
int one (0); 
int two (0); 
int total(0); 
int sel[MAX]; 

do 
{ 
    cout << "Press 1 to pick a side or 0 to quit: "; 
    cin >> userSelection; 



    for (int i = 1; i < MAX; i++) 
    { 
     srand(time(NULL)); 
     sel[i] = 1 + (rand() % 2); 

     if (sel[i] == 1) 
     { 
      cout << "<<<--- Left" << endl; 
      one++; 
      total++; 
     } 
     else 
     { 
      cout << "Right --->>>" << endl; 
      two++; 
      total++; 
     } 
    } 


} while (userSelection == 1); 

cout << "Replaying Selections" << endl; 
for (int j = 0; j < MAX; j++) 
{ 
    cout << sel[j] << endl; 
} 

cout << "Printing Statistics" << endl; 
double total1 = ((one/total)*100); 
double total2 = ((two/total)*100); 
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl; 
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl; 

system("pause"); 
return 0; 
}; 
+2

ich frage mich, warum Ihr Compiler nicht von 'const int MAX beschwerte = '100' ; ' – WhiZTiM

+0

Ich bin mir nicht sicher, ob ich Visual Studio 2015 verwende. Was sollte die Syntax sein? – CA444

+3

'const int MAX = 100;' –

Antwort

0

Sie haben ein Multi-Charakter hier konstant ... und das Verhalten gehen nicht wie erwartet ...

Ändern Sie diese Zeile

const int MAX = '100'; 

zu

const int MAX = 100; 

Hinweis die entfernten einfachen Anführungszeichen.

Und zweitens, ich werde Sie beraten die Samen des C Zufallsgenerator aus dem for-Schleife zu entfernen, weil Sie wahrscheinlich die gleichen Werte aus den rand() erhalten, wenn Sie es immer sofort rufen nach dem Aussäen ...

aber bevorzugt Verwendung des Algorithmus von C++'s random header

Hier ist eine korrigierte Version des Original-Code ....

#include <iostream> 
#include <ctime> 
using namespace std; 

int main() 
{ 

int userSelection = 1; 
const int MAX = 100;  // <---changed 
int randNum(0); 
int one (0); 
int two (0); 
int total(0); 
int sel[MAX]; 

do 
{ 
    cout << "Press 1 to pick a side or 0 to quit: "; 
    cin >> userSelection; 


    srand(time(NULL)); //< moved to here 
    for (int i = 0; i < MAX; i++)  // <-- modified starting index 
    { 
     sel[i] = 1 + (rand() % 2); 

     if (sel[i] == 1) 
     { 
      cout << "<<<--- Left" << endl; 
      one++; 
      total++; 
     } 
     else 
     { 
      cout << "Right --->>>" << endl; 
      two++; 
      total++; 
     } 
    } 


} while (userSelection == 1); 

cout << "Replaying Selections" << endl; 
for (int j = 0; j < MAX; j++) 
{ 
    cout << sel[j] << endl; 
} 

cout << "Printing Statistics" << endl; 
double total1 = ((one/total)*100); 
double total2 = ((two/total)*100); 
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl; 
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl; 

system("pause"); 
return 0; 
}; 
+0

Er ist auch 1-basiert Indexierung seiner ersten Schleife, die er korrigieren könnte. – kfsone

+0

Danke, das hat den Fehler behoben. Ich bemerke, dass es scheint, dass das Array nicht alle Nummern speichert. Hast du eine Idee warum? – CA444

+0

@kfsone Wäre es besser, wenn mein Rand 0-1 statt 1-2 wäre? – CA444

0

Ich denke, dass es im Grunde eine gute Idee ist, mehr über C-Datentypen und Deklaration zu lesen. Ihr Fehler:

const int MAX = '100' sollte const int MAX = 100 ohne Anführungszeichen sein. C++ führt implizite Konvertierung von Zeichenliteralen zu int durch.

Verwandte Themen