Jump to content
  • Advertisement

Mr Stx

Member
  • Content Count

    8
  • Joined

  • Last visited

Community Reputation

116 Neutral

About Mr Stx

  • Rank
    Newbie
  1. Mr Stx

    c++ combat code

    Hi, im learning c++ currently and am adventuring into creating a very basic game, I posted list week regarding a problem with a while loop on a coin flip program, I currently have an issue with the following code and was hoping someone could offer me advice. (please ignore the int startfight2 and my over-indulgent use of header files). #include <string> #include <sstream> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int userhitpoints = 100; int manhitpoints = 100; int bla; int startfight; int startfight2; int main() { cout << "Begin?\n\n1: Yes\n2:No\n\n"; cin >> startfight; if (startfight == 1) { cout << "\nA man approaches you 'lets fight'\n\n1:Fight\n2:Defend\n\n"; cin >> startfight2; if (startfight2 == 1) { do { srand(time(0)); int damage = rand() % (50 - 10) + 10; int damage2 = rand() % (50 - 10) + 10; int minushp = userhitpoints - damage; cout << "\nYou hit the man for: " << damage << "\nThe man hits you for: " << damage2 << "\n\n" << "mans Hit Points:" << minushp; cout << "\nYour Hit Points: "; int minushp2 = manhitpoints - damage2; cout << minushp2 << "\n\nHit Again?\n"; cin >> startfight2; } while (manhitpoints > 0); } else { cout << "You are dead"; } } else if (startfight2 == 2) { int damage = rand() % (50 - 10) + 10; int defend = damage / 2; cout << "You defend and do 0 damage"; } else cout << "fail"; return 0; } Its a work in progress and isn't complete but my current issue regards the "hitpoints" integers i want them to retain their value after the first loop so the attacker will have to keep attacking untill either userhitpoints or manhitpoints reaches 0.
  2. Mr Stx

    coin flip problem

    #include <ctime> #include <cstdlib> #include <iostream> #include <string> using namespace std; int choice; int randRange (int low, int high) { return rand() % ( high - low ) + low; } int main () { cout << "flip a coin? \n\n1: Yes\n\n2: No\n"; cin >> choice; while (!(choice == 1 || choice == 2)) { cin >> choice; cout << "please enter 1 or 2"; } if (choice == 1) { srand ( time( NULL ) ); randRange( 1 , 10 ); if (randRange( 1,10) >= 5) { cout << "Heads"; } else { cout << "Tails"; } } else if (choice == 2) { cout << "Boooo"; return 0; } }
  3. Mr Stx

    coin flip problem

     while (!( choice == 1 || choice == 2))   still gives me the infinite loop on an invalid input
  4. Mr Stx

    coin flip problem

    ok I have done a good bit of research and have the information provided in this thread great starting points, I have come across a hurdle regarding the "!(choice == 1 || choice ==2)" statement, I understand the concept of it but the compiler doesn't allow me to enter this as the "!" isn't in brackets.
  5. Mr Stx

    coin flip problem

    Thanks guys ill spend a few hours looking into the information you have kindly supplied me and if I'm still stuck ill post again.
  6. Mr Stx

    coin flip problem

    I am having trouble comprehending this. Currently I think I am asking: When the user input is not either 1 or 2 then ask the question again and retrieve another response, if I am not asking this, how would it be asked?   thanks
  7. Mr Stx

    coin flip problem

    #include <ctime> #include <cstdlib> #include <iostream> using namespace std; int choice; int randRange (int low, int high) { return rand() % ( high - low ) + low; } int main () { cout << "flip a coin? \n\n1: Yes\n\n2: No\n"; cin >> choice; while ( choice != 1 || choice != 2) { cout << "please enter 1 or 2"; cin >> choice; } if (choice == 1) { srand ( time( NULL ) ); randRange( 1 , 10 ); if (randRange( 1,10) >= 5) { cout << "Heads"; } else { cout << "Tails"; } } else if (choice == 2) { cout << "Boooo"; return 0; } } Thanks very much for the quick reply, a quick follow up question. I have been struggling with while loops for the last few days, the above script is my current attempt to loop the script if the user input is not one of the acceptable values, from what i have read it would seem that a do-while loop would apply but i should be able to do it with a while statement?   currently the program runs and gets stuck in a infinite loop if i enter an invalid input. i thought the prompt to enter an input would solve this, what am i doing wrong :)?
  8. Mr Stx

    coin flip problem

    Hi all i'm new, I started learning a week or so ago and have joined to the forum so I can get some help and eventually contribute to the community. I am reading a number of books and online guides and have come across a problem I cannot solve when creating a simple coin flip randomizer. #include <ctime> #include <cstdlib> #include <iostream> using namespace std; int choice; int randRange (int low, int high) { return rand() % ( high - low ) + low; } int main () { cout << "flip a coin? \n\n1: Yes\n\n2: No\n"; cin >> choice; if (choice == 1) { srand ( time( NULL ) ); randRange( 1 , 10 ); if (randRange >= 5) { cout << "Heads"; } else { cout << "Tails"; } } else if (choice == 2) { cout << "Boooo"; return 0; } } I am getting a error on the if (randRange >= 5) line, the compiler says that c++ forbids a comparison between pointer and integer, but as far as I am aware I am not using a pointer.   reply's would be gratefully welcomed.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!