C/C++ Thread

Discussion in 'General Software' started by Cromewell, Jan 17, 2011.

  1. Mark4_4

    Mark4_4 Member

    Messages:
    232
    My bad i started using c++ before i moved onto C :p
     
  2. brian

    brian VIP Member

    Messages:
    5,327
    Ok so I have something up with this,

    I have a random number generator that looks like this
    Code:
    int random(int min, int max) {
    	srand(time(NULL));
    	double r = ((((rand()% 100)/99)*(max - 1)) + min);
    	r = floor(r);
    	return r;
    }
    
    however its not generating a random number, the only two numbers I got were 1 and 4. What am I doing wrong.
     
  3. Cromewell

    Cromewell Administrator Staff Member

    Messages:
    15,449
    I think your error is in the part where you calculte the random number bounds. I used double r = rand() * 10000000 % (max - min) + min; and it seems to make a random number.
     
  4. AE7

    AE7 New Member

    Messages:
    51
    Put that call to srand() in your main function - srand() must only be called once and with it being in this random() function, srand() is called every time random() is called.
     
  5. Dystopia

    Dystopia Active Member

    Messages:
    6,605
    I really like writing in C++. The other day I wrote and rewrote a program for 6 hours, just to have a classmate point out I didn't have the line using namespace std; in a function, which fixed the program. I was rather happy.

    I generally read posts rather than post because everyone here is at a much higher level, so when someone posts code, asking for help, well, I just barely understand the code, sometimes not at all :p
     
  6. kobaj

    kobaj VIP Member

    Messages:
    2,946
    I've got a fun question if someone would like to help.

    In my C++ class we were required to make a card game. As such, I had three classes. A card class, which had two enums for Suit and Cardinal. A deck class, with a private vector<card> *mycarddeck = new vector<card>(52) And a main class called game which had its own Deck mydeck(). The other details shouldn't matter at this moment.

    Now, if within the game I needed to access something like mydeck.mycarddeck->at(). I couldn't. Simply because mycarddeck is private. So I created an accessor method, within Deck called at() which returned mycarddeck->at(). IE, allowing me to simply mydeck.at().

    HOWEVER, my professor said this was TERRIBLE. "You wasted a lot of time" blah blah. And when I asked "how else would you recommend I gain access to at() then since it is private?" he basically replied "figure it out".

    So I am here.

    Is there something in C++ that I'm missing? Magical ability to override private declaration without using inheritance? Perhaps not using vector methods outside of Deck in the first place?
     
  7. AE7

    AE7 New Member

    Messages:
    51
    All of my profs recommend accessor methods for purposes like yours. But there are some profs who just don't think logically and practically, been there, done that.

    It would make little sense, but would this prof want you to make a Deck object, then derive objects like PlayerDeck or GameDeck from that?
     
  8. mihir

    mihir VIP Member

    Messages:
    3,980
    there are two ways to access a private inline function.
    either you make a function in the public visibility mode and call the private function and then call the public function or you can also make a friend function which has access to all the private member variables and functions.




    As for Troncoso's Problem


    try this.
    Make a doubly circular linked list and use this as an input for your string and then reverse the linked list.You can either do this by pointers or by just exchanging the data in the nodes or you can just output the reverse whatever you want and that way your string will also be dynamically allocated and unlimited
     
  9. AE7

    AE7 New Member

    Messages:
    51
    Friend functions would do the trick.

    Troncoso could also use the STL's Deque
     
  10. brian

    brian VIP Member

    Messages:
    5,327
    Thanks!

    Well if anyone wants a fun game...
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cmath>
    #include <cstdlib>
    #include <stdio.h>
    #include <vector>
    using namespace std;
    
    int random(int, int);
    int player();
    int computer();
    int pcard[2][100]; //players card deck
    int ccard[2][100]; //computers card deck
    int pnumber = 0; // number of cards (in c++ form ie 0 = 1) player has
    int cnumber = 0; // " " "
    int lastcard[2][1];//the last card played
    int rnumseed = 10; //random number seed itteration count
    void main ()
    {
    	srand(time(NULL));
    	//Give players 5 random cards
    	for (int i = 0; i< 5; ++i) {
    			pcard[0][i] = random(1,10);
    			pcard[1][i] = random(1,4);
    			pnumber++;
    			ccard[0][i] = random(1,10);
    			ccard[1][i] = random(1,4);
    			cnumber++;
    	}
    	--pnumber;
    	--cnumber;
    	//create random card to start
    	lastcard[0][0] = random(1,10);
    	lastcard[1][0] = random(1,4);
    	//plays until all cards are gone
    	do {
    		player();
    		computer();
    		if (cnumber == 0)
    			cout << "\n\nUNO!" << endl;
    	}while (cnumber >= 0 && pnumber >= 0);
    	if (cnumber < 0)
    		cout << "\nThe computer wins :(" << endl;
    	else
    		cout <<"\nYou win!!! :)" << endl;
    
    }
    
    int random(int min, int max) {
    	//seed for random number, have it incress each time, very random mostly.
    	double r = (((((rand()*rnumseed)% 100)/99.)*(max)) + min);
    	//cout << r <<endl;
    	r = floor(r);
    	if (r > max) {
    		r=max;
    	}
    	rnumseed +=17;
    	return r;
    }
    
    int player() {
    	cout << endl;
    	start: //used if card is incorrect
    	int tempdeck[2][100]; //temp deck to remove the card played
    	int cardchoice; // the input of what card to use
    	cout << "Player it is your turn, the current card is\n" << lastcard[0][0] << "," << lastcard[1][0] <<endl; 
    	cout << "Your current cards are" <<endl;
    	for (int i = pnumber; i >= 0; --i) { //loops to display the cards in their hand. 
    		cout << "(" << i+1 << ")"<< pcard[0][i] << "," << pcard [1][i] << " ";
    	}
    	/*cout << "The computers cards are:" <<endl;
    	//for (int i = cnumber; i >= 0; --i) { //loops to display the cards in their hand.   //debug
    	//	cout << "(" << i+1 << ")"<< ccard[0][i] << "," << ccard [1][i] << " ";
    	}*/
    	cout <<"\nEnter the card to use, or 0 to draw" <<endl;
    	cin >> cardchoice;
    	if (cardchoice == 0) { //draws a new card and adds it to the last number, also incresses the number of cards. This is if the user chooses 0
    		pnumber++;
    		pcard[0][pnumber] = random(1,10);
    		pcard[1][pnumber] = random(1,4);
    		cout <<"You recieved " << pcard[0][pnumber] << "," << pcard[1][pnumber] <<endl;
    	}
    	else{
    		--cardchoice; //puts the card the user choose into c++ terms
    		if (pcard[0][cardchoice] == lastcard[0][0] || pcard[1][cardchoice] == lastcard[1][0]) //checks to make sure the card will work
    		{
    			lastcard[0][0] = pcard[0][cardchoice];//updayes the latest card on the desk
    			lastcard[1][0] = pcard[1][cardchoice];
    			for (int i = pnumber; i >=0; --i) { //removes the last card used from the players deck
    				if (i > cardchoice) {
    					tempdeck[0][i-1] = pcard[0][i]; 
    					tempdeck[1][i-1] = pcard[1][i];
    				}
    				else if (i == cardchoice)
    				{
    				}
    				else if (i < cardchoice)
    				{
    					tempdeck[0][i] = pcard[0][i];
    					tempdeck[1][i] = pcard[1][i];
    				}
    			}
    			--pnumber; //decresses number of cards the user has
    			pcard[0][pnumber+1] = 0;
    			pcard[1][pnumber+1] = 0;
    			for (int i = pnumber; i >= 0; --i) { //moves the temp deck to the real deck
    				pcard[0][i] = tempdeck[0][i];
    				pcard[1][i] = tempdeck[1][i];
    			}
    		}
    		else { //if the card does not work
    			cout << "You entered an invalad card try again" << endl;
    			goto start;
    		}
    	}
    	return(0);
    }
    int computer() {
    	bool placed = 0;
    	cout << "Its the computers turn" << endl;
    	int tempdeck[2][100];
    	for (int i = cnumber; i >= 0; --i) {
    		int cardchoice = i;
    		if (ccard[0][i] == lastcard[0][0] || ccard[1][i] == lastcard[1][0]) {
    			lastcard[0][0] = ccard[0][i];
    			lastcard[1][0] = ccard[1][i];
    			for (int x = cnumber; x >=0; --x) { //removes the last card used from the players deck
    				if (x > cardchoice) {
    					tempdeck[0][x-1] = ccard[0][x]; 
    					tempdeck[1][x-1] = ccard[1][x];
    				}
    				else if (x == cardchoice)
    				{
    				}
    				else if (x < cardchoice)
    				{
    					tempdeck[0][x] = ccard[0][x];
    					tempdeck[1][x] = ccard[1][x];
    				}
    			}
    			--cnumber;
    			ccard[0][cnumber+1] = 0;
    			ccard[1][cnumber+1] = 0;
    			for (int x = cnumber; x >= 0; --x) { //moves the temp deck to the real deck
    				ccard[0][x] = tempdeck[0][x];
    				ccard[1][x] = tempdeck[1][x];
    			}
    			cout << "The computer used " << lastcard[0][0] << "," << lastcard[1][0] <<endl;
    			placed =1;
    			break;
    		}
    	}
    	if (placed == 0){
    		++cnumber;
    		ccard[0][cnumber] = random(1,10);
    		ccard[1][cnumber] = random(1,4);
    		cout << "The computer drew a card" << endl;
    	}
    	return(0);
    }
    
    Sorry I don't like to remove my header files
     
  11. mihir

    mihir VIP Member

    Messages:
    3,980
    I always like to use Double Linked List since almost same coding required but way easier further manipulation.

    So would a public function calling the private function
     
  12. mihir

    mihir VIP Member

    Messages:
    3,980
    Declare a structure with a self referential variable and then give some function declaration which you will use.
    Then save it as a header file.
    Then Write a .c file and define all the function you declared in the header file and include the header file in this file.Then make another .c and declare the main function and and call the approprite functions.And you have an unlimited string.and also anyother task you want to do with that code.
    compile your program like this

    gcc declaration.c main.c
     
  13. Cromewell

    Cromewell Administrator Staff Member

    Messages:
    15,449
    You can seed the random all you want, it doesn't really matter if you are seeding with time(null).
    One method would be to have a function that returns the deck vector instead of a card at a position. It's hard to say what exactly they are looking for, I always had to make a rediculous amount of accessor methods in school.
     
  14. Dystopia

    Dystopia Active Member

    Messages:
    6,605
    Turn it into an .exe! I really cbf to move the headers into functions and change the main function for VS.
     
  15. mihir

    mihir VIP Member

    Messages:
    3,980
    The game above it didnt work for me it went into an infinite loop when i selected a card.
     
  16. AE7

    AE7 New Member

    Messages:
    51
    While that is true, calling it once is out an idea of best practice.

    It does nothing to increase the randomness of the numbers generated by the rand() function-no need to dirty up code by calling srand() multiple times.
     
  17. brian

    brian VIP Member

    Messages:
    5,327
    Thats weird, what compiler are you using? I used VB 2010 and it works without a glitch
     
  18. mihir

    mihir VIP Member

    Messages:
    3,980
    I used a Bloodshed Dev C++
     
  19. brian

    brian VIP Member

    Messages:
    5,327
    Ok I have one where I dont know where to start..
    I need some help to start off. I know of cin but I feel this program is asking for something else (ie. it wants me to figure out the code for the enter key)

    Any ideas?
     
  20. mihir

    mihir VIP Member

    Messages:
    3,980
    Every keyboard button has a carriage return code which is different for every programming language.For C/C++ the carriage return code for the "return" key is the escape code '\n'.Or they might also be talking about non language specific codes like for enter the code will be U+21B.
    The first one is easy to make but the second one would need some thinking.You can use the getc function which reads a character and returns and integer and also gives suitable return on end of file.which will be the ascii sequence of the pressed letter.I can write the code for you if you want.But in C
     
    Last edited: Mar 11, 2011

Share This Page