Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on August 05, 2011, 09:53:04 am

Title: char[8] to char[8] giving error
Post by: Munchor on August 05, 2011, 09:53:04 am
Inspired on the Rock, Papers, Scissors topic I decided to try and make a C version:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main();

int main() {
  char options[][8] = {"rocks", "papers", "scissors"}; //Possible options
  srand( time(NULL) ); //For random integer
 
  /* Define variables needed */
  int user_choice_index;
  char user_choice[8];
  int cpu_choice_index;
  char cpu_choice[8];
  int i;
 
  while (1) {
    /* Get user choice and index */
    fgets(user_choice, sizeof user_choice, stdin);
    for (i=0; i<3; i++)
    {
      if ( user_choice == options[i] )
      {
        user_choice_index = i; //Index of user's choice
        break;
      }
    }
   
    /* Get computer choice */
    cpu_choice_index = ( rand() %3 ); //Random number [0,3]
    cpu_choice = options[cpu_choice_index];
   
    /* Check who wins */
    if ( strcmp(cpu_choice, user_choice) == 0 )
    {
      printf("Tie\n");
    }
    else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
    {
      printf("You won");
    }
    else
    {
      printf("You lose");
    }
  }
 
  return 0;
}

However, when compiling I get:

Quote
rock_papers_scissors.c:33:16: error: incompatible types when assigning to type ‘char[8]’ from type ‘char *’

On the line:

Code: [Select]
cpu_choice = options[cpu_choice_index];
Each member of the array options is of type char[8], as you can see in the declaration:

Code: [Select]
char options[][8] = {"rocks", "papers", "scissors"}; //Possible options
So why does it think it is a char * instead of a char[8]? Thanks!

EDIT

I recorded I was copying addresses and used strcpy(); but the program isn't work that well:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main();

int main() {
  char options[][8] = {"rocks", "paper", "scissors"}; //Possible options
  srand( time(NULL) ); //For random integer
 
  /* Define variables needed */
  int user_choice_index;
  char user_choice[8];
  int cpu_choice_index;
  char cpu_choice[8];
  int i;
 
  while (1) {
    /* Get user choice and index */
    fgets(user_choice, sizeof user_choice, stdin);
    for (i=0; i<3; i++)
    {
      if ( user_choice == options[i] )
      {
        user_choice_index = i; //Index of user's choice
        break;
      }
    }
   
    /* Get computer choice */
    cpu_choice_index = ( rand() %3 ); //Random number [0,3]
    strcpy(cpu_choice, options[cpu_choice_index]);
    printf("%s", cpu_choice);
   
    /* Check who wins */
    if ( strcmp(cpu_choice, user_choice) == 0 )
    {
      printf("Tie\n");
    }
    else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
    {
      printf("You won\n");
    }
    else
    {
      printf("You lose\n");
    }
  }
 
  return 0;
}

When I inputed scissors, here's what happened:

Quote
scissors
scissors�@You won
rocksYou lose
Title: Re: char[8] to char[8] giving error
Post by: Scipi on August 05, 2011, 10:40:14 am
Change
Code: [Select]
cpu_choice = options[cpu_choice_index];

to

Code: [Select]
*cpu_choice = options[cpu_choice_index];
Title: Re: char[8] to char[8] giving error
Post by: Munchor on August 05, 2011, 10:43:05 am
Change
Code: [Select]
cpu_choice = options[cpu_choice_index];

to

Code: [Select]
*cpu_choice = options[cpu_choice_index];

Could you see my edit please? :) And that doesn't work by the way HOMER-16, since you're making an integer from a pointer without a cast.
Title: Re: char[8] to char[8] giving error
Post by: Scipi on August 05, 2011, 11:09:07 am
Ah yeah, I didn't see that.

You can fix the
Quote
scissors�@You won

By making the char array able to hold 9 chars to make the string 0 terminated.

Code: [Select]
char options[][9] = {"rocks", "paper", "scissors"}; //Possible options
  srand( time(NULL) ); //For random integer

  /* Define variables needed */
  int user_choice_index;
  char user_choice[9];
  int cpu_choice_index;
  char cpu_choice[9];

Idk enough C to make it say if you won, lost or tied The Game properly though.

(It keeps saying I lost, so I guess I lost [and you did too]) :P
Title: Re: char[8] to char[8] giving error
Post by: Munchor on August 05, 2011, 11:15:58 am
By making the char array able to hold 9 chars to make the string 0 terminated.

Nicely spotted. The strcmp( is not working, when we tie, it says "You lose" :/ Current code:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main();

int main() {
  char options[][9] = {"rocks", "paper", "scissors"}; //Possible options
  srand( time(NULL) ); //For random integer
 
  /* Define variables needed */
  int user_choice_index;
  char user_choice[9];
  int cpu_choice_index;
  char cpu_choice[9];
  int i;
 
  while (1) {
    /* Get user choice and index */
    fgets(user_choice, sizeof user_choice, stdin);
    for (i=0; i<3; i++)
    {
      if ( user_choice == options[i] )
      {
        user_choice_index = i; //Index of user's choice
        break;
      }
    }
   
    /* Get computer choice */
    cpu_choice_index = ( rand() %3 ); //Random number [0,3]
    strcpy(cpu_choice, options[cpu_choice_index]);
    printf("%s\n", cpu_choice);
   
    /* Check who wins */
    if ( strcmp(cpu_choice, user_choice) == 0 )
    {
      printf("Tie\n");
      continue;
    }
    else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
    {
      printf("You won\n");
      continue;
    }
    else
    {
      printf("You lose\n");
      continue;
    }
  }
 
  return 0;
}
Title: Re: char[8] to char[8] giving error
Post by: calcdude84se on August 05, 2011, 11:58:26 am
I see a few problems:
In your for loop, you should be using strcmp, not ==
You should just be using printf("%s\n", options[cpu_choice_index]);. Don't do whatever mess you were trying with strcpy.
To check who wins, you should not be comparing strings, just use cpu_choice_index and user_choice_index.
Finally, those continue's are unnecessary.
Title: Re: char[8] to char[8] giving error
Post by: Munchor on August 05, 2011, 12:03:09 pm
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main();

int main() {
  char options[][9] = {"rocks", "paper", "scissors"}; //Possible options
  srand( time(NULL) ); //For random integer
 
  /* Define variables needed */
  int user_choice_index;
  char user_choice[9];
  int cpu_choice_index;
  int i;
 
  while (1) {
    /* Get user choice and index */
    fgets(user_choice, sizeof user_choice, stdin);
    for (i=0; i<3; i++)
    {
      if ( strcmp(options[i], user_choice) == 0 )
      {
        user_choice_index = i; //Index of user's choice
        break;
      }
    }
   
    /* Get computer choice */
    cpu_choice_index = ( rand() %3 ); //Random number [0,3]
    printf("%s\n", options[cpu_choice_index]);
   
    /* Check who wins */
    if ( user_choice_index == cpu_choice_index )
    {
      printf("Tie\n");
    }
    else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
    {
      printf("You won\n");
    }
    else
    {
      printf("You lose\n");
    }
  }
 
  return 0;
}

Made some changes calcdude, I think it's fixed :)
Title: Re: char[8] to char[8] giving error
Post by: calcdude84se on August 05, 2011, 12:09:02 pm
Excellent! :D
One of the more difficult things about C is remembering that there are no strings with pleasant operations like '==' like in Python, only char pointers and functions on them. C's '==' is very close to (if not the same as!) Python's 'is'
Title: Re: char[8] to char[8] giving error
Post by: Munchor on August 05, 2011, 12:20:45 pm
If I input scissors though:

Quote
scissors
scissors
Tie
rocks
You lose
Title: Re: char[8] to char[8] giving error
Post by: calcdude84se on August 05, 2011, 12:45:12 pm
"A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str."
That might be your problem. If it is, you'll need to make user_choice a 10-byte array: 8 for printing characters, one for the newline, and another for the null. That'll throw of your comparisons with strcmp, though, so you could also add a newline to each element of options. Then the printf("%s\n"... line won't need that \n.
Title: Re: char[8] to char[8] giving error
Post by: Ashbad on August 05, 2011, 02:52:00 pm
"A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str."
That might be your problem. If it is, you'll need to make user_choice a 10-byte array: 8 for printing characters, one for the newline, and another for the null. That'll throw of your comparisons with strcmp, though, so you could also add a newline to each element of options. Then the printf("%s\n"... line won't need that \n.

That could add up to multiple bytes uneedlessly wasted by including the newline :P but, since this isn't being optimized for a less-than-16-bit-address-spaced RAM space, I think it'll be quite fine.

And, to point out, == works the same as python's "is" in working, but it's not exactly the same since with C; it just compares the values of the two pointers, and Python's does a tiny bit more just to make sure the two instances are unique.  But yeah, to humans, its the same :P
Title: Re: char[8] to char[8] giving error
Post by: calcdude84se on August 05, 2011, 09:46:48 pm
Ashbad: it's the same instance if and only if it's at the same address, as far as I can tell. Note that small integers actually do act like C ints, so 5 is 5 returns True, just like 5 == 5 in C returns whatever's C's value for true is.
Title: Re: char[8] to char[8] giving error
Post by: Ashbad on August 05, 2011, 09:50:35 pm
Ashbad: it's the same instance if and only if it's at the same address, as far as I can tell. Note that small integers actually do act like C ints, so 5 is 5 returns True, just like 5 == 5 in C returns whatever's C's value for true is.

Hmm, perhaps I'm thinking of another language, though I was pretty sure python did more to check than just comparing two hashes, since there is some really minor undertakings to achieve attaining a hash, since objects in python aren't referred to as primitively as they are in lower level language (hash and offset :P).  Again, I might be thinking of another language.
Title: Re: char[8] to char[8] giving error
Post by: calcdude84se on August 05, 2011, 10:08:49 pm
Since when did an address become a hash? ???
Anyway, I think at the lowest level it's by address, since the following code produces an address:
Code: [Select]
class A:
    pass
print repr(A())
That, and pointers are also a special address type. Edit: not in Python, though, which, as we know, doesn't have pointers.