Having problem with code

Hello i am new to coding i suck at math but i like doing it anyway. so don't judge me, i am trying to have the user enter the difference in years between bob and Kerry. if the years are correct then i want it to print "correct" if not i want it to loop " try again " until the user gets it right here is my code so far. also i would only like positive random numbers and for bob to always be younger in years than Kerry. please help thank you
UPDATED CODE (SOLVED)
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
srand((unsigned)time(0)); // randnumber variable
double bnum,knum,yrs;
bnum = (rand()%500);
knum =(rand()%500+2000);
yrs = knum - bnum;
cout<<"Bob is "<< bnum <<" years old."<< endl;
cout<<"Kerry is "<< knum <<" years old."<< endl;
cout<<"How many years seperate Kerry from Bob?"<< endl;
cout<<"Enter a number below then press ENTER: "<< endl;

do {
cin >> yrs;
if (yrs > knum-bnum )
cout<<"Go lower..."<< endl;
else if (yrs < knum-bnum)
cout<< " Go higher.."<< endl;
else
cout << "Great Job!..";
cout << endl;
}while ( yrs != knum-bnum );
getchar();

system ("PAUSE");
return 0;
}

Last edited on
Please first read http://www.cplusplus.com/forum/beginner/1/

Also int (bob); does this even compile? I guess you meant
int bob;

int(bob) tries to call constructor of int (at least the equivalent of constructor since int is a built-in type) with argument bob. I don't know if it will compile but int(10) produce a new int and initiate it to 10.
So even if it compiles it ineffective as a command:
int(10); //it does nothing although it compiles fine

 
rand()% 2000
this will produce 0-1999 not 2000.

If you want bob to be younger assign it a value upto Kerry's age:
1
2
kerry = (rand()%2000);
bob = rand()%kerry;


By the way really impressive the age of your character!
Last edited on
Thank you eypros.
I tried to do something that incorporates all the lessons i read in my book which is like 2 chapters and 5 lesson examples. but i realized i need to do more in order for it to work. as for the age lol it was just something to do .
i just cant get it to do the math right and then print out correct then exit the program or if its wrong have print wrong try again and have the user renter the age until it is correct
all i want is this program to is create two random number, one for bnum and another for knum and have the user submit how many years separate knum from bnum and if they are correct print cout>>"correct"; if they are wrong print cout>>"wrong try again"; and have the user keep entering numbers until they get it correct . and when they get it correct have it end the program. I am new to this so please explain stuff to me like i am 5.
Topic archived. No new replies allowed.