Quick help, Please.
Sep 24, 2014 at 11:57am UTC
Hi, I'm new to C++ and I can't see whats wrong with this High Low Program. If you have any bits that work faster and make less lag feel free to comment. But my biggest problem is that an error shows up and it says : "Run-Time Check Failure #3 - The variable 'score' is being used without being initialized." Then my score changes to -818931839.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <windows.h>
#include <string>
using namespace std;
void setcolor(unsigned short color) //The function that you'll use to
{ //set the colour
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
bool game_running = true ;
int main()
{
while (game_running == true )
{
int score = 0;
int num = 0;
int num2 = 0;
string letter;
srand(time(NULL));
num = 1 + rand() % (10 - 1 + 1);
num2 = 1 + rand() % (10 - 1 + 1);
cout << "\nPoints " ;
setcolor(10);
cout << score << endl;
setcolor(7);
cout << "The first number is " ;
setcolor(4);
cout << num << endl;
setcolor(7);
cout << "Will it be higher or lower? " ;
cin >> letter;
cout << "The second number was " ;
setcolor(10);
cout << num2 << endl;
if (letter == "L" ||letter == "l" ){
if ( num>num2) {
cout << "YOU WIN" ;
int score = score+1;
system("PAUSE>nul" );
system("CLS" );
srand(time(NULL));
num = 1 + rand() % (10 - 1 + 1);
num2 = 1 + rand() % (10 - 1 + 1);
cout << "\nPoints " ;
setcolor(10);
cout << score << endl;
setcolor(7);
cout << "The first number is " ;
setcolor(4);
cout << num << endl;
setcolor(7);
cout << "Will it be higher or lower? " ;
cin >> letter;
cout << "The second number was " ;
setcolor(10);
cout << num2 << endl;
system("CLS" );
system("PAUSE>nul" );
}
else if (num<num2) {
setcolor(4);
cout << "FAIL\n" ;
system("PAUSE>nul" );
}
else if (num==num2) {
setcolor(5);
cout << "SAME NUMBER BUT SINCE I'M NICE I'LL GIVE YOU A POINT\n" ;
int score = score+1;
system("PAUSE>nul" );
system("CLS" );
srand(time(NULL));
num = 1 + rand() % (10 - 1 + 1);
num2 = 1 + rand() % (10 - 1 + 1);
cout << "\nPoints " ;
setcolor(10);
cout << score << endl;
setcolor(7);
cout << "The first number is " ;
setcolor(4);
cout << num << endl;
setcolor(7);
cout << "Will it be higher or lower? " ;
cin >> letter;
cout << "The second number was " ;
setcolor(10);
cout << num2 << endl;
system("CLS" );
system("PAUSE>nul" );
}
}
if (letter == "H" ||letter == "h" ){
if ( num<num2) {
cout << "YOU WIN" ;
int score = score+1;
system("PAUSE>nul" );
system("CLS" );
srand(time(NULL));
num = 1 + rand() % (10 - 1 + 1);
num2 = 1 + rand() % (10 - 1 + 1);
cout << "\nPoints " ;
setcolor(10);
cout << score << endl;
setcolor(7);
cout << "The first number is " ;
setcolor(4);
cout << num << endl;
setcolor(7);
cout << "Will it be higher or lower? " ;
cin >> letter;
cout << "The second number was " ;
setcolor(10);
cout << num2 << endl;
system("CLS" );
system("PAUSE>nul" );
}
}
else if (num>num2) {
setcolor(4);
cout << "FAIL\n" ;
system("PAUSE>nul" );
}
else if (num==num2) {
setcolor(5);
cout << "SAME NUMBER BUT SINCE I'M NICE I'LL GIVE YOU A POINT\n" ;
int score = score+1;
system("PAUSE>nul" );
system("CLS" );
srand(time(NULL));
num = 1 + rand() % (10 - 1 + 1);
num2 = 1 + rand() % (10 - 1 + 1);
cout << "\nPoints " ;
setcolor(10);
cout << score << endl;
setcolor(7);
cout << "The first number is " ;
setcolor(4);
cout << num << endl;
setcolor(7);
cout << "Will it be higher or lower? " ;
cin >> letter;
cout << "The second number was " ;
setcolor(10);
cout << num2 << endl;
system("CLS" );
system("PAUSE>nul" );
}
}
}
Sep 24, 2014 at 12:16pm UTC
Lines 47 77 103 135: you are declaring new variable called score which shadows previous one and trying to assign it itself + 1.
Sep 24, 2014 at 12:22pm UTC
Oh so I remove the int bit?
Sep 24, 2014 at 12:24pm UTC
Oo thanks worked, but the score doesn't get saved when it restarts ( When a higher, Same, or lower is executed twice it restarts ) Any tips for that?
Sep 24, 2014 at 1:00pm UTC
Your variable declared inside loop. This means that it would be recreated each time loop restarts.
Sep 24, 2014 at 1:49pm UTC
Thanks! Super helpful.
Topic archived. No new replies allowed.