Card and Deck

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1247 Views - Last Post: 10 February 2010 - 04:34 AM Rate Topic: -----

#1 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Card and Deck

Posted 09 February 2010 - 09:19 AM

What is wrong with my code?

i am having these errors:

error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
fatal error LNK1120: 1 unresolved externals

#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>

using namespace std;

class Card{
public:
	Card(string s[4], string r[13]);

	
	Card(){	//constructor
		s[0]="Spade";
		s[1]="Heart";
		s[2]="club";
		s[3]="Diamond";
		r[0]="Ace";
		r[1]="Two";
		r[2]="Three";
		r[3]="Four";
		r[4]="Five";
		r[5]="Six";
		r[6]="Seven";
		r[7]="Eight";
		r[8]="Nine";
		r[9]="Ten";
		r[10]="Joker";
		r[11]="Queen";
		r[12]="King";

	}


	string suit;
	string rank;
	int i;
	
	srand ( time(NULL) );
	
	getSuit(){
	do{
		i=rand()%10-6;


	}while (i >=4 || i<0);
		suit=s[i];	
	
	}

	getRank(){
	do{
		i=rand()%10+3;




	}while (i >=13 || i<0);
		Rank=r[i];	
	
	}


	getSuit();
	getRank();

	void print(){
	cout>>getSuit()>>getRank>>endl;
	}
	

	void print(); //Example :"SpadeA",Diadmond 10" etc.
};




Please advise Thank You.

Is This A Good Question/Topic? 0
  • +

Replies To: Card and Deck

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Card and Deck

Posted 09 February 2010 - 09:22 AM

You forgot to add the entry point of your program. More precisely, 'int main()' { } is missing.
Was This Post Helpful? 1
  • +
  • -

#3 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Card and Deck

Posted 09 February 2010 - 09:42 AM

After adding main i still having the errors :<

int main(){
	
	Card c1();

	c1.print();

	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Card and Deck

Posted 09 February 2010 - 09:50 AM

I recreated the project and resaved it and now its not showing the error..

However my codes has 42 errors.

Where the first one is error C2061: syntax error : identifier 'time'

I dun understand why is it wrong?
Was This Post Helpful? 0
  • +
  • -

#5 Keerigan  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 04-February 10

Re: Card and Deck

Posted 09 February 2010 - 09:51 AM

Don't know if this is an issue or not
void print(){
        cout>>getSuit()>>getRank>>endl;
        }



but should your getRank have () with it?

This post has been edited by Keerigan: 09 February 2010 - 09:52 AM

Was This Post Helpful? 1
  • +
  • -

#6 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 538
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Card and Deck

Posted 09 February 2010 - 09:57 AM

tnecniv, here is your program without errors. I would strongly recommend you look at what I am returning to you and what you posted. What you posted was a mess!!!

Try to understand what I have changed and why. If you have any questions please feel free to ask.

#include <iostream> 
#include <string> 
#include <time.h> 
#include <stdlib.h> 
 
using namespace std; 
 
class Card{ 
public: 
        string s[4];
		string r[13]; 
 
         
        Card(){ //constructor 
                s[0]="Spade"; 
                s[1]="Heart"; 
                s[2]="club"; 
                s[3]="Diamond"; 
                r[0]="Ace"; 
                r[1]="Two"; 
                r[2]="Three"; 
                r[3]="Four"; 
                r[4]="Five"; 
                r[5]="Six"; 
                r[6]="Seven"; 
                r[7]="Eight"; 
                r[8]="Nine"; 
                r[9]="Ten"; 
                r[10]="Joker"; 
                r[11]="Queen"; 
                r[12]="King"; 
 
				srand(time(NULL)); 
		} 
 
 
        string suit; 
        string rank; 
        int i; 
                
        string getSuit() { 
	        do { 
	           i=rand()%10-6; 
	        } while (i >=4 || i<0); 
	        return s[i];       
        } 
 
        string getRank() { 
	        do{ 
	                i=rand()%10+3; 
	        } while (i >=13 || i<0); 
	        return r[i];                
        } 
 
        void print(){ 
        	cout << getSuit() << getRank() << endl; 
        } 
       
};

int main(){ 
         
        Card c1; 
 
        c1.print(); 
 
        return 0; 
}



I have not run this code, but at least it compiles now!!!
Was This Post Helpful? 1
  • +
  • -

#7 stephenl  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-February 10

Re: Card and Deck

Posted 09 February 2010 - 10:04 AM

nvm

This post has been edited by stephenl: 09 February 2010 - 10:05 AM

Was This Post Helpful? 0
  • +
  • -

#8 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Card and Deck

Posted 09 February 2010 - 10:16 AM

Thanks for the advise, i have tried to compile again but having error on the srand line..

Wats wrong ?

#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>


using namespace std;

class Card{
public:
	string s[4]; 
	string r[13];

	
	Card(){	//constructor
		s[0]="Spade";
		s[1]="Heart";
		s[2]="club";
		s[3]="Diamond";

		r[0]="Ace";
		r[1]="Two";
		r[2]="Three";
		r[3]="Four";
		r[4]="Five";
		r[5]="Six";
		r[6]="Seven";
		r[7]="Eight";
		r[8]="Nine";
		r[9]="Ten";
		r[10]="Joker";
		r[11]="Queen";
		r[12]="King";
		
		srand(time(NULL));//THIS part having problem
	}


	string suit;
	string rank;
	int i;
	
	
	
	string getSuit(){
	
		i=(rand()%10)-6;
	
		suit=s[i];	
	return s[i];
	}

	string getRank(){
	 i=(rand()%10)+3;
	 Rank=r[i];	
	 return r[i];
	}


	
	void print(){
	cout>>getSuit()>>getRank()>>endl;
	}
	

	 //Example :"SpadeA",Diadmond 10" etc.
};


int main(){
	
	Card c1();

	c1.print();

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 538
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Card and Deck

Posted 09 February 2010 - 10:19 AM

It compiles for me, but I would try

  srand((unsigned int)time(NULL));



time returns a type of time_t whereas srand expects an unsigned int. That should do the trick for you.
Was This Post Helpful? 1
  • +
  • -

#10 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Card and Deck

Posted 09 February 2010 - 10:22 AM

View PostMartyn.Rae, on 09 February 2010 - 09:19 AM, said:

It compiles for me, but I would try

  srand((unsigned int)time(NULL));



time returns a type of time_t whereas srand expects an unsigned int. That should do the trick for you.


Thanks. i will try. may i noe why srand is inside the constructor??
not outside
Was This Post Helpful? 0
  • +
  • -

#11 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 538
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Card and Deck

Posted 09 February 2010 - 10:24 AM

Outside the constructor, it would be in error as it would not conform to C++, and it would never be called. You want srand to be called once, and inside the constructor is perfect as it is constructed only once.
Was This Post Helpful? 1
  • +
  • -

#12 tnecniv  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 03-October 09

Re: Card and Deck

Posted 09 February 2010 - 10:35 AM

View PostMartyn.Rae, on 09 February 2010 - 09:24 AM, said:

Outside the constructor, it would be in error as it would not conform to C++, and it would never be called. You want srand to be called once, and inside the constructor is perfect as it is constructed only once.


Thank You. if i nvr interpret wrongly, that srand will not run in class if it is not in the constructor?

and after trying my program is running properly!
Was This Post Helpful? 0
  • +
  • -

#13 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 538
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Card and Deck

Posted 09 February 2010 - 10:38 AM

View Posttnecniv, on 09 February 2010 - 04:35 PM, said:

View PostMartyn.Rae, on 09 February 2010 - 09:24 AM, said:

Outside the constructor, it would be in error as it would not conform to C++, and it would never be called. You want srand to be called once, and inside the constructor is perfect as it is constructed only once.


Thank You. if i nvr interpret wrongly, that srand will not run in class if it is not in the constructor?

and after trying my program is running properly!


You have understood correctly. It would never be called unless it was in the constructor or one of the member functions.
Was This Post Helpful? 1
  • +
  • -

#14 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Card and Deck

Posted 09 February 2010 - 12:33 PM

It's OK to call srand() in the constructor, but if you initialize the class twice, then srand() is called twice, if you initialize the class three times, srand() is called three times, and so on ... I'd choose to put it right after calling the main() function:
int main()
{
   srand(unsigned(time(0)));
   // code goes here
   return 0;
}


Notice when srand() is called, it initializes the random number generator FOR ALL functions, from every CPP/H file, etc ... So, if you are dealing with functions using random numbers, do not call srand() in every function. A simple call in main() would do the trick.

This post has been edited by sarmanu: 09 February 2010 - 12:37 PM

Was This Post Helpful? 1
  • +
  • -

#15 Guest_hi*


Reputation:

Re: Card and Deck

Posted 09 February 2010 - 02:21 PM

in addition to Keerigan, don't the cout arrows point the other way?
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2