comparing char type in c++

if(char[i] == char[y]) - never finds match

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 57085 Views - Last Post: 21 November 2008 - 07:41 PM Rate Topic: -----

#1 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

comparing char type in c++

Post icon  Posted 17 November 2008 - 03:33 AM

I am going round and round with the comparing char or string type in c++ and which is the best (easiest for noob!) way to handle comparison operations.

I don't get it yet when I read string and c string and CString and char and all the conversion methods.

What I need to do to keep my brain from melting is pick between char or string so I don't have to do crazy conversions before simple comparisons between two single characters.

Here is my code example which never finds a match when I've really really really ran it bunches of times to make sure I am giving it what should be a matching pair, which I could be messing up of course. I'll give u what I think are the important bits, lemme know if u need to see more ok?

I would happily change everything over to string rather than char if it would solve the problem and would let me use single characters passed to functions letting me use the == without extensive conversions.

[edit] OH! BTW, I tried changing the comparison in the sub function to use strcmp instead and received a long list of errors, it didn't like it at all[/edit]

#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

void CheckCards(char Card_Board [][4], char Card_Array[][4],int i1,int i2,int i3,int i4, char& Flag);

int main( )
{
	const int Element_Size = 4;
	
	char Card_Array [Element_Size][Element_Size] =
	{ {1,1,2,2},
	{3,3,4,4},
	{5,5,6,6},
	{7,7,8,8}
	};
	
	char Card_Board[4][4];
	char Flag = 'y';

cout << "Enter an X and Y position of the first card to flip.\n";
	   cin >> i1;
	   cin >> i2;
	   cout << "Enter an X and Y position of the second card to flip:\n";
	   cin >> i3;
	   cin >> i4;;
	   
	   if((i1!=i3) || (i2!=i4))//check for repeated card
	   {   //make sure card hasnt been matched yet
		   if( (Card_Board[i1][i2]='*') && (Card_Board[i3][i4]='*') )
		   {
			   cout << "\nSo far so good.\n";
			   //call the card compare func
			   CheckCards(Card_Board, Card_Array, i1, i2, i3, i4, Flag);
		   }
	   }
return 0;
}

//sub function - heres important comparison part that never matches
void CheckCards(char Card_Board[][4], char Card_Array[][4], int i1, int i2, int i3, int i4, char& Flag)
{
	 //is == the wrong operator for the data type?
	 if (Card_Array[i1][i2] == Card_Array[i3][i4])
	 {
		Card_Board[i1][i2] = Card_Array[i1][i2];
		Card_Board[i3][i4] = Card_Array[i3][i4];
		
		//PrintBoard(Card_Board, Card_Array)
		cout << "\nCongratulations, you found a match!\n";
	 }
	 else
	 {
		 cout << "\nEnter any character to continue or x to end.\n";
		 cin >> Flag;
	 }
return;
}



This post has been edited by badjava: 17 November 2008 - 03:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: comparing char type in c++

#2 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: comparing char type in c++

Posted 17 November 2008 - 04:41 AM

View Postbadjava, on 17 Nov, 2008 - 02:33 AM, said:

I am going round and round with the comparing char or string type in c++ and which is the best (easiest for noob!) way to handle comparison operations.


In C++ all comparisons are easy.

Quote

...What I need to do to keep my brain from melting is pick between char or string so I don't have to do crazy conversions before simple comparisons between two single characters.


What are you attempting to do ... ?
step 1.
step 2.
step 3.

...


Quote

Here is my code example ...
it won't compile ... so won't find anything.

Again ... what are you trying to do?

Break it down into little steps and code and test each step

like

char a ='a';
char b = 'a';

if ( a == b ) cout << "a == b is true";
else cout << "Better get a new PC";

This post has been edited by David W: 17 November 2008 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: comparing char type in c++

Posted 17 November 2008 - 05:53 AM

View PostDavid W, on 17 Nov, 2008 - 03:41 AM, said:

In C++ all comparison are easy.


Very helpful ty

Quote

What are you attempting to do ... ?
step 1.
step 2.
step 3.

compare the two char array elements with a == operator that I commented in the code.

Quote

it won't compile ... so won't find anything.
Again ... what are you trying to do?


compare the two char array elements with a == operator that I commented in the code.

Quote

it won't compile ... so won't find anything.


I mentioned that I just put in enough so you could get the idea of how I initialized the array, declared the functions and passed the arrays to the function. Then I attempted to compare the char array elements with a == operator and I thought I had comments in all the right places to explain my code snippets.

Following is the full code, it compiles and runs but what you will get from the running program where you can't see the one char array comparison with the == operator I highlighted in the source code I'm not sure. There aren't any error messages, it compiles and runs fine, I am looking for help with the way the comparison is acting with function passed char array elements and the == operator or a fault in my comparison logic, thank you.

#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

void InitializeBoard (char Card_Board [][4], const int Element_Size);
void CheckCards(char Card_Board [][4], char Card_Array[][4],int i1,int i2,int i3,int i4, char& Flag);

int main( )
{
	const int Element_Size = 4;
	
	char Card_Array [Element_Size][Element_Size] =
	{ {1,1,2,2},
	{3,3,4,4},
	{5,5,6,6},
	{7,7,8,8}
	};
	
	char Card_Board[4][4];
	char Flag = 'y';
	
	int Guessed = 0;
	int Guess_Count = 0;
	char Guess_Flag = 'n';
	int i1, i2, i3, i4;
	
	cout << "\nFind all 8 matching pairs on the board.\n";
	
	InitializeBoard(Card_Board, Element_Size);
	
	while((Guess_Count<(Element_Size*2)) && (Flag != 'x'))
	{

	
	   cout << "Enter an X and Y position of the first card to flip.\n";
	   cin >> i1;
	   cin >> i2;
	   cout << "Enter an X and Y position of the second card to flip:\n";
	   cin >> i3;
	   cin >> i4;;
	   
	   if((i1!=i3) || (i2!=i4))
	   {
		   if( (Card_Board[i1][i2]='*') && (Card_Board[i3][i4]='*') )
		   {
			   cout << "\nSo far so good.\n";
			   CheckCards(Card_Board, Card_Array, i1, i2, i3, i4, Flag);
		   }
		   
		   else
			   cout << "\nCard has already been matched.\n";
	   }
	   else
		   cout << "\nUnable to choose the same card to compare.\n";
		   
	   system("pause");
	   
	   }
	
	//call draw board
	//print number of guesses to solve game = Guess_Count;
	
return 0;
}

void CheckCards(char Card_Board[][4], char Card_Array[][4], int i1, int i2, int i3, int i4, char& Flag)
{
	 if (Card_Array[i1][i2] == Card_Array[i3][i4])
	 {
		Card_Board[i1][i2] = Card_Array[i1][i2];
		Card_Board[i3][i4] = Card_Array[i3][i4];
		
		//PrintBoard(Card_Board, Card_Array)
		cout << "\nCongratulations, you found a match!\n";
	 }
	 else
	 {
		 cout << "\nEnter any character to continue or x to end.\n";
		 cin >> Flag;
	 }
return;
}
	 
void InitializeBoard(char Card_Board[][4], const int Element_Size)
{
	 
	 int row = 0;
	 int col = 0;
	 
	 // print header row \n;
	 //cout << "\n\n\nFind all the matching pairs on the board.\n";
	 cout << "	1 2 3 4 \n";
	 cout << "   =========\n";
	 
	 for(row=0;row < Element_Size; row++)
	 {
		//print row number;
		cout << row+1 << " | ";
		for(col=0;col < Element_Size; col++)
		{
		   Card_Board[row][col]= '*';
		   //print Card_Board [row] [col];
		   cout << Card_Board [row] [col] << " ";
		}
		//print | /n;
		cout << "|\n";		
	 }
	 cout << "   =========\n";//print footer row

//system("pause");
return;
}

This post has been edited by badjava: 17 November 2008 - 05:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: comparing char type in c++

Posted 17 November 2008 - 06:00 AM

View Postbadjava, on 17 Nov, 2008 - 04:53 AM, said:

void CheckCards(char Card_Board[][4], char Card_Array[][4], int i1, int i2, int i3, int i4, char& Flag)
{
	 if (Card_Array[i1][i2] == Card_Array[i3][i4])
	 {
		Card_Board[i1][i2] = Card_Array[i1][i2];
		Card_Board[i3][i4] = Card_Array[i3][i4];
		
		//PrintBoard(Card_Board, Card_Array)
		cout << "\nCongratulations, you found a match!\n";
	 }
	 else
	 {
		 cout << "\nEnter any character to continue or x to end.\n";
		 cin >> Flag;
	 }
return;
}
}


I think I may have just found my logic error. I forgot about the array elements actually starting at 0,0 and I'm comparing it with a straight user input coordinate starting at 1,1. I need to knock 1 each off the input coordinates entered by the user and compare that value against the array....I think. Going off to try it now :pirate:

[edit] Yeah that was it, after I subtracted 1 each from the user input and used those values in the array elements being compared I get matches on all the right coordinates. WHEW!

I hope I get this extra credit assignment fully functional, I could use it!

This post has been edited by badjava: 17 November 2008 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

#5 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: comparing char type in c++

Posted 17 November 2008 - 01:47 PM

View Postbadjava, on 17 Nov, 2008 - 05:00 AM, said:

View Postbadjava, on 17 Nov, 2008 - 04:53 AM, said:

void CheckCards(char Card_Board[][4], char Card_Array[][4], int i1, int i2, int i3, int i4, char& Flag)
{
	 if (Card_Array[i1][i2] == Card_Array[i3][i4])
	 {
		Card_Board[i1][i2] = Card_Array[i1][i2];
		Card_Board[i3][i4] = Card_Array[i3][i4];
		
		//PrintBoard(Card_Board, Card_Array)
		cout << "\nCongratulations, you found a match!\n";
	 }
	 else
	 {
		 cout << "\nEnter any character to continue or x to end.\n";
		 cin >> Flag;
	 }
return;
}
}


I think I may have just found my logic error. I forgot about the array elements actually starting at 0,0 and I'm comparing it with a straight user input coordinate starting at 1,1. I need to knock 1 each off the input coordinates entered by the user and compare that value against the array....I think. Going off to try it now :pirate:

[edit] Yeah that was it, after I subtracted 1 each from the user input and used those values in the array elements being compared I get matches on all the right coordinates. WHEW!

I hope I get this extra credit assignment fully functional, I could use it!


Glad you got it working ... (but see comments.)

void CheckCards(char Card_Board[][4], char Card_Array[][4], int i1, int i2, int i3, int i4, char& Flag)
{
	 if (Card_Array[i1][i2] == Card_Array[i3][i4])
	 {
		Card_Board[i1][i2] = Card_Array[i1][i2];
		Card_Board[i3][i4] = Card_Array[i3][i4];
		
		//PrintBoard(Card_Board, Card_Array)
		cout << "\nCongratulations, you found a match!\n";
	 }
	 else
	 {
		 cout << "\nEnter any character to continue or x to end.\n";
		 cin >> Flag;
	 }
// return; // this line is redundant here ...
//} // this } is an error and will keep the program from compiling
}




Sometimes you may find the program flows a little smoother if you return a value or a bool as a 'returned value' like this ...

char CheckCards(char Card_Board[][4], char Card_Array[][4], int i1, int i2, int i3, int i4)
{
    char Flag;

    ...

    cin >> Flag;
    return Flag;
}


bool CheckCards(char Card_Board[][4], char Card_Array[][4], int i1, int i2, int i3, int i4)
{
    char Flag;

    ...

    cin >> Flag;
    return (Flag == 'y' || Flag=='Y');
}

Was This Post Helpful? 0
  • +
  • -

#6 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: comparing char type in c++

Posted 17 November 2008 - 02:08 PM

View PostDavid W, on 17 Nov, 2008 - 12:47 PM, said:

Sometimes you may find the program flows a little smoother if you return a value or a bool as a 'returned value' like this ...
return (Flag == 'y' || Flag=='Y');


You know the return boolean isn't such a bad idea. I try to make all my functions as simple as possible which usually means a void function but the flag return would make the function more simple with less passed values, not bad. But are you sure your code example checks the value of 'Flag' and returns the value correctly? What if someone enters something other than the Y or y, it looks like that would return a failure code and perhaps make the program fail out at that point?
Was This Post Helpful? 0
  • +
  • -

#7 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: comparing char type in c++

Posted 17 November 2008 - 02:34 PM

View Postbadjava, on 17 Nov, 2008 - 01:08 PM, said:

View PostDavid W, on 17 Nov, 2008 - 12:47 PM, said:

Sometimes you may find the program flows a little smoother if you return a value or a bool as a 'returned value' like this ...
return (Flag == 'y' || Flag=='Y');


You know the return boolean isn't such a bad idea. I try to make all my functions as simple as possible which usually means a void function but the flag return would make the function more simple with less passed values, not bad. But are you sure your code example checks the value of 'Flag' and returns the value correctly? What if someone enters something other than the Y or y, it looks like that would return a failure code and perhaps make the program fail out at that point?


It's your call how you want to run a program ...

Here's an example program that the function 'more()' defaults to yes ...
(i.e. you specifically must enter a 'n' or a 'N' char it get NO more)

#include <iostream>
using namespace std;

/* forward declaration of functions */

int readNum();
int reverseNum(int);
bool more();


int main()
{   
    int num;
    do
    {
        num=readNum();
        cout << "\n You entered " << num
             << "\n Reversed is " << reverseNum(num) << endl;
    }while( more() );  
}


/* definition of functions */

int readNum()
{
    cout << "\n Enter a number to be reversed: ";
    int num=0;
    cin  >> num;
    cin.clear(); /* in case error flag was set by a non 'int' entry */
    cin.sync(); /* 'flush' cin */
    return num;
}

int reverseNum(int number)
{
    int rev = 0;           
    do   
    {
        rev = rev*10 + number%10;
        number /= 10;
    }while( number ); /* exits when number is zero */
    return rev;
}

bool more()
{
    cout << "\n More (y/n) ? ";
    int reply = cin.get();
    cin.sync();
    return !(reply=='n' || reply=='N');
}

Was This Post Helpful? 0
  • +
  • -

#8 brds   User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: comparing char type in c++

Posted 17 November 2008 - 02:58 PM

View Postbadjava, on 17 Nov, 2008 - 03:08 PM, said:

View PostDavid W, on 17 Nov, 2008 - 12:47 PM, said:

Sometimes you may find the program flows a little smoother if you return a value or a bool as a 'returned value' like this ...
return (Flag == 'y' || Flag=='Y');


You know the return boolean isn't such a bad idea. I try to make all my functions as simple as possible which usually means a void function but the flag return would make the function more simple with less passed values, not bad. But are you sure your code example checks the value of 'Flag' and returns the value correctly? What if someone enters something other than the Y or y, it looks like that would return a failure code and perhaps make the program fail out at that point?


If Flag=='o' it will return 0 (False). So instead of passing Flag around, you can use the function itself for control while(somefunction()) { /* Do this */ }.
Was This Post Helpful? 0
  • +
  • -

#9 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: comparing char type in c++

Posted 17 November 2008 - 03:04 PM

View Postbrds, on 17 Nov, 2008 - 01:58 PM, said:

If Flag=='o' it will return 0 (False). So instead of passing Flag around, you can use the function itself for control while(somefunction()) { /* Do this */ }.


OOOooh hey I like the looks of that. Would you be able to give a little code snippet example? I'm interested in seeing how u would implement/call it, and would you use a void function still?
Was This Post Helpful? 0
  • +
  • -

#10 brds   User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: comparing char type in c++

Posted 17 November 2008 - 03:44 PM

View Postbadjava, on 17 Nov, 2008 - 04:04 PM, said:

OOOooh hey I like the looks of that. Would you be able to give a little code snippet example? I'm interested in seeing how u would implement/call it, and would you use a void function still?


Umm... David W already gave you an example, about 3 posts above this one. :(
Was This Post Helpful? 0
  • +
  • -

#11 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: comparing char type in c++

Posted 17 November 2008 - 07:36 PM

Quote

...I'm interested in seeing how u would implement/call it, and would you use a void function still?...


And brds gave a great generic example here in his post ...

Quote

while(somefunction()) { /* Do this */ }.


So ... No ... you can't return a 'void'.

In C++ return a 'bool' or an 'int'

In C return an int

The while loop will only execute if the 'somefunction()' returns 'true'

C/C++ will use any non-zero value as 'true' ... so 0 is false

C++ also has type bool with values true and false

This post has been edited by David W: 17 November 2008 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

#12 badjava   User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: comparing char type in c++

Posted 17 November 2008 - 10:54 PM

View Postbrds, on 17 Nov, 2008 - 02:44 PM, said:

View Postbadjava, on 17 Nov, 2008 - 04:04 PM, said:

OOOooh hey I like the looks of that. Would you be able to give a little code snippet example? I'm interested in seeing how u would implement/call it, and would you use a void function still?


Umm... David W already gave you an example, about 3 posts above this one. :(


ah sry I cant get past the condescension and arrogance/sarcasm in his posts, I just want to choke em when I catch the smart ass attitude and I don't get much out of the partial "I'm better than you and I'm not going to fully explain my answers" game. That goes against every reason that I'm here and I'm not going to invest in the pain and misery it would take to draw the information out of him. Thanks for attempting to give him the kudos tho, ur a better guy than me I guess.
Was This Post Helpful? 0
  • +
  • -

#13 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: comparing char type in c++

Posted 17 November 2008 - 11:18 PM

View Postbadjava, on 17 Nov, 2008 - 09:54 PM, said:

... I cant get past the ...


There is a lot to learn in programming. I doubt anyone aspires to guess just how much that may be ... anymore :D I only know a very tiny corner. Thank God that one doesn't have to know everything to know something.

'Cutting your teeth' on C or C++ can be a very painful and time consuming thing. I would suggest texts with lot's of GOOD example code ... graded from easy to more complex.

Learning all the rules of syntax in coding is not the only problem. Figuring out how to use all that ... and how to attack the problem at hand ... can also be a problem :D

As you gain experience with different 'solutions' ... you will see more of the beauty of 'intelligent design' and may appreciate your Heavenly Creator more.

Shalom shalom,

David

p.s.

You may appreciate the approach and code examples here:

http://developers-he...index.php/topic,46.0.html

This post has been edited by David W: 17 November 2008 - 11:32 PM

Was This Post Helpful? 0
  • +
  • -

#14 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: comparing char type in c++

Posted 18 November 2008 - 05:49 AM

@badjava
I am not sure whether it was you or someone else, but I think I had discussion over this before... and we also came up with the final answer. Anyways, I consider it was not you and provide what he was able to end up with at that time.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void shuffle(int [][4]);
int main()
{
    char comma, ans = 'y';
    int r1, c1, r2, c2, cards[4][4], i(0);
    bool cardstatus[4][4];  // array to store whether card is flipped or not, you can use char if bool doesn't work for you.    
    bool gameover = false; // if bool doesn't work for you, use char in same way I told for cardstatus.
    int moves;    
    do
    {
        moves = 0;
        //shuffle
        shuffle(cards); // I think shuffing cards is better idea than shuffling base start array.
        //display board
        cout<<"    1 2 3 4\n";
        cout<<"  ";
        for (int i=0; i<=8; i++)
        {
            cout<<"-";
        }
        cout<<endl;
        for (int r=0; r<4; r++)
        {
            cout<<r+1<<" | ";
            for (int c=0; c<4; c++)
            {
                cout<<"* ";
                cardstatus[r][c] = false; // use 0 for false and 1 for true if true/false is not supported.
            }
            cout<<endl;
        }
        cout<<endl;
        do // game starts from here as user will do moves here onwards.
        {
            do
            {
                //selection
                cout<<"Please insert the first card row and column seperated by a comma.\n";
                cin>>r1>>comma>>c1;
                if(cardstatus[r1-1][c1-1] == true) // check if the card is flipped.
                { 
                    cout << "This card is already flipped! Select another card.";
                }
            }while(cardstatus[r1-1][c1-1]!= false);
            do
            {
                cout<<"Please insert the second card row and column seperated by a comma.\n";
                cin>>r2>>comma>>c2;
                if(cardstatus[r2-1][c2-1] == true) // check if the card is flipped.
                { 
                    cout << "This card is already flipped! Select another card.";
                }
                if((r1==r2)&&(c1==c2))
                {
                    cout << "You can't select same card twice!";
                    continue;
                }
            }while(cardstatus[r2-1][c2-1]!= false);
            //fix
            r1--;
            c1--;
            r2--;
            c2--;
            //reveal
            cout<<"    1 2 3 4\n";
            cout<<"  ";
            for (int i=0; i<=8; i++)
            {
                cout<<"-";
            }
            cout<<endl;
            for (int r=0; r<4; r++)
            {
                cout<<r+1<<" | ";
                for (int c=0; c<4; c++)
                {
                    if ((r==r1)&&(c==c1))
                    {
                        cout<<cards[r][c]<<" ";
                    }
                    else if((r==r2)&&(c==c2))
                    {
                        cout<<cards[r][c]<<" ";
                    }
                    else if (cardstatus[r][c] == true)
                    {
                        cout<<cards[r][c]<<" ";
                    }
                    else
                    {
                        cout<<"* ";
                    }
                }
                cout<<endl;
            }                        
            //match?
            if (cards[r1][c1]==cards[r2][c2]) // if cards match, keep them flipped.
            {
                cout << "Cards Matched!"<<endl;
                
                cardstatus[r1][c1] = true;                             
                cardstatus[r2][c2] = true;                            
            }
            cin.get();
            cin.get();
            //this pushes the next board onto a blank screen
            for (int b=0; b<=20; b++)
                cout<<endl;
                
            for (int r=0; r<4; r++) // reprint the board, makes it easy for user to put a new guess.
            {
                cout<<r+1<<" | ";
                for (int c=0; c<4; c++)
                {                
                    if (cardstatus[r][c] == true)
                    {
                        cout<<cards[r][c]<<" ";
                    }
                    else
                    {
                        cout<<"* ";
                    }
                }
                cout<<endl;
            }            
            gameover = true;
            for (int r=0; r<4; r++) // check all card status, they all should be true/flipped to end the game.
            {
                for (int c=0; c<4; c++)
                {
                    if(cardstatus[r][c]==false)
                    {
                      gameover = false;
                      break;
                    }
                }
                if(gameover == false)
                {
                    break;
                }
            }
            moves++; // counting moves here.
            //repeat
        }while(gameover != true); // loop, the only way to get out is to finish the game!
        cout << "Congratulations, You won!!!"<<endl;
        cout << "It Requred " << moves << " moves to finish it."<<endl<<endl;
        cout << "Would you like to play again? (y=Yes/n=No) : ";
        ans = cin.get();
    }while(ans == 'y' || ans == 'Y'); // If user wants to play game again.
    cin.get();
    return 0;
}
void shuffle(int cards[][4])
{
    int start[16]={1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};
    for (int s=0; s<=20; s++)
    {
        for (int x=0; x<16; x++)
        {
            srand((unsigned)time(NULL));
            int i=rand()%15+1;
            int temp=start[x];
            start[x]=start[i];
            start[i]=temp;
        }
    }    
    int i=0;
    for (int r=0; r<4; r++) // put values in cards here
        {
            for (int c=0; c<4; c++)
            {
                cards[r][c]=start[i];
                cout<<cards[r][c];                
                i=i+1;
            }
            cout<<endl;
        }
}




I hope this will help you. :)

Note: As the code was supposed to serve the purpose and not to find the optimized way to resolve the problem. It might not be the best solution. But from beginner's point of view, "Hey, it works...!!!".

@David W
You are absolutely right about gaining experience. I appreciate your efforts to literate users about programming, but it's just that "extra" extra which bother people many times. You can't teach everything up to graduation to a 1st grade student, it's something like that only. I generally try to identify the level of user from his/her posts and then give solutions as per his/her level and bit extra than that. If you go too much ahead than that, it make them uncomfortable. :)
Please don't mind, I was just trying to understand why badjava came up with that type of comment.
Was This Post Helpful? 0
  • +
  • -

#15 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3212
  • View blog
  • Posts: 19,240
  • Joined: 14-September 07

Re: comparing char type in c++

Posted 18 November 2008 - 08:22 AM

kinda offtopic: That smiley face at the end of every post really irritates people. I would recommend not using it. Since it does not convey a colon and a capital 'D'. It looks like a smirk.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2