Simple project becomes very annoying.
First of all, let me say right up front that I am very novice to C++. ive been playing around with it the past couple weeks and doing as much research as I could, and I reached the point where I believed I could design a simple game.
boy, was I wrong.....
I've searched for answers to my problem, and since I'm not entirely sure what my problem is, I have turned to the wise sages on this message board for assistance.
I am going to state all the problems up front, then explain them later:
1. the program suddenly crashes for no apparent reason. (I have dug through my code, and there doesnt seem to be a problem. It is all stuff I have done before)
2. a certain value in my program always returns the same, even though its supposed to be random. I have played around with random numbers before, and I thought I had the hang of it.
here is my code: if you dont feel like reading the whole thing, skip it. I will explain afterwards where exactly I think the problems are, so you dont have to read the whole thing. But I may be wrong, so here is the code to use for referance.
#include <stdio.h>
#include <math.h> //I included this becuase I thought
I would need it origionally.
#include <stdlib.h>
#include <time.h>
int hand ();
void main() {
int choice;
int pick;
int score = 20;
bool quit = false;
int hidden;
printf("This is a simple game of chance. The rules are as follows:\n");
printf("I will show you five numbers. One of these numbers is the correct\n");
printf("number, and the other four are fake. If you chose the correct\n");
printf("number, that number will be added to your score.\n");
printf("Every hand subtracts two points from your score. you start with 20 points.\n");
printf("Here is the catch, though:\n");
printf("The higher the number is, the less likely it is to be the correct one,\n");
printf("but you get more points for higher numbers.\n");
printf("\n");
printf("Good luck!\n");
system ("PAUSE");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n");
while ( ! quit ) {
hidden = hand();
printf(" 1. pick\n 2. quit\n 3. score\n");
printf("Enter a selection number:");
scanf("%d", choice); //CRASH HAPPENS AFTER INPUT HERE.
if (choice == '2')
quit = true;
if (choice == '3')
printf("%d", score);
if (choice == '1') {
score = score - 2;
scanf("Enter a number choice: %d", pick);
if (pick == hidden) {
score = score + hidden;
printf("That is correct! Your score is now %d\n", score);
}
if (pick != hidden) {
printf("Wrong! Your score is now %d\n", score);
}
}
}
}
int hand() { // function for creating hand and
assigning one number to 'hidden'
int a, b, c, d, e, hidden; // the 5 random numbers
and the 'hidden'
int aasg, basg, casg, dasg, easg; // intergers used to
assign weight to numbers
so that larger numbers have
less chance of being selected
(asg = assign)
int aper, bper, cper, dper, eper; // per = percent. not really
a percent, but used to decide
which number is selected.
srand ( time (NULL) );
a = ( rand() % 20 ) + 1;
b = ( rand() % 20 ) + 1;
c = ( rand() % 20 ) + 1;
d = ( rand() % 20 ) + 1;
e = ( rand() % 20 ) + 1;
printf("%d %d %d %d %d\n\n", a, b, c, d, e);
if ( 0 < a <= 5 ) // assigning weight
aasg = 4;
if ( 5 < a <= 10 )
aasg = 3;
if ( 10 < a <= 15 )
aasg = 2;
if ( 15 < a <= 20 )
aasg = 1;
if ( 0 < b <= 5 )
basg = 4;
if ( 5 < b <= 10 )
basg = 3;
if ( 10 < b <= 15 )
basg = 2;
if ( 15 < b <= 20 )
basg = 1;
if ( 0 < c <= 5 )
casg = 4;
if ( 5 < c <= 10 )
casg = 3;
if ( 10 < c <= 15 )
casg = 2;
if ( 15 < c <= 20 )
casg = 1;
if ( 0 < d <= 5 )
dasg = 4;
if ( 5 < d <= 10 )
dasg = 3;
if ( 10 < d <= 15 )
dasg = 2;
if ( 15 < d <= 20 )
dasg = 1;
if ( 0 < e <= 5 )
easg = 4;
if ( 5 < e <= 10 )
easg = 3;
if ( 10 < e <= 15 )
easg = 2;
if ( 15 < e <= 20 )
easg = 1;
aper = aasg; // giving the ()per values
ranges. such as
aper = from 0 to aasg
bper = basg + aasg; // bper looks like its
actually from 0 to basg,
but I clear that up below
cper = casg + basg + aasg;
dper = dasg + casg + basg + aasg;
eper = easg + dasg + casg + basg + aasg;
hidden = ( rand() % ( aasg + basg + casg + dasg + easg ) ) + 1;
if ( hidden <= aper )
hidden = a;
if ( aper < hidden <= bper ) // giving bper its true
range: from aper to bper
hidden = b;
if ( bper < hidden <= cper )
hidden = c;
if ( cper < hidden <= dper )
hidden = d;
if ( dper < hidden <= eper ) // hidden always has value
hidden = e; of e, no matter what.
(not supposed to happen)
return hidden; // allowing hidden to be
extracted from this function
}
ok, whew.... thats the end of my code. (I also attached the file for those who dont like reading it inside of a little post, and would like to see the whole file as it appears to me)
after the initial introduction, I show them the hand and give them options. I have this bit of code seperated from the rest, and the comment "//CRASH HAPPENS AFTER INPUT HERE" so that it is easier to find. no matter what they put as input, they get an illegal opperation.
thats the first of my problems. solving that is a priority, and I have no idea where to go to solve it.
second, a minor code problem, my hidden value is always the last number in the hand (in the code, this is the 'e' value). I had the program print the hidden value and I ran through it multiple times. my question is this: are multiple operators allowed in "IF" statements? such as:
if ( cper < hidden <= dper )
hidden = d;
does that statement actually work in determining of hidden falls between cper and dper, or does it only look at the part after hidden. the reason I ask is because that is the only thing I can think of that would always give hidden the 'e' value, because
if (dper < hidden <= eper)
hidden = e;
,since it is the last statement , would always assign e to hidden, since hidden will always be <= eper.
well, guys... what do you think? I know it is a long post, but this was the only way I could state my problem, and this place is the only place I know of that would probably help solve it.
thanks ahead of time, and I appreciate your patience in wading through this very looong post.