OrAnGeWorX
Member • Dec 13, 2008
Character input question
Hey guys, I was wondering if there was a way to scanf/getchar() a string of characters into a table ?
Am i shooting for the stars or is it a possibility ?
what I'm trying to do is basically have the user input 4 characters, that's pretty much all i need but when the user hits enter, the characters would be assigned to elements of a table...
it should be doable i guess...
it's part of a mastermind game I'm creating. the game is pretty much OK except for that question i ask.
In the program, the user has 6 attempts at cracking a 4 color randomly picked code. normal way of running would be, user inputs 4 characters for red green blue and yellow (r, v, b, j - ie: rrbj - repetitions allowed)
when user hits enter, program prints out the numbers of hit&miss colors... if any of course and will keep prompting to enter a new code till either the 6 retries are over or if the user guessed the code..
My code is pretty much fine except for when a user enters a code... he either has to do each character at a time or enter the same code 4 times... which is not satisfactory.
The program needs to read the 4 character string and put it in a table with a simple for loop.
Here's my code
Am i shooting for the stars or is it a possibility ?
what I'm trying to do is basically have the user input 4 characters, that's pretty much all i need but when the user hits enter, the characters would be assigned to elements of a table...
it should be doable i guess...
it's part of a mastermind game I'm creating. the game is pretty much OK except for that question i ask.
In the program, the user has 6 attempts at cracking a 4 color randomly picked code. normal way of running would be, user inputs 4 characters for red green blue and yellow (r, v, b, j - ie: rrbj - repetitions allowed)
when user hits enter, program prints out the numbers of hit&miss colors... if any of course and will keep prompting to enter a new code till either the 6 retries are over or if the user guessed the code..
My code is pretty much fine except for when a user enters a code... he either has to do each character at a time or enter the same code 4 times... which is not satisfactory.
The program needs to read the 4 character string and put it in a table with a simple for loop.
Here's my code
#includeThanks for any insight... this is puzzling me quite much#include #define NBCOLORS 4 #define NBTRIES 6 char readkey(); void createCode(char code[]); int playGame(char code[]); int main (void) { char secretCode[NBCOLORS]; createCode(secretCode); while(playGame(secretCode)); return 0; } [B]// Character input/storage char readkey() { fflush(stdin); char c = '\n'; while (c == '\n' || c == ' ') c = getchar(); return c; }[/B] void createCode(char code[]) { int i; srand(1810); /* create random code */ for (i = 0; i < NBCOLORS; i++) switch(rand() % 4) { case 0: code[i] = 'r'; break; case 1: code[i] = 'b'; break; case 2: code[i] = 'v'; break; case 3: code[i] = 'j'; break; } } int playGame(char code[]) { char codeCopy[NBCOLORS]; int victory = 0; int i, j, k; char c = 0; /* character being read */ for (i = 0; i < NBTRIES; i++) { int identical = 0; int present = 0; for (j = 0; j < NBCOLORS; j++) codeCopy[j] = code[j]; printf("Votre coup (essai no %i): ", i+1); for (j = 0; j < NBCOLORS; j++) { c = readkey(); if (codeCopy[j] == c) { codeCopy[j] = '#'; identical++; } else { for(k = 0; k < NBCOLORS; k++) if (codeCopy[k] == c) { codeCopy[k] = '#'; present++; } } } printf("Couleurs identiques: %i\n", identical); printf("Coleurs presentes: %i\n", present); if(identical == NBCOLORS) { printf("Bravo! Vous avez trouve notre sequence de couleurs\n"); victory=1; break; } } if(!victory) { printf("Desole, la sequence de couleurs a deviner etait "); for (j = 0; j < NBCOLORS; j++) putchar(code[j]); putchar('\n'); } while (c != 'o' && c != 'O' && c != 'n' && c != 'N') { fflush(stdin); printf("Voulez-vous jouer une autre partie? (O/N)?"); c = readkey(); } if (c == 'o' || c == 'O') return 1; return 0; }