Thread: Assign constant char to another char variable

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    15

    Assign constant char to another char variable

    Hi all,
    I'm having trouble with my project (connect 4). At the beginning of the program I define 2 constants char that represents black checker and red checker as follows:

    #define RED 'X'
    #define BLACK 'O'

    I have a menu() function that prints out welcome to the game, stuff like that. I then proceed to ask the user which color he would like to be, then assign it to one of the constant char (RED or BLACK) appropriately as follows:
    Code:
    int choice;
    char player;
    char com;
    scanf("so you wan to be... %d \n", &choice);
    	if (choice == 1) {
    		player = RED;
    		com = BLACK;
    		
    	}
    	
    	else if (choice == 2) {
    		player = BLACK;
    		com = RED;
    		
    	}
    I then prints out char player and char com to confirm that I have assigned the values correctly:
    printf("the player chose to be: %c\n", player);
    but this only prints out "the player chose to be: "
    what am I doing wrong here ? Any suggestion would be greatly appreciated. Thanks in advance everyone !

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    scanf("so you wan to be... %d \n", &choice);
    
    
    should be more like
    
    
    puts("so you wan to be...");
    scanf("%d%*c", &choice); // (%*c for when the user hits enter)
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    Thank you for the prompt reply, but can you please elaborate a bit (I'm very new to C) ? So %*c is a pointer right ?

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    In the context of scanf - When you put a '*' after the %, it will read the input and do nothing with it

    i.e.
    Code:
    scanf("%d%*c", &choice);
    %d - This would read the first input part as a number and put it into choice
    %*c - This would read a character and then do nothing with it.

    Here is a full list of all the things you can do with scanf (for windows)
    - Format Specification Fields: scanf and wscanf Functions (CRT)
    - scanf Width Specification (CRT)
    - scanf Type Field Characters (CRT)
    Fact - Beethoven wrote his first symphony in C

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Nope. The stuff that appears between quotes as the first parameter of scanf is a mini-language specific to scanf, so the asterisk is not related to pointers at all.

    %c means to read a character and assign it to a given address.
    %*c means to read a character and then just throw it away (ignore it).
    The * can be used with any of the specifiers to indicate that the data is to be read and then thrown away.

    So, e.g.,
    scanf("%d%*d%d", &n, &m);
    Reads an integer and assignes it to n,
    read another integer and ignores it,
    reads another integer and assignes it to m.

    In the case at hand, click_here has used %*c in an attempt to "eat" the newline so it won't be left in the stream. It's not foolproof (someone might type an extra space before the newline), but it usually works.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-03-2012, 01:23 AM
  2. assign a string to char pointer
    By seaking1 in forum C Programming
    Replies: 8
    Last Post: 12-08-2009, 02:54 PM
  3. How to assign ';' to a char?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2008, 03:08 AM
  4. Can't Return/Assign Char Pointer
    By SiliconHobo in forum C Programming
    Replies: 21
    Last Post: 12-16-2007, 10:56 AM
  5. why can't you assign a char to a char?
    By yahn in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2005, 11:31 AM