get an if statement to work

Started by
9 comments, last by Xizorp 18 years, 2 months ago
hello all! I am trying to code a program to practice using arrays and I started with this little program that I cant seem to get to working right. I'm trying to have two charters a jedi and sith fight. The player being the jedi and the computer the sith. The player chooses a number from 1, 2, or 3 and that is there move. Then the computer uses the same process but it get a random number instead for the siths action and the sth performs that action. Now my problem is that when its time for the sith to perform its action the program ends? I cant figure out why any help would be hot?

#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;

int attack (int atk);
int defend (int def);
stall ();


int main ()
{
 // the array is jedi[0] = attack, jedi[1] = defend, jedi[2] = vitality, jedi[3] = wound
 int jedi[4]= {3,4,10,8};
 //for the sith see jedi
 int sith[4]= {4,3,8,10};
 int move = 0;
 int dicenumber = 0;
 //trying two different ways for bool type one with "int" and the other with "bool"
 int jedturn = 0;
 bool sithturn = 1;

 
 srand(time(NULL));
 //start of players turn.
 cout << "attack with 1? defend with 2. stall with 3."<<endl;
 cin >> move;

 switch (move)
 {
 case 1:
 attack (jedi[0]) ;
 jedturn = 1;
 break;

 case 2:
 defend (jedi[1]);
 jedturn =1;
 break;

 case 3:
 stall ();
 jedturn =1;
 break;

 default:
 cout << "please enter 1, 2 or 3."<<endl;
 break;
 }
 //end of players turn

 //start of pc turn
 //chosse random number for acticon for enemy
  //then enemy perform the action
 if (jedturn == 1)
 {
  int enemyaction = rand() % 3+1;
  
  

  switch (enemyaction)
  {
  case (1):
  cout << "blah "<< attack(sith[0]);
  break;

  case (2):
  defend (sith[1]);
  break;

  case (3):
  sithturn = 1;
  break;

  default:
  break;
  }  
 }
 else
 cout << "sith dose nothing"<<endl;
 //end of player turn

 return 0;
}

int attack (int atk)
{
 int totalattack = 0;

 totalattack = atk + rand() % 20;
 cout << totalattack<< endl;
 return totalattack;
}

int defend (int def)
{
 int totaldefend;
 totaldefend = def + 5;
 return totaldefend;
}

 stall ()
{
 cout<< "end turn" << endl;
 return 0;
}

Advertisement
Looks like a great start! I'm glad you didn't decide to start making Everquest 3 first. Anyway, if you look at your program, you'll see it's doing just what you asked it to do: Player gives input, player moves, Sith move, return 0; If you haven't been introduced to the while loop yet, now is an excellent time. Consider:

bool GameIsFinished = false;while (!GameIsFinished) // this could also be written "while (GameIsFinished == false)"{  GetPlayerInput();  DoPlayerMove(); // have player take a move  if (A_Player_Is_Dead() == true)  // if someone is dead now, the game is over    GameIsFinished = true;  DoSithMove();  // let computer move  if (A_Player_Is_Dead() == true)  // see above    GameIsFinished = true;}cout << "Thanks for playing!";return 0;


In this way, the game will keep going until a player is dead. Obviously the functions I used were pseudocode, by the way. :)
It only takes one mistake to wake up dead the next morning.
good start so far :) one other thing i noticed

stall ()
{
cout<< "end turn" << endl;
return 0;
}

that function returns a value but isnt set up to do so (return 0), try

int stall()
{
cout<< "end turn" << endl;
return 0;
}
hum a funny thing happened? I have been trying to get the line cout << "blah "<< attack(sith[0]); to show up in my program, and i couldnt until i put cout<< "sith defends" << endl; in case 2 and cout<< "sith stall" << endl; in case 3 why didn't the cout << "blah "<< attack(sith[0]); show up before?
Most likely this is because the output buffer wasn't being flushed. When you type a line like:

cout << "My text";

The computer puts "My text" into a holding space (a "buffer") but doesn't print everything in the buffer to the screen until later, to be more efficient - if you were to put:

cout << "My text";
cout << "My text2";
cout << "My text3" << std::endl;

It could print all of it at once instead of having to get it, print it, get it, print it, get it, and print it because it's collected in the buffer. When you use std::endl; or std::flush; you tell the computer to go ahead and print the text in the buffer no matter what, causing it to be displayed.

'Least, that's my understanding of it.

Edit: I wouldn't know why putting std::endl in other cases would have that effect.
It only takes one mistake to wake up dead the next morning.
thanks for all the help! much appreciated.
what's up all! today I stared reading about pointers and wanted to implement them in my program. I have been doing well so far but I am coming across one problem. in the program when it comes time for the sith to attack it displays the sith actions as well as the players actions. Giving the player an extra turn and giving the player the same action as the computer. I was wondering if this has something to do with the pointers I am using if its the function I'm using or something else?
#include <iostream>#include <time.h>#include <math.h>using namespace std;int playersmove (int *arr, bool *whoturn);int sithturn (int *sitharr,bool *siturn);int attack (int *atk);int defend (int *def);stall ();int main (){	int i = 0;	//array types	int jedi[4]= {3,4,10,8};	int sith[4]= {4,3,8,10};	//boolen types	bool jediturn = true;	bool sithsturn = false;	//pointer types	bool *turn;	turn = &jediturn;	bool *pcturn;	pcturn = &sithsturn;		//function	if (jediturn == true)	{	playersmove(jedi, turn);	}	if (jediturn == false)	{		jediturn = false;		sithsturn = true;	sithturn (sith, pcturn);	}		return 0;}int playersmove (int *arr ,bool *whoturn){	int move = 0;	cout << "attack with 1? defend with 2. stall with 3."<<endl;	cin >> move;	switch (move)	{	case 1:	attack (arr);	*whoturn = false;	break;	case 2:	defend (arr+1);	*whoturn = false;	break;	case 3:	stall ();	*whoturn = false;	break;	default:	cout << "please enter 1, 2 or 3."<<endl;	break;	}	return *whoturn;}int sithturn(int *sitharr, bool *siturn){	int enmeyaction = rand() % 3;						switch (enmeyaction)		{		case (1):		cout << "the sith attacks "<<  endl;		attack (sitharr);		*siturn = false ;		break;		case (2):		cout<< "sith defends" << endl;		defend (sitharr);		*siturn = false;		break;		default:		break;		}			return *siturn;}int attack (int *atk){	int totalattack = 0;	int tack = *atk;	totalattack = tack + rand () % 20 +1;	cout << "you chosse to attack " <<totalattack<< endl;	return totalattack;}int defend (int *def){	int totaldefend;	totaldefend = *def + 5;	cout << "you rasie your guard " << totaldefend << std::endl;	return totaldefend;}stall (){	cout<< "end turn" << endl;	return 0;}
I don't have time to answer your question, but I just have to warn your: its great to learn how pointers work, but you're going to want to use for an 'appropriate' use. You don't need to use pointers there, so it would probably be best to avoid them there. I don't really know of anything your level that requires the use of pointers; they don't become exceedingly useful(most of the time) until you get to classes.
I agree, you shouldn't "try to use pointers". They're not a must-have or a "good thing". They're just something you unfortunately have to deal with occasionally.
So you should understand what they are, and what they can do that you can't do otherwise, and then wait until you run into a situation where you actually need them. But as long as you don't need them, stay away. [wink]
Quote:Original post by Xizorp
what's up all! today I stared reading about pointers and wanted to implement them in my program. I have been doing well so far but I am coming across one problem.


Zen: Yes, the problem you are coming across is that you want to implement something just because you read about it. :)

This topic is closed to new replies.

Advertisement