Thursday, July 30, 2009

Do you know c++ programming? I need help, I can't get this one to work.?

I'm supposed to make a program that generates 2 random numbers between 10-99 and then asks you for the answer, if you get it incorrect you have to keep trying again until you get it correct.











int main ( )


{





int answer=0;


int value=0;


int ran1=0;


int ran2=0;


int total=0;


int high=0;


int low=0;








srand ((unsigned int) (time (0)));


cout %26lt;%26lt; rand ()%26lt;%26lt; endl;


cout %26lt;%26lt; rand ()%26lt;%26lt; endl;


value = (rand() % 90) +10;


cout %26lt;%26lt; "What is the first number generated? ";


cin %26gt;%26gt; ran1;


cout %26lt;%26lt; "What is the second number generated? ";


cin %26gt;%26gt; ran2;


cout %26lt;%26lt; "What is the total? ";


cin %26gt;%26gt; total;


total=ran1+ran2;


while (answer%26lt;total || answer%26gt;total)


{


cout %26lt;%26lt;"Incorrect!"%26lt;%26lt;endl;


cout %26lt;%26lt;"Please, try again.";


cin %26gt;%26gt; answer;


}


cin %26gt;%26gt; total;








return 0;


}

Do you know c++ programming? I need help, I can't get this one to work.?
//Try this....


#include "stdafx.h"


#include %26lt;ctime%26gt;


#include %26lt;iostream%26gt;


#include %26lt;cstdlib%26gt;





using namespace std;





int main()


{





srand((unsigned)time(0));


int rand1 = (rand()%90)+1;


int rand2 = (rand()%90)+1;


int int1, int2, total, user_total;


total = rand1 + rand2;





cout%26lt;%26lt;rand1%26lt;%26lt;endl;


cout%26lt;%26lt;rand2%26lt;%26lt;endl;





cout %26lt;%26lt; "What is the first number generated? ";


cin %26gt;%26gt; int1;


cout %26lt;%26lt; "What is the second number generated? ";


cin %26gt;%26gt; int2;


cout %26lt;%26lt; "What is the total? ";


cin %26gt;%26gt; user_total;


while (total != user_total)


{


cout %26lt;%26lt;"Incorrect!"%26lt;%26lt;endl;


cout %26lt;%26lt;"Please, try again."%26lt;%26lt;endl;


cin %26gt;%26gt; user_total;





}


cout%26lt;%26lt;"Correct!"%26lt;%26lt;endl;


cout%26lt;%26lt;"The total is "%26lt;%26lt;total%26lt;%26lt;endl;





return 0;


}
Reply:// Try more like this:





int main()


{


int ran1, ran2, guess1, guess2, guesstotal, totalrand;





srand ((unsigned int) (time (0)));


while (1) //loop forever...


{


ran1= (rand() % 90) +10;


ran2= (rand() % 90) +10;


totalrand=ran1+ran2;





cout %26lt;%26lt; "What is the first number generated? ";


cin %26gt;%26gt; guess1;


cout %26lt;%26lt; "What is the second number generated? ";


cin %26gt;%26gt; guess2;


cout %26lt;%26lt; "What is the total? ";


cin %26gt;%26gt; guesstotal;





if(guesstotal == totalrand)


{


cout %26lt;%26lt; "Very good!" %26lt;%26lt; endl;


break; //break the loop, user succeeded...


}


else


{


cout %26lt;%26lt;"Incorrect!"%26lt;%26lt;endl;


cout %26lt;%26lt;"Please, try again.";


}


} //end while()





return 0;


}


/* Discussion: note we're still not using all the variables to potential---and I wouldn't call this code finished ---but I think you're alot closer to where you want to be....





This code also depends on if you want to keep generating new random numbers or not. If you don't want new random numbers each time, move that portion of code outside the loop. I'm not really clear (sorry) on which it is you want...new random numbers each time, or re-using them 'til user guesses. But hopefully this helps you either way...


*/


No comments:

Post a Comment