Solved

New to C programming; don't understand error msg

Posted on 2003-10-21
10
262 Views
Last Modified: 2010-04-15
Hi, I'm new to C programming.  I've read some tutorials online to get the fundamentals and decided to start out by programming a simple BlackJack program.  I use CodeWarrior to compile it and then I run down the list of errors and try to fix them one by one.  So far I've got it down to only 88 errors : )  But now I'm stuck on one that I can't figure out.  

When I define the function userdraw(), which allows the player to draw from a 52 card deck


int userdraw(int usercards[5], int *unumber, int p)
{
      int ucard = 0;
      *unumber = &usercards[p]
      getcard(ucard);     //<-------this is where the error shows
      *unumber = ucard;
      unumber++;
      p++;
}


I get the message:

--------------------------------------------------------
Error : cannot convert
'int *' to
'int'
Blackjack.c line 30   getcard(ucard);
--------------------------------------------------------

What does this mean?  I know the * symbol has to do with arrays and pointers but have no idea what it means here.  Any solution/suggestion you guys can offer will be greatly appreciated

If you need more info, here is all of the program that I have writen so far, and yes, I know, it's full of errors.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
time_t time(time_t *timer);

int r;

int getcard(int card) /* gets a random number from 1 to 52 */
{
      /* int i; */
      unsigned int seed;
            

      for (seed = 1; seed <= 5; seed++) {
            srand(seed);
            }
      
      card = rand()%51+1;
      return card;
            
}
      
int userdraw(int usercards[5], int *unumber, int p)
{
      int ucard = 0;
      *unumber = &usercards[p]
      getcard(ucard);     //<-------this is where the error shows
      *unumber = ucard;
      unumber++;
      p++;
}

int dealerdraw(int dealercards[5], int *dnumber, int q)
{
      int card = 0;
      getcard(card);
      dealercards[dnumber] = card;
      dnumber++;
      q++;
}

int cardsort(int hand[5], int redraw, int total)
{
      int numvalue;
      int cnumber = 0;
      
      while(cnumber < 4)
      {
            int i = cnumber + 1;
            while (i < 4)
            {
                  if (hand[cnumber] == hand[i])
                  redraw = 1;
                  return redraw;
            }
            i = 0;
            cnumber = 0;
            
            numvalue = 13 - hand[cnumber] % 13;
            
            if (numvalue > 10)
                  numvalue = 10;
            
            total += numvalue;
            
            if (cnumber > 2 && hand[cnumber] == 1 && total < 21)
                  {
                  hand[cnumber] = 11;
                  total += 10;
                  }
                  
            if (cnumber > 2 && hand[cnumber] == 11 && total > 21)
                  {
                  hand[cnumber] = 1;
                  total -= 10;
                  }
                                     
            cnumber++;
      }
      
      
}

int firstdeal()
{
      
      int dealercards[5];
      int usercards[5];
      int i = 0;
      int p = 0
      int q = 0
      int *unumber = &usercards[0];
      int *dnumber = &dealercards[0];
      int usertotal = 0;
      int dealertotal = 0;
      int redraw;
      while (i < 5)
      {
            dealercards[i] = 0;
            usercards[i] = 0;
            i++;
      }      
      i = 0;
      
      dealerdraw(dealercards[], *dnumber, q);
      dealerdraw(dealercards[], *dnumber, q);
            
      cardsort(dealercards, redraw, dealertotal);
      
      while redraw      
      {
            dnumber--;
            dealerdraw();
            cardsort(dealercards, redraw, dealertotal);
            
      }
      
      userdraw();
      userdraw();
      
      cardsort(usercards, redraw, usertotal);
      
      while redraw
      {
            unumber--;
            userdraw();
            cardsort(usercards, redraw, usertotal);
      }      
      displaytable()
      
}

int displaytable()
{
      int i = 0;
      char dschar[5];
      char dfchar[5];
      char uschar[5];
      char ufchar[5];
      
      for (i = 0; i < 5; i++)
      {
            dschar[i] = dealercards[i] / 13 + 167;
            uschar[i] = usercards[i] / 13 + 167;
            
            if dealercards[i] == 11
                  dfchar[i] = 'J';
            if dealercards[i] == 12
                  dfchar[i] = 'Q';
            if dealercards[i] == 13
                  dfchar[i] = 'K';
            if dealercards[i] == 1
                  dfchar[i] = 'A';            
            if dealercards[i] == 10
                  dfchar[i] = 'T';      

            if usercards[i] == 11
                  ufchar[i] = 'J';
            if usercards[i] == 12
                  ufchar[i] = 'Q';
            if usercards[i] == 13
                  ufchar[i] = 'K';
            if usercards[i] == 1
                  ufchar[i] = 'A';
            if usercards[i] == 10
                  ufchar[i] = 'T';
            
            if usercards[i] < 9
                  ufchar[i] = usercards[i] + 47                  
            
            if dealercards[i] < 0
                  dfchar[i] = dealercards[i] + 47
                  
            if usercards[i] == 0
            {
                  ufchar[i] = 'x';
                  uschar[i] = 'x';      
            }
            
            
            
      }
      

      printf("----BlackJack v1.0 by John Saville---- \n \n \n");
      printf("-------     -------     -------     -------     ------- \n");
      printf("-%c   %c-     -%c   %c-     -%c   %c-     -%c   %c-     -%c   %c- \n" , uschar[0], ufchar[0], uschar[1], ufchar[1], uschar[2], ufchar[2], uschar[3], ufchar[3], uschar[4], ufchar[4]);
      printf("-     -     -     -     -     -     -     -     -     - \n");
      printf("- %c -     - %c -     - %c -     - %c -     - %c - \n" , ufchar[0], ufchar[1], ufchar[2], ufchar[3], ufchar[4]);
      printf("-     -     -     -     -     -     -     -     -     - \n");
      printf("-%c   %c-     -%c   %c-     -%c   %c-     -%c   %c-     -%c   %c- \n" , uschar[0], ufchar[0], uschar[1], ufchar[1], uschar[2], ufchar[2], uschar[3], ufchar[3], uschar[4], ufchar[4]);
      printf("-------     -------     -------     -------     ------- \n");
      
      printf("\n \n \n \n")
      
      printf("-------     -------     -------     -------     ------- \n");
      printf("-%c   %c-     -%c   %c-     -%c   %c-     -%c   %c-     -%c   %c- \n" , dschar[0], dfchar[0], dschar[1], dfchar[1], dschar[2], dfchar[2], dschar[3], dfchar[3], dschar[4], dfchar[4]);
      printf("-     -     -     -     -     -     -     -     -     - \n");
      printf("- %c -     - %c -     - %c -     - %c -     - %c - \n" , dfchar[0], dfchar[1], dfchar[2], dfchar[3], dfchar[4]);
      printf("-     -     -     -     -     -     -     -     -     - \n");
      printf("-%c   %c-     -%c   %c-     -%c   %c-     -%c   %c-     -%c   %c- \n" , dschar[0], dfchar[0], dschar[1], dfchar[1], dschar[2], dfchar[2], dschar[3], dfchar[3], dschar[4], dfchar[4]);
      printf("-------     -------     -------     -------     ------- \n");
      
      
}

main() {
      printf("BlackJack by John Saville \n");
      printf("\n \n \n");
      firstdeal();
      return 0;
}
0
Comment
Question by:someotherdude
10 Comments
 
LVL 5

Accepted Solution

by:
migoEX earned 25 total points
Comment Utility
I think the error comes from the previous line:
*unumber = &usercards[p];

1) unumber is a 'int*' (pointer to int), so *unumber is 'int'
2) usercards is an array of ints 'int[]', usercards[p] in 'int', and '&usercards[p]' is 'int*'

So you try to assign a 'int*' value to 'int'

You probably meant to write
*unumber = usercards[p];

(& before a variable means "address of")
0
 
LVL 5

Assisted Solution

by:dennis_george
dennis_george earned 25 total points
Comment Utility
Hi man....

   uf..... what a code man....... Its all full of syntax errors.... most of the places no semicolon..... in conditional statements and in loops no bracket......
 
you are not at all distinguishing between a pointer and its value... you are using it as u wish.... where ever you need value you are passing pointer and where ever you need pointer you are passing value of that pointer...

See if you declare something like

int *ptr;  // you are declaring a pointer of type int....
int num = 20 ;

// when you want to assign some address to the pointer variable
ptr = &num ;   // ptr is the pointer and *ptr is the value pointed by that pointer
                     // you take address of any variable by &

// when you want to access the value of the pointer then use *
int val = *ptr ;
*ptr = 35 ; // now num also will changed to 35 from 20 ;

And many more errors i removed some of them ....  after i got tired .......

Before making such a big program... first try to make small programs and learn and master the fundamentals.....


Hope got this

Dennis ..

*******************************



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
time_t time(time_t *timer);

int r;

int getcard(int card) /* gets a random number from 1 to 52 */
{
     /* int i; */
     unsigned int seed;
         

     for (seed = 1; seed <= 5; seed++) {
          srand(seed);
          }
     
     card = rand()%51+1;
     return card;
         
}
     
int userdraw(int usercards[5], int *unumber, int p)
{
     int ucard = 0;
     unumber = &usercards[p];  // Dennis :: Semicolon missing
                                                // Dennis :: assigning pointer to int
     getcard(ucard);     //<-------this is where the error shows
     *unumber = ucard;
     unumber++;
     p++;
       return 0 ;
}

int dealerdraw(int dealercards[5], int *dnumber, int q)
{
     int card = 0;
     getcard(card);
     dealercards[*dnumber] = card; // Dennis :: array index should be integer not pointer
     dnumber++;
     q++;
       return 0 ;
}

int cardsort(int hand[5], int redraw, int total)
{
     int numvalue;
     int cnumber = 0;
     
     while(cnumber < 4)
     {
          int i = cnumber + 1;
          while (i < 4)
          {
               if (hand[cnumber] == hand[i])
               redraw = 1;
               return redraw;
          }
          i = 0;
          cnumber = 0;
         
          numvalue = 13 - hand[cnumber] % 13;
         
          if (numvalue > 10)
               numvalue = 10;
         
          total += numvalue;
         
          if (cnumber > 2 && hand[cnumber] == 1 && total < 21)
               {
               hand[cnumber] = 11;
               total += 10;
               }
               
          if (cnumber > 2 && hand[cnumber] == 11 && total > 21)
               {
               hand[cnumber] = 1;
               total -= 10;
               }
                               
          cnumber++;
     }
     
     
}

int displaytable()
{
     int i = 0;
     char dschar[5];
     char dfchar[5];
     char uschar[5];
     char ufchar[5];
     
  /*
     for (i = 0; i < 5; i++)
       {
            dschar[i] = dealercards[i] / 13 + 167;
            uschar[i] = usercards[i] / 13 + 167;
           
            if dealercards[i] == 11
                 dfchar[i] = 'J';
            if dealercards[i] == 12
                 dfchar[i] = 'Q';
            if dealercards[i] == 13
                 dfchar[i] = 'K';
            if dealercards[i] == 1
                 dfchar[i] = 'A';          
            if dealercards[i] == 10
                 dfchar[i] = 'T';    
 
            if usercards[i] == 11
                 ufchar[i] = 'J';
            if usercards[i] == 12
                 ufchar[i] = 'Q';
            if usercards[i] == 13
                 ufchar[i] = 'K';
            if usercards[i] == 1
                 ufchar[i] = 'A';
            if usercards[i] == 10
                 ufchar[i] = 'T';
           
            if usercards[i] < 9
                 ufchar[i] = usercards[i] + 47              
           
            if dealercards[i] < 0
                 dfchar[i] = dealercards[i] + 47
                 
            if usercards[i] == 0
            {
                 ufchar[i] = 'x';
                 uschar[i] = 'x';    
            }
           
           
           
       }
       */
 

     printf("----BlackJack v1.0 by John Saville---- \n \n \n");
     printf("-------     -------     -------     -------     ------- \n");
     printf("-%c   %c-     -%c   %c-     -%c   %c-     -%c   %c-     -%c   %c- \n" , uschar[0], ufchar[0], uschar[1], ufchar[1], uschar[2], ufchar[2], uschar[3], ufchar[3], uschar[4], ufchar[4]);
     printf("-     -     -     -     -     -     -     -     -     - \n");
     printf("- %c -     - %c -     - %c -     - %c -     - %c - \n" , ufchar[0], ufchar[1], ufchar[2], ufchar[3], ufchar[4]);
     printf("-     -     -     -     -     -     -     -     -     - \n");
     printf("-%c   %c-     -%c   %c-     -%c   %c-     -%c   %c-     -%c   %c- \n" , uschar[0], ufchar[0], uschar[1], ufchar[1], uschar[2], ufchar[2], uschar[3], ufchar[3], uschar[4], ufchar[4]);
     printf("-------     -------     -------     -------     ------- \n");
     
     printf("\n \n \n \n");
     
     printf("-------     -------     -------     -------     ------- \n");
     printf("-%c   %c-     -%c   %c-     -%c   %c-     -%c   %c-     -%c   %c- \n" , dschar[0], dfchar[0], dschar[1], dfchar[1], dschar[2], dfchar[2], dschar[3], dfchar[3], dschar[4], dfchar[4]);
     printf("-     -     -     -     -     -     -     -     -     - \n");
     printf("- %c -     - %c -     - %c -     - %c -     - %c - \n" , dfchar[0], dfchar[1], dfchar[2], dfchar[3], dfchar[4]);
     printf("-     -     -     -     -     -     -     -     -     - \n");
     printf("-%c   %c-     -%c   %c-     -%c   %c-     -%c   %c-     -%c   %c- \n" , dschar[0], dfchar[0], dschar[1], dfchar[1], dschar[2], dfchar[2], dschar[3], dfchar[3], dschar[4], dfchar[4]);
     printf("-------     -------     -------     -------     ------- \n");
     
     
}



int firstdeal()
{
     
     int dealercards[5];
     int usercards[5];
     int i = 0;
     int p = 0 ; // Dennis :: Semicolon missing
     int q = 0 ; // Dennis :: Semicolon missing
     int *unumber = &usercards[0];
     int *dnumber = &dealercards[0];
     int usertotal = 0;
     int dealertotal = 0;
     int redraw;
     while (i < 5)
     {
          dealercards[i] = 0;
          usercards[i] = 0;
          i++;
     }    
     i = 0;
     
       // Dennis :: when you pass an array you should not write the [] bracket
       // Dennis :: when passing pointer you should pass pointer only
       // Dennis :: int *ptr;  here if you only specify ptr then it is a pointer
       // Dennis :: but if you specify *ptr then its value pointed by ptr
     dealerdraw(dealercards, dnumber, q);
     dealerdraw(dealercards, dnumber, q);
         
     cardsort(dealercards, redraw, dealertotal);
     
       // Dennis :: put the loop condition else its a infinite loop
     while( redraw)    
     {
          dnumber--;
              // Dennis :: put the required parameters here
          //dealerdraw();
          cardsort(dealercards, redraw, dealertotal);
         
     }
     
       // Dennis :: put the required parameters here
     //userdraw();
     //userdraw();
     
     cardsort(usercards, redraw, usertotal);
     
    /*
     while redraw
         {
              unumber--;
              userdraw();
              cardsort(usercards, redraw, usertotal);
         }    */
     
     displaytable();
     
}

main() {
     printf("BlackJack by John Saville \n");
     printf("\n \n \n");
     firstdeal();
     return 0;
}
0
 

Expert Comment

by:kalyanreddy_kk
Comment Utility
The statement "*unumber = ucard;" is not correct, since when ucard is specifying an address and u r trying to store it in an interger variable "*unumber"

Remember, when u say, *variablename, u r referring to the location where the value is stored, and not the location where the address is stored.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:shamstar
Comment Utility
Hi,

If you put a semi-colon at the end of line 29, then you should see that the error is actually on line 29 and not on line 30.
I dont understand why you are trying to assign a value to unumber in line 29, when its not even used before its assigned a new value a few rows down.
Maybe you could just get rid of that line?

More specific to your question:

I think you might also be a little confused at the use of the inderication operator (*).  This operator allows you to reference the data that a pointer points to.  So on line 29, currently you are saying that the value of unmber (which should be an integer) should be assigned with a pointer to an integer - that clearly is not possible, which is why the compiler is saying that it cannot covert a pointer to an intger to an actual integer.  I think what you mean to do there is make unmber point to the same as whats on the right hand side, you can do this by removing the * at line 29.

That should sort out your problem, but it does make me wonder that you might have made similar mistakes in the same place which might not be caught by the compiler, thus giving you unexpected results.

Let me know how it goes!
0
 
LVL 8

Expert Comment

by:shamstar
Comment Utility
Whats the latest someotherdude??
0
 

Author Comment

by:someotherdude
Comment Utility
I decided to scrap what I had and start anew.  I'm still working on the new one and so far have gotten rid of the syntax errors.  I'm hoping to have it finished by next week.  Anyway, here it is.  Remember this is my first C program and it is unfinished.


// A minimal Win32 console app

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
time_t time(time_t *timer);


int getcards(int cardsavail[]) /* gets ten random numbers ranging from 2 to 52.  These will       

                  be all the cards available to both the user and the dealer.             

            */
{

      
      int i;
      int j;
      int k;
      
            
      for (i = 0; i < 10; i++)
      {
            
      
            srand(time(NULL) + i);
            cardsavail[i] = rand()%52+1;
            
            
            for (j = 0; j < i; j++)  // decrements loop if two cards match
            {
                  
                  for (k = j + 1; k < i; k++)
                  {
                        if (cardsavail[k] == cardsavail[j])
                              i--;
                  }            
            }      
      }      
      
      
            
}


int drawcard(int hand[], int playerplace, int place, int cardsavail[])
//draws a card
{

      
      hand[playerplace] = cardsavail[place];
      place = place + 1;
      playerplace = playerplace + 1;
      return playerplace;
      return place;
}      


      
      
int tabledisplay(int dealercards[], int usercards[], int userplace, int dealerplace, int

pot, int cardsavail[], char suitvalue[])
//provides output to user
{
      int even = 0;
      int odd = 0;
      int item = 0;
      int printc = 0;
      char prints = 0;
      printf("\n BlackJack by John Saville \n");
      printf("\n \n \n \n");

      printf("Dealer's Hand \n \n");
      for (item = 0; item < dealerplace; item++)
      {

            if (item == 0)
            printf("%c",'X'); // first dealer card
            if (dealercards[item] == 13){
            printf("%c",'A');
            dealercards[item] = 11;
            }if (dealercards[item] == 10){
            printf("%c",'J');
            dealercards[item] = 10;
            }if (dealercards[item] == 11){
            printf("%c",'Q');
            dealercards[item] = 10;
            }if (dealercards[item] == 12){
            printf("%c",'K');
            dealercards[item] = 10;
            }if (dealercards[item] < 11){
            printf("%d",dealercards[item]);
            even = item * 2;}
            
            if (item == 0)
            printf("%c",' ');// is hidden
            if (item != 0)
            printf("%c ",suitvalue[even]);
            
      }      
      item = 0;
      printf("\n \n \n \n");
      printf("Your Hand \n \n");
      for (item = 0; item < userplace; item++)
      
      {
            if (usercards[item] == 13){
            printf("%c",'A');
            usercards[item] = 11;
            }if (usercards[item] == 10){
            printf("%c",'J');
            usercards[item] = 10;
            }if (usercards[item] == 11){
            printf("%c",'Q');
            usercards[item] = 10;
            }if (usercards[item] == 12){
            printf("4c",'K');
            usercards[item] = 10;
            }if (usercards[item] < 11){
            printf("%d",usercards[item]);
            odd = (item + 1) * 2 - 1;}
            printf("%c ",suitvalue[odd]);
            
      }      
      printf("\n \n \n \n");
      
      
      printf("\n \n \n \n");
      printf("Pot %d  $ \n", pot);
      
}

cardsort(int cardsavail[], char suitvalue[])
//determines the suit and value of each card
{
      int count = 0;
      
      for (count = 0; count < 10; count++)
      {
            suitvalue[count] = cardsavail[count] / 13 + 3;      
      
      }      

      count = 0;      

      for (count = 0; count < 10; count++)
      {
            cardsavail[count] = cardsavail[count] % 14;
      }

}


main()
{

      

      int pot = 1000;
      int cardsavail[10] = {0,0,0,0,0,0,0,0,0,0};
      char suitvalue[10] = {0,0,0,0,0,0,0,0,0,0};
      int usercards[5] = {0,0,0,0,0};
      int dealercards[5] = {0,0,0,0,0};
      int place = 0;
      int userplace = 0;
      int dealerplace = 0;
      int count = 0;
      int wager = 0;
      int totalwager = 0;
      int usertotal = 0;
      int dealertotal = 0;
      char play = 's';
      char again = 'y';

      while (again == 'y')
      {
      getcards(cardsavail);
      cardsort(cardsavail, suitvalue);
      for (count = 0; count < 2; count++)
      {
            drawcard(dealercards, dealerplace, place, cardsavail);
            place++;
            dealerplace++;
            drawcard(usercards, userplace, place, cardsavail);
            place++;
            userplace++;
      }
      
      count = 0;
      for (count = 0; count < 5; count++);
      {
            usertotal = usertotal + usercards[count];
            dealertotal = dealertotal + dealercards[count];
      }      
            
      tabledisplay(dealercards, usercards, userplace, dealerplace, pot, cardsavail,             

      suitvalue);

      printf("Enter the amount you wish to wager followed by ENTER followed by an h for

hit or an s for      stand followed by ENTER \n");
      scanf("%d %c", &wager, &play);      
      printf("you wagered %d $ and choose %c", wager, play);
            
      totalwager = totalwager + wager;
      pot = pot - totalwager;
      
      
            
      if (play == 'h');{      
      drawcard(usercards, userplace, place, cardsavail);
      place++;
      userplace++;
      usertotal = usertotal + usercards[userplace];}
      

      if (usertotal > 21);{
      count = 0;
      for (count = 0; count < userplace; count++)
      {
            if (usercards[userplace] = 11)
            usercards[userplace] = 1;
      }
      if (usertotal > 21);{
      tabledisplay(dealercards, usercards, userplace, dealerplace, pot, cardsavail,             

      suitvalue);             
      printf("you bust, dealer wins \n");
      totalwager = 0;
      printf("Again y/n");
      scanf("%c", &again);}}
      
      

      

      if (play == 's');{      
      
      while (dealertotal < 15);
      {
            count = 0;
            for (count = 0; count < dealerplace; count++)
            {
                  if (dealercards[count] = 11){
                  if (dealertotal > 21){
                  dealercards[count] = 1;}}
                  
            }
            drawcard(dealercards, dealerplace, place, cardsavail);
      }
            
      if (dealertotal > 21){
      printf("dealer bust, you win \n");
      pot = pot + (totalwager * 2);
      printf("Again y/n");
      scanf("%c", &again);}}
      


      }


      return 0;
}
0
 
LVL 8

Expert Comment

by:shamstar
Comment Utility
Hummm... I think all the experts picked up some of the problems, and normally a split all experts would be the way to go.. but its only 50 points.  So would say it was answered by migoEX and possibly split with dennis_george
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now