Jump to content
  • Advertisement
Sign in to follow this  
JimmyThompsonUK

Newb at C++ - need help

This topic is 3799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i'm new to these boards. I want to learn how to program using C++, and steer it towards game development. I started only a few days ago and read up on a few tutorials. I want to take it slow and know things i've been taught well before I learn new things. I decided to make a Rock, Scissors, Paper game in C++ that runs in a console window. It has 0 errors in the compiler, but when you input anything into the "Please pick your move (R/S/P)" it crashes, any help and C++ programming advice (Such as things to avoid doing etc.) would help out alot:
#include <iostream>
#include <cstdlib>

using namespace std;

int wld;
int UserScore;
int AIScore;
int roll = rand(3);
char string[1];
char choice[256];

void recurse()
{
  recurse();
}

int userturn()
{
     cout<<"\n";
     cout<<"Please pick your move (R/S/P): ";
     cin.getline(string,sizeof(string));

     if(strlen(string) > 1)
     {
          cout<<"\n";
          cout<<"Invalid entry, please select either (R)ock, (S)cissors or (P)aper"<<"\n";
          recurse();
     }

     if(string != "R" && string != "S" && string != "P" && string != "r" && string != "s" && string != "p")
     {
          cout<<"\n";
          cout<<"Invalid entry, please select either (R)ock, (S)cissors or (P)aper"<<"\n";
          recurse();
     }

     if(string == "R"||string == "r") cout<<"You have selected Rock!"<<"\n";
     if(string == "S"||string == "p") cout<<"You have selected Scissors!\n";
     if(string == "P"||string == "p") cout<<"You have selected Paper!\n";
     cin.get();
}

int main(int argc, char *argv[])
{
  cout<<"Welcome to Jimmy's Rock Paper Scissors game!\n";
  cout<<"When prompted, select (R)ock, (S)cissors or (P)aper\n";
  cout<<"Rock beats scissors, Scissors beat Paper and Paper beats Rock.\n";
  userturn();

  if(roll == 0) cout<<"Your opponent has selected Rock!\n";
  if(roll == 1) cout<<"Your opponent has selected Scissors!\n";
  if(roll == 2) cout<<"Your opponent has selected Paper!\n";

  if(string == "R"||string == "r")
  {
      if(roll == 0) wld = 2;
      if(roll == 1) wld = 1;
      if(roll == 2) wld = 3;
  }
  if(string == "S"||string == "s")
  {
      if(roll == 0) wld = 3;
      if(roll == 1) wld = 2;
      if(roll == 2) wld = 1;
  }
  if(string == "P"||string == "p")
  {
      if(roll == 0) wld = 1;
      if(roll == 1) wld = 3;
      if(roll == 2) wld = 2;
  }

  if(wld == 1)
  {
     cout<<"You Win!!!"<<"\n";
     UserScore = UserScore + 1;
  }

  if(wld == 2) cout<<"It's a draw, nobody wins."<<"\n";

  if(wld == 3)
  {
     cout<<"You lose."<<"\n";
     AIScore = AIScore + 1;
  }

  cout<<"The Score is now "<<UserScore<<" - "<<AIScore;
  cout<<"\n";
  cout<<"\n";
  cout<<"Play again (Y/N)?: ";
  cin.getline(choice,sizeof(choice));

  if(choice == "Y"||choice == "y")
  {
      recurse();
  }
  if(choice == "N"||choice == "n")
  {
      cout<<"Press any key to close...";
      cin.get();
      return 0;
  }
  cin.get();
}
Thanks alot. :)

Share this post


Link to post
Share on other sites
Advertisement

void recurse()
{
recurse();
}





Can you explain what you think this code will do, when executed?

Also, you are using strlen on an array of length 1. Do you understand how strlen works?

I would advise just using a single char here, and to use std::string instead of char arrays.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
*** Source Snippet Removed ***


Can you explain what you think this code will do, when executed?


Re-call the function. But without it I get an implicit declaration of Recurse(); error. That's why it's there...I do see why it would crash the console though.

Share this post


Link to post
Share on other sites
It crashes because you call recurse over and over again until the stack fills up and overflows. Why are you recursing on a function that does nothing?

Share this post


Link to post
Share on other sites
Quote:
Original post by JimmyThompsonUK
Quote:
Original post by rip-off
*** Source Snippet Removed ***


Can you explain what you think this code will do, when executed?


Re-call the function. But without it I get an implicit declaration of Recurse(); error. That's why it's there...I do see why it would crash the console though.


I edited my post to ask more questions.

As for that particular snippet, consider how it works. When recurse is called, what will happen? That function calls recurse again. That call, in turn, will call recurse again. This forms a cycle that never ends - in theory. In practise - the stack, where certain bookkeeping information is kept (such as where to return) will become full, and your program will crash.

Share this post


Link to post
Share on other sites
Quote:
Re-call the function. But without it I get an implicit declaration of Recurse(); error.
You realize, you can have an empty definition, right? You don’t need to put in any code. If you want to emphasize the emptiness of the definition, you can always put in a return;

Share this post


Link to post
Share on other sites
I think you want to recurse, not call a function named recurse. Try calling


main(argc, argv);



in the place of


recurse()



also, those comparisons are scary, you are comparing two memory addresses that will never be the same. You want strcmp with c-style strings, or use std::string like was suggested. Also, you should try not to copy code, notice how your if statements are all the same in the bodies, combine the conditionals with ||.

Share this post


Link to post
Share on other sites
I'm not saying I would do it, I am merely suggesting an explanation for why he had a function named recurse, that will immediately lock up the system. Maybe that wasn't clear. I would avoid recursion in something like this, like the plague.

Share this post


Link to post
Share on other sites
You can use a loop in main() and just keep looping until the user wants to quit. This can take the recurse() call out of main().

Inside of userturn(), if there is an invalid entry then just try it again (Another possible loop). No need to use recurse() here either.

Look into C++ loops and see if you can find a way to remove the calls to recurse(). Feel free to ask if you have any problems.

Also...

char string[1];. One character strings should be a single char. I see that you are comparing this char array like this: string != "R". This might lead you into trouble when you try to compare strings greater then one character as you cannot compare strings this way.

This is one of the many benefits of using std::string over the C style char arrays which allow this type of comparison.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!