3 Replies - 387 Views - Last Post: 16 August 2012 - 06:52 AM Rate Topic: -----

#1 knightz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-August 12

Not sure where i went wrong!

Posted 16 August 2012 - 02:18 AM

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime> 
#include <time.h>


using namespace std;


using std::cout;
using std::cin;
using std::endl; 


// variables in the game. 

int player1turn (int) ;
int player2turn (int) ;

int diceroll (); 
const int winscore= 100; 

int main ()

{
	srand (time (NULL));
	int player1score = 0 ;
	int player2score = 0 ; 
	
	do 
	{
		player1score = player1score + player1turn( player1score );
	
		cout << " Player 1 total score is " << player1score << endl;
	
		if ( player1score >= winscore ) 
		{
			cout << "Player 1 has won !! " ;
			return 0;
		}
			player2score = player2score + player2turn(player2score) ;
			cout << "PLayer 2 total score is " << player2score << endl;
			
			if ( player2score >= winscore)
			{ 
				cout<< "PLayer 2 has won !! ";
				return 0;
			}
	}

	while ( player1score < winscore && player2score < winscore);
}

	


	//The code for the game- allows players to roll the dice and gives them the option to hold or roll again
	//Also holds their score

		int player1turn (int player1score) 
	{
		int thisscore= 0 ;
		int	roll= 0;

		char rOh;

		do
		{
			roll = diceroll () ;
				if (roll == 1 )
				{
					cout << "You have rolled a 1. It's the end of your turn " << endl;
					return 0;
				}
				thisscore = thisscore + roll ;

				cout << "You have rolled a " << roll << " . Your score so far is " << thisscore << endl;

				do

				{
					cout << "Do you want to roll again (r) or hold (h) ? " ;
					cin >> rOh ;
				
				}
				while (rOh == 'r' );
					if ( rOh == 'h')
						return player1score;
		}

		
	
		int player2turn (int player2score) 
	{
		int thisscore= 0 ;
		int	roll= 0;

		char rOh;

		do
		{
			roll = diceroll () ;
				if (roll == 1 )
				{
					cout << "You have rolled a 1. It's the end of your turn " << endl;
					return 0;
				}
				thisscore = thisscore + roll ;

				cout << "You have rolled a " << roll << " . Your score so far is " << thisscore << endl;

				do

				{
					cout << "Do you want to roll again (r) or hold (h) ? " ;
					cin >> rOh ;
				
			
				while (rOh == 'r' );
					if ( rOh == 'h')
						return thisscore;
		}
	
				//Random dice generator
	int diceroll () 

	{
		int roll ;
		
		 roll = ( rand () % 6 ) +1 ;
	}


my errors are :
1>c:\users\victor\documents\visual studio 2010\projects\game1\game1\game1.cpp(27): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\victor\documents\visual studio 2010\projects\game1\game1\game1.cpp(94): error C2062: type 'int' unexpected
1>c:\users\victor\documents\visual studio 2010\projects\game1\game1\game1.cpp(95): error C2143: syntax error : missing ';' before '{'
1>c:\users\victor\documents\visual studio 2010\projects\game1\game1\game1.cpp(126): error C2062: type 'int' unexpected
1>c:\users\victor\documents\visual studio 2010\projects\game1\game1\game1.cpp(128): error C2143: syntax error : missing ';' before '{'
1>c:\users\victor\documents\visual studio 2010\projects\game1\game1\game1.cpp(133): fatal error C1075: end of file found before the left brace '{' at 'c:\users\victor\documents\visual studio 2010\projects\game1\game1\game1.cpp(102)' was matched 


not sure how to fix them

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

Is This A Good Question/Topic? 0
  • +

Replies To: Not sure where i went wrong!

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Not sure where i went wrong!

Posted 16 August 2012 - 02:30 AM

Well, you have done many wrong things. Some of them include posting code without code tags and posting non-indented code. If you would just properly indent your code, you would see that you have missed one bracket in player1turn. Due to this, it is throwing some errors.

This post has been edited by aresh: 16 August 2012 - 02:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 knightz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-August 12

Re: Not sure where i went wrong!

Posted 16 August 2012 - 03:06 AM

thanks ! sorry quite new to this and coding
Was This Post Helpful? 0
  • +
  • -

#4 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Not sure where i went wrong!

Posted 16 August 2012 - 06:52 AM

The indentation thing might seem trivial but it stops you making mistakes, and makes the code much more readable. If you have an IDE like Code::Blocks or Visual Studio then it shouldn't be too hard to keep code properly indented. One thing that might be confusing is all the different styles and which one is the right one. You realise eventually though the style used doesn't matter so much, but you have to use the same style consistently.

After running your code through Visual Studio's indenter this came out:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime> 
#include <time.h>


using namespace std;


using std::cout;
using std::cin;
using std::endl; 


// variables in the game. 

int player1turn (int) ;
int player2turn (int) ;

int diceroll (); 
const int winscore= 100; 

int main ()

{
	srand (time (NULL));
	int player1score = 0 ;
	int player2score = 0 ; 

	do 
	{
		player1score = player1score + player1turn( player1score );

		cout << " Player 1 total score is " << player1score << endl;

		if ( player1score >= winscore ) 
		{
			cout << "Player 1 has won !! " ;
			return 0;
		}
		player2score = player2score + player2turn(player2score) ;
		cout << "PLayer 2 total score is " << player2score << endl;

		if ( player2score >= winscore)
		{ 
			cout<< "PLayer 2 has won !! ";
			return 0;
		}
	}

	while ( player1score < winscore && player2score < winscore);
}




//The code for the game- allows players to roll the dice and gives them the option to hold or roll again
//Also holds their score

int player1turn (int player1score) 
{
	int thisscore= 0 ;
	int	roll= 0;

	char rOh;

	do
	{
		roll = diceroll () ;
		if (roll == 1 )
		{
			cout << "You have rolled a 1. It's the end of your turn " << endl;
			return 0;
		}
		thisscore = thisscore + roll ;

		cout << "You have rolled a " << roll << " . Your score so far is " << thisscore << endl;

		do

		{
			cout << "Do you want to roll again (r) or hold (h) ? " ;
			cin >> rOh ;

		}
		while (rOh == 'r' );
		if ( rOh == 'h')
			return player1score;
	}



	int player2turn (int player2score) 
	{
		int thisscore= 0 ;
		int	roll= 0;

		char rOh;

		do
		{
			roll = diceroll () ;
			if (roll == 1 )
			{
				cout << "You have rolled a 1. It's the end of your turn " << endl;
				return 0;
			}
			thisscore = thisscore + roll ;

			cout << "You have rolled a " << roll << " . Your score so far is " << thisscore << endl;

			do

			{
				cout << "Do you want to roll again (r) or hold (h) ? " ;
				cin >> rOh ;


				while (rOh == 'r' );
				if ( rOh == 'h')
					return thisscore;
			}

			//Random dice generator
			int diceroll () 

			{
				int roll ;

				roll = ( rand () % 6 ) +1 ;
			}

It shows pretty clearly what's up. There are lots of curly braces not matching up, so functions are being defined in other functions and the file ends with some braces not matched up.

Another little recurring problem is in do-while loops the while bit is in the wrong place. The while statement goes after the closing curly brace of the do loop. Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1