Jump to content

Function help

Mr. Brovahkiin

Happy to help. It was quite nice to dust off my C++ again. :)

Overall, pointers are not really that complicated once you've gotten your

head around the basics, although you can write some hilariously, sadistically

incomprehensible code using pointers if you feel so inclined. ;)

For more information on this, I've had a look around on Youtube and the following

tutorial gives a pretty neat and compact overview of pointers:

And in this one he goes specifically into passing arrays to functions, although

of course he uses the square brackets instead of the asterisk. But you could do

the same thing he does by using an asterisk instead and doing the corresponding

pointer arithmetic.

Here's some fundamentals on pointers, arrays and memory layout. This is quite useful

(or rather: essential) for understanding some of the basic mechanics of programming

in C++ and similar languages.

And finally, a rather extensive look at arrays, starting with the basics:

Here he also takes a look at some member functions for the array container

class according to the new C++11 standard (the "at" operator, for example).

There's a ton of other vids and channels out there (Brian Will also has an excellent youtube

channel).

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Ok so instead of making a new thread I figured I just post in this one. I'm getting an error every time I try to compile that says "[Linker Error]undefined reference to checkMatch(int*,int*) id return exit one status. Here is my code 

#include <iostream>#include <ctime>#include <cstdlib>using namespace std;void displayInstructions();void GenerateCards(int *deck);int getGuess ( );bool checkMatch (int *player , int* generatedCards);int main (){   int i, answer, playercard;	const int CARDS = 24; 	int generatedCards [5]; 	int player [5]; 	string deck[CARDS] = {	"red circle","red square","red diamond","red triangle","blue circle","blue square","blue diamond",	 "blue triangle","yellow circle","yellow square","yellow diamond","yellow triangle","orange circle",	 "orange square" ,"orange diamond","orange triangle","purple circle","purple square","purple diamond",     "purple triangle","green circle","green square","green diamond","green triangle"};		displayInstructions(); 	GenerateCards (generatedCards); 	     for (i=0; i < 5; i++)         *(player + i) = getGuess();      checkMatch (player, generatedCards);	system("pause"); 	return 0;}void displayInstructions(){	cout <<	"The computer will generate a deck of cards.\n"		 << "There are 4 shapes: cricle, square, diamond, and trianlge.\n"		 << "There are 6 colors: red, blue, yellow, orange, purple, and green.\n"		 << "Pick a shape then pick a color.\n"		 << "Do this 5 times and see if your predictions match what the computer picked.\n";		 	 }void GenerateCards(int *generatedCards){    int i;    int selectedInt;    int alreadyPresent = 0;    int successfulSelections = 0;    srand ( time(NULL) );     while (successfulSelections < 5) {        selectedInt = (rand() % 24) + 1;        if (successfulSelections==0) {            *(generatedCards)=selectedInt;        }         for (int l=0;l<successfulSelections;l++) {            if (*(generatedCards+l)==selectedInt) {                alreadyPresent=1;                break;            } else {                alreadyPresent=0;                *(generatedCards+successfulSelections)=selectedInt;            }        }        if (alreadyPresent==0) {            successfulSelections++;        }    }}int getGuess ( ){		int answer=0;    const int CARDS = 24;	string deck[CARDS] = {	"red cricle","red square","red diamond","red triangle","blue circle",	 "blue square","blue diamond","blue triangle","yellow circle","yellow square",     "yellow diamond","yellow triangle","orange circle","orange square",     "orange diamond","orange triangle","purple circle","purple square",	 "purple diamond","purple triangle","green circle","green square","green diamond",     "green triangle"};	int i;	string color, shape, final;	cout << "Please choose a card"<<endl;	cin >> color >> shape;		final = color + " " + shape;			  	for (i=0; i < 24; i++)			  		{	              if (final==*(deck+i))        	{answer=i;             		break;             }		}	return answer;		}
Link to comment
Share on other sites

Link to post
Share on other sites

Good news is if I copy-paste your code I get the same error. ;)

And I would expect it to. Do you have checkMatch actually defined anywhere (not just

the function's prototype, but the actual function itself)?

EDIT: As a side not, I highly recommend not doing this:

 

for (i=0; i < 5; i++)         *(player + i) = getGuess(); 
but instead this:

 

for (i=0; i < 5; i++) {         *(player + i) = getGuess(); }
Even if it's just one line of code in your loop, the second approach makes your code

a lot less error prone. I don't have decades' worth experience in programming but I

have done some commercial work (though in PHP, not C++), and stuff like that can be

a hilariously annoying source for errors.

Same thing with if statements and every other opportunity where you can do this.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Good news is if I copy-paste your code I get the same error. ;)

And I would expect it to. Do you have checkMatch actually defined anywhere (not just

the function's prototype, but the actual function itself)?

EDIT: As a side not, I highly recommend not doing this:

 

for (i=0; i < 5; i++)         *(player + i) = getGuess(); 
but instead this:

 

for (i=0; i < 5; i++) {         *(player + i) = getGuess(); }
Even if it's just one line of code in your loop, the second approach makes your code

a lot less error prone. I don't have decades' worth experience in programming but I

have done some commercial work (though in PHP, not C++), and stuff like that can be

a hilariously annoying source for errors.

Same thing with if statements and every other opportunity where you can do this.

 

Like the actual checkMatch as like a variable? And I usually do include brackets even on one line loops cause it's easier for me to visualize what belongs where. Thanks for the heads up though. I think in all the writing and re writing of the code I forgot to put it there :P

Link to comment
Share on other sites

Link to post
Share on other sites

Good to know, happens to the best ;)

As for the checkMatch function (I'm assuming your intention is for this to be the

function which checks if the player's selections match with the computer's), I

mean something like this:

 

#include <iostream>#include <ctime>#include <cstdlib>using namespace std;void displayInstructions();void GenerateCards(int *deck);int getGuess ( );bool checkMatch (int *player , int* generatedCards);int main (){    /* Other Code */    checkMatch (player, generatedCards);    /* Other Code */}void displayInstructions(){    /* Some Code */}void GenerateCards(int *generatedCards){    /* Some Code */}int getGuess ( ){       /* Some Code */	}bool checkMatch (int *player , int* generatedCards){    /* YOUR CHECKING CODE HERE */}

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Yes that is what I plan to do.

 

Edit: Just so you have some reference. Player is the array that I store the user input in and generatedCards is the code that you so lovingly (lol) wrote for me to get 5 random numbers

Link to comment
Share on other sites

Link to post
Share on other sites

Then that error will disappear, I've just tried it quickly. So, keep on coding :)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Wait, what's changed other then the brackets? Or are my reading comprehension skills failing?

Edit: Nevermind lol. I figured it out. +1 to dumb for me 

Link to comment
Share on other sites

Link to post
Share on other sites

Quick question.If I want it to check if the card matches each time the person inputs something, the way I'm doing it right now won't do that right?

Link to comment
Share on other sites

Link to post
Share on other sites

You mean as in:

  • player enters 1 guess
  • said guess is checked
  • result is outputted
  • player enters next guess, etc.
?

Well as far as I can tell you are actually doing that with the for loop in getGuess(),

you're just not doing anything with the result yet.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

You mean as in:

  • player enters 1 guess
  • said guess is checked
  • result is outputted
  • player enters next guess, etc.
?

Well as far as I can tell you are actually doing that with the for loop in getGuess(),

you're just not doing anything with the result yet.

 

Ok. I'll write up the checkMatch function and be back

Link to comment
Share on other sites

Link to post
Share on other sites

On another note, I suggest making these alterations, since I'm pretty sure that's actually

part of this exerice's point:

Also: note that "final" is actually a keyword for C++11. It does not seem to cause any

problems in this context, but you might want to replace it with something else.

 

// start of codeint getGuess(string*,int)// more codeint main(){    /* code */    const int CARDS = 24;    int generatedCards [5];    int player [5];    string deck[CARDS] = {        "red circle","red square","red diamond","red triangle","blue circle","blue square",        "blue diamond", "blue triangle","yellow circle","yellow square","yellow diamond",        "yellow triangle","orange circle", "orange square" ,"orange diamond","orange triangle",        "purple circle","purple square","purple diamond","purple triangle","green circle",        "green square","green diamond","green triangle"    };    getGuess(deck,CARDS)    /* code */}int getGuess(string* deck, int CARDS){    /* you don't need to redeclare deck and CARDS again here, just delete those lines.*/    int answer = 0;    string color,shape,combined_choice;    cout << "Please chose a card" << "\n";    cin >> color >> shape;    combined_choice = color + " " + shape;    for (int i=0;i<CARDS;i++)    {        if (combined_choice==*(deck+i))        {            answer=i;            break;        }    }    return answer;}
This spares you to have to redeclare the deck array (which is very inefficient and inelegant).

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites


Ok so I changed it and declared it. I have this bool function now but it's not doing anything. Don't think I used it right


bool checkMatch (int *player, int *generatedcards)

{

if (*player==*generatedcards)

{ cout<< "Chosen card matches"<< endl;

return true;

else

cout<<"Chosen card does not match"<<endl;

return false;}

}

Link to comment
Share on other sites

Link to post
Share on other sites

Ok so I changed it and declared it. I have this bool function now but it's not doing anything. Don't think I used it right

That's because it's not going through the array(s). It's only testing the first value

of each array against each other (remember: you need to check every player choice against

all of the computer's selections, not just the first one, so you need to cycle through the

computer's array once for each player selection). You can actually recycle my is_in_array

function from above for this:

 

bool is_in_array(int to_test,int elements,int* array) {    // to_test: element to test array for    // elements: the number of elements in an array that are to be    // tested. Does not need to be as high as the array element    // count.    for (int i=0;i<elements;i++) {        if (*(array+i)==to_test) {            cout << "card matches" << "\n";            return true;        }    }    cout << "card does not match" << "\n";    return false;}
Try if you can use this.

EDIT: The elements number can always be set to five in this case. I'll leave it to

you to adjust this ;).

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

That's because it's not going through the array(s). It's only testing the first value

of each array against each other (remember: you need to check every player choice against

all of the computer's selections, not just the first one, so you need to cycle through the

computer's array once for each player selection). You can actually recycle my is_in_array

function from above for this:

 

bool is_in_array(int to_test,int elements,int* array) {// to_test: element to test array for// elements: the number of elements in an array that are to be// tested. Does not need to be as high as the array element// count.for (int i=0;i<elements;i++) {if (*(array+i)==to_test) {cout << "card matches" << "\n";return true;}}cout << "card does not match" << "\n";return false;}
Try if you can use this.

 

Oh jesus christ I forgot the most basic thing about an array. Thank you, I'm taking a break for today. I've been working on this since 8 this morning and it's now 5 lol

Link to comment
Share on other sites

Link to post
Share on other sites

Yes sometimes you need to give your head a rest to get some perspective on things and

let your thoughts sort themselves out a bit :lol:

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Yes sometimes you need to give your head a rest to get some perspective on things and

let your thoughts sort themselves out a bit :lol:

But seriously, thank you. You've been beyond helpful

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×