Author Topic: char[8] to char[8] giving error  (Read 5935 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
char[8] to char[8] giving error
« 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
« Last Edit: August 05, 2011, 10:20:55 am by ephan »

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: char[8] to char[8] giving error
« Reply #1 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];

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: char[8] to char[8] giving error
« Reply #2 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.

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: char[8] to char[8] giving error
« Reply #3 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

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: char[8] to char[8] giving error
« Reply #4 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;
}

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: char[8] to char[8] giving error
« Reply #5 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.
« Last Edit: August 05, 2011, 11:58:57 am by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: char[8] to char[8] giving error
« Reply #6 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 :)

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: char[8] to char[8] giving error
« Reply #7 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'
« Last Edit: August 05, 2011, 12:09:13 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: char[8] to char[8] giving error
« Reply #8 on: August 05, 2011, 12:20:45 pm »
If I input scissors though:

Quote
scissors
scissors
Tie
rocks
You lose

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: char[8] to char[8] giving error
« Reply #9 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.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Ashbad

  • Guest
Re: char[8] to char[8] giving error
« Reply #10 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
« Last Edit: August 05, 2011, 02:52:42 pm by Ashbad »

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: char[8] to char[8] giving error
« Reply #11 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.
« Last Edit: August 05, 2011, 09:47:05 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Ashbad

  • Guest
Re: char[8] to char[8] giving error
« Reply #12 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.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: char[8] to char[8] giving error
« Reply #13 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.
« Last Edit: August 06, 2011, 10:32:35 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.