简体   繁体   中英

Add a 1 if user entered an “*”, or add a 0 if user entered a “ ” to an array in C

I need to take the users input (asterisk or a space) and if the user entered an asterisk I would then insert a 1 into an array, if the user entered a space I would then insert a 0 into an array. Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>

void userInput(){
    printf("Enter Generation 0 (Length must be 10, * for 1 and " " for 0):\n");
    char c;                 /* int */
    int count;
    char arr[10];
    for(int i = 0; i < 10; i++){
        scanf("%c", &c);
        if(c == '*')
            arr[i] = '1';
        else
            arr[i] = '0';
    }
    for(int j = 0; j < 10; j++){
        printf("%c", arr[j]);
    }
}

int main(int argc, const char * argv[]) {
    userInput();
}

The problem is that the loop only allows the user to enter a character 6 times and then prints.

Output if I enter all *'s:

Enter Generation 0 (Length must be 10, * for 1 and  for 0):
*
*
*
*
*
1010101010

Output if I enter all spaces:

Enter Generation 0 (Length must be 80, * for 1 and  for 0):





0000000000

There are any number of ways to approach capturing the '*' and ' ' and translating them to 1 and 0 , respectively. You are essentially writing a little input handler that will read until your array is full, or the user terminates input with [ctrl + d] . You simply need to check to insure the array is not full and that you haven't reached EOF . Something simple will do:

#include <stdio.h>

#define MAXC 10

int main (void) {

    unsigned char arr[MAXC] = {0};
    int c = 0, i, idx = 0;

    while (idx < MAXC && (c = getchar()) != EOF) {
        if (c == '*') arr[idx++] = 1u;
        if (c == ' ') arr[idx++] = 0u;
    }

    for (i = 0; i < idx; i++)
        printf (" arr[%d] : %u\n", i, arr[i]);

    return 0;
}

It will take any form of input, act appropriately for '*' and ' ' , and ignore an additional characters until the array is full or EOF is generated:

Example Use/Output

$ ./bin/rdspcasterix
(**) (**)
  (**)
*
 arr[0] : 1
 arr[1] : 1
 arr[2] : 0
 arr[3] : 1
 arr[4] : 1
 arr[5] : 0
 arr[6] : 0
 arr[7] : 1
 arr[8] : 1
 arr[9] : 1

$ ./bin/rdspcasterix
The quick*brown fox*jumps over*a lazy*dog
**  **
 arr[0] : 0
 arr[1] : 1
 arr[2] : 0
 arr[3] : 1
 arr[4] : 0
 arr[5] : 1
 arr[6] : 0
 arr[7] : 1
 arr[8] : 1
 arr[9] : 1

There is no one-right way to do this, so take a look at all answers and choose the one that works best for your situation.

You could also use fgetc with STDIN as your file stream to read char by char

EDIT Added a solution with buffer validation

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define EXTRACHARS 2
#define SIZE 10

static void clear_buf(void)
{
  int ch;
  while(ch = getc(stdin), ch != '\n' && ch!=EOF);
  clearerr(stdin);
}
int main(void)
{
  int c = 0; 
  char seq[SIZE + EXTRACHARS];

  printf("Enter 10 * or ' ': ");  
  fgets(seq, SIZE+EXTRACHARS, stdin);
  if (seq[strlen(seq)- 1] != '\n')
    {
      printf("Buffer overflowed \n");
      clear_buf();
    }
  else
    {
      while(c < SIZE)
    {
      if(seq[c] == '*')
        {
          printf("1");
        }
      else if(seq[c] == ' ')
        {
          printf("0");
        }
      ++c;
    }
      printf("\n");
    }
  return EXIT_SUCCESS;
}

ok new code work real well for me tested on QT Ide Mingw Compiler

#include <stdio.h>
#include <stdlib.h>

void userInput(){
    printf("Enter Generation 0 (Length must be 10, * for 1 and " " for 0):\n");
    char c;
    char temp;
    char arr[10];
    int i=0;
    do{
        printf("\nEnter number %d : ", i+1);
        c = getchar();
        while((temp = getchar()) != '\n' && c != EOF);    // this is alternative to fflush
        if(c == 42)     // 42 is ascii code to *
            arr[i] = '1';
        else
            arr[i] = '0';

        i++;
    }while((c!=EOF)&&(i<10));
    for(int j = 0; j < 10; j++){
        printf("%c\n", arr[j]);
    }
}

int main(int argc, const char * argv[]) {
    userInput();
}

this may not work on online compilers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM