C/C++ Thread

Mark4_4

Member
strrev isn't a standard function I think that is a Visual C++ bonus function, there is a reverse function but it's a C++ template and works on vectors.

Brian: a 2 dimensional array may work for you if you don't want to use objects.

My bad i started using c++ before i moved onto C :p
 

brian

VIP Member
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.
 

Cromewell

Administrator
Staff member
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.
 

AE7

New Member
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.

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.
 

Dystopia

Active Member
Haha, besides the 3 of us (rather the 2 of you) no one on the forums has expressed enough interest in C to actually post about it. Makes me sad. I'd love to compare and learn from others who are learning the language. As for the problem, while I understand it for the most part, it's still a bit over my head, so I have to pass.

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
 

kobaj

VIP Member
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?
 

AE7

New Member
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?

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?
 

mihir

VIP Member
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?

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
 

AE7

New Member
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

Friend functions would do the trick.

Troncoso could also use the STL's Deque
 

brian

VIP Member
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.

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
 

mihir

VIP Member
Friend functions would do the trick.

Troncoso could also use the STL's Deque

I always like to use Double Linked List since almost same coding required but way easier further manipulation.

Friend functions would do the trick.

Troncoso could also use the STL's Deque
So would a public function calling the private function
 

mihir

VIP Member
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
 

Cromewell

Administrator
Staff member
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.
You can seed the random all you want, it doesn't really matter if you are seeding with time(null).
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?
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.
 

Dystopia

Active Member
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

Turn it into an .exe! I really cbf to move the headers into functions and change the main function for VS.
 

mihir

VIP Member
The game above it didnt work for me it went into an infinite loop when i selected a card.
 

AE7

New Member
You can seed the random all you want, it doesn't really matter if you are seeding with time(null).

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.
 

brian

VIP Member
Ok I have one where I dont know where to start..
Write a program that reads a key pressed on the keyboard and displays its code on the screen. Use the program to determine the code for the Enter key. Then write a function named ReadOneChar() that reads a character and ignores any succeeding characters until the Enter key is pressed. The entered character should be returned by ReadOneChar().

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?
 

mihir

VIP Member
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:
Top