Certain statements printing twice - c

In a hangman program I'm writing, words with two of the same letter, such as "eel" or "bee," upon the user entering "e" the first time and then trying to enter "e" for the second prompt, will display "You've already guessed this letter" twice. How can I fix this?
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
int main(){
char w[13][3] = {
{ 'c', 'a', 't' }, //0
{ 'd', 'o', 'g' }, //1
{ 'r', 'a', 't' }, //2
{ 'e', 'e', 'l' }, //3
{ 'c', 'o', 'w' }, //4
{ 'o', 'w', 'l' }, //5
{ 'e', 'm', 'u' }, //6
{ 'b', 'a', 't' }, //7
{ 'e', 'l', 'k' }, //8
{ 'p', 'i', 'g' }, //9
{ 'b', 'e', 'e' }, //10
{ 'h', 'e', 'n' }, //11
{ 'f', 'o', 'x' }, //12
};
char u,
newline,
dis[16];
int random,
guesses = 3,
finish = 0;
_Bool successfulGuess = false;
srand(time(NULL));
random = rand() % 13;
printf("Animal %d\n", random); //check random number
printf("---------\n\n");
printf("Enter a letter: ");
u = getchar();
newline = getchar();
for (int i = 0; i < 3; i++){
if (w[random][i] == u){
successfulGuess = true;
dis[i] = u;
}
else {
dis[i] = '_';
}
}
for (int j = 0; j < 3; j++){
dis[j] = dis[j];
}
printf("\n");
for (int i = 0; i < 3; i++){
printf("%c", dis[i]);
}
if (successfulGuess == false){
--guesses;
}
printf("\n\nGuesses left: %d", guesses);
printf("\n\n");
while (guesses > 0){
finish = 0;
successfulGuess = false;
printf("Enter a letter: ");
u = getchar();
newline = getchar();
for (int i = 0; i < 3; i++){
if (u == dis[i]){
successfulGuess = true;
printf("\nYou already guessed this letter.\n");
printf("\ninput = dis[i]\nGuesses left: %d\n\n", guesses);
}
else if (w[random][i] == u){
successfulGuess = true;
dis[i] = u;
printf("\ninput = array char\nGuesses left: %d\n\n", guesses);
}
}
for (int i = 0; i < 3; i++){
printf("%c", dis[i]);
}
if (successfulGuess == false){
guesses--;
printf("\n\nbool statement\nGuesses left: %d\n\n", guesses);
}
if (guesses == 0){
printf("Sorry, you've run out of guesses.");
}
for (int i = 0; i < 3; i++) {
if (dis[i] != '_') {
finish++;
}
if (finish == 3){
printf("\n\nYou guessed the word!");
guesses = 0;
}
else{
continue;
}
}
printf("\n\n");
}
system("pause");
}

You are looping i three times even if you have already shown the message. I think it should break ather the message:
for (int i = 0; i < 3; i++){
if (u == dis[i]){
successfulGuess = true;
printf("\nYou already guessed this letter.\n");
printf("\ninput = dis[i]\nGuesses left: %d\n\n", guesses);
/* BREAK HERE */
break;
}

Related

Tic Tac Toe game

I was working on learning c code and was making a tic-tac-toe game. The Boolean issue was fixed. Now the issue is that it is looping the printf("There is no empty space!"); and prinf("Invalid !!!"); after it take the player1 name. I also wanted to know if the line where I printed the array with the grid is correct or not.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
char space[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
int row;
int column;
char token = 'x';
bool tie = false;
char n1[256];
char n2[256];
void functionboard()
{
char space[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
printf(" | | \n");
printf(" ", space[0][0], "| ", space[0][1], "| ", space[0][2], " \n");
printf("______|________|_____\n");
printf(" | | \n");
printf(" ", space[1][0], " | ", space[1][1], " | ", space[1][2], " \n");
printf("______|________|_____\n");
printf(" | | \n");
printf(" ", space[2][0], " | ", space[2][1], " | ", space[2][2], " \n");
printf(" | | \n");
}
void functionOne()
{
int dight;
if (token == 'x')
{
printf(n1, "please enter");
scanf("&d", &dight);
}
if (token == '0')
{
printf(n2, "please enter");
scanf("&d", &dight);
}
if (dight == 1)
{
row = 0;
column = 0;
}
if (dight == 2)
{
row = 0;
column = 1;
}
if (dight == 3)
{
row = 0;
column = 2;
}
if (dight == 4)
{
row = 1;
column = 0;
}
if (dight == 5)
{
row = 1;
column = 1;
}
if (dight == 6)
{
row = 1;
column = 2;
}
if (dight == 7)
{
row = 2;
column = 0;
}
if (dight == 8)
{
row = 2;
column = 1;
}
if (dight == 9)
{
row = 2;
column = 2;
}
else if (dight < 1 || dight > 9)
{
prinf("Invalid !!!");
}
if (token == 'x' && space[row][column] != 'x' && space[row][column] != '0')
{
space[row][column] = 'x';
token = '0';
}
else if (token == '0' && space[row][column] != 'x' && space[row][column] != '0')
{
space[row][column] = '0';
token = 'x';
}
else
{
printf("There is no empty space!");
functionboard();
}
functionOne();
}
bool functionDraw()
{
for (int i = 0; i < 3; i++)
{
if (space[i][0] == space[i][1] && space[i][0] == space[i][2] || space[0][i] == space[1][i] && space[0][i] == space[2][i])
return true;
}
if (space[0][0] == space[1][1] && space[1][1] == space[2][2] || space[0][2] == space[1][1] && space[1][1] == space[2][0])
{
return true;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (space[i][j] != 'x' && space[i][j] != '0')
{
return false;
}
}
}
tie = true;
return false;
}
int main()
{
printf("Enter the name of the first player : \n");
scanf("%c", n1);
printf("Enter the name of the second player : \n");
scanf("%c", n2);
printf("%c is player1 so he/she will play first \n", n1);
printf("%c is player2 so he/she will play first \n", n2);
while (!functionDraw())
{
functionboard();
functionOne();
functionDraw();
}
if (token == 'x' && tie == false)
{
printf("%c Wins!!\n", n2);
}
else if (token == '0' && tie == false)
{
printf("%c Wins!!\n", n1);
}
else
{
printf("its a draw!!");
}
}
If the error you're getting is about the type bool and not the variable itself, see this question: you need to also include <stdbool.h> to be able to use the bool type.
There's no built-in bool type in classic C. You can use it though by using #include <stdbool.h>. The other solution is replacing it with an int since it can be used like a bool

Can't get my C program to print the output

When I go to print the output of the program everything shows up as zero. I think the variable aren't storing themselves, but I'm not totally sure. When I go to look over everything, it looks right but clearly isn't. Any help would be really appreciated. Sorry if the formatting seems a little off, Stack Overflow wouldn't accept it otherwise.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int digit(char term[])
{
int i = 0;
int val = 0;
while (term[i] != '\0')
{
val = val * 10 + term[i] - '0';
}
i++;
return val;
}
void error()
{
printf("Error: Sales figures must be numbers.\n");
printf("Please try again.\n");
}
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if( isdigit(term[i]) == 0)
{
return false;
i++;
}
}
return true;
}
int main()
{
int sales[3][2], costs[3] = {3, 4, 1}, weekends[2] = {0, 0};
int i, j, val;
char term[100];
while (1)
{
printf("Number of Bagel sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[0][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[1][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Saturday: ");
scanf("%s", term);
if (isnumber(term) == false)
{
error();
}
else
{
sales[2][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Bagel sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[0][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[1][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[2][1] = digit(term);
break;
}
}
for (i = 0; i < 2, i++;)
{
for (j = 0; j < 3, j++;)
{
weekends[i] += costs[j] * sales[i][j];
}
}
printf("\n");
for (i = 0; i < 3, i++;)
{
printf("%d", costs[i]);
}
printf(".");
for (i = 0; i < 3, i++;)
{
for (j = 0; j < 2, j++;)
{
printf("%d", sales[i][j]);
}
if (i == 0)
{
printf(" = ");
printf("%d %d", weekends[0], weekends[1]);
}
printf("\n ");
}
printf("\nTotal sales on Saturday: $%d", weekends[0]);
printf("\nTotal sales on Sunday: $%d", weekends[1]);
printf("\nTotal sales over the weekend: $%d", weekends[0] + weekends[1]);
return 0;
}
You are not incrementing i in the loop. Your code for digit is:
int digit(char term[])
{
int i = 0;
int val = 0;
while (term[i] != '\0')
{
val = val * 10 + term[i] - '0';
}
i++; /* ---- this is outside the loop !! */
return val;
}
But it ought to look like:
int
digit(const char *term)
{
int val = 0;
while( *term != '\0' ){
val = val * 10 + *term - '0';
term += 1;
}
return val;
}

C programming, only the first if statement works

I was wondering if anyone could help me understand why only my first if statements is working. Basically, I am working on a l33t speak convertor (lol) and only my first if statement works, here is my code:
#include<stdio.h>
#include<string.h>
void translate (char blurp[]);
int main(void) {
char message[1024];
printf("enter a message: \n");
fgets(message, 1024, stdin);
translate(message);
return 0;
}
void translate (char blurp[]) {
int i;
int length;
length = strlen(blurp);
printf("\nHere it is translated: \n");
for ( i = 0; i != length; i++) {
if (blurp[i] == 'a') {
blurp[i] = '4';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'b') {
blurp[i] = '8';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'e') {
blurp[i] = '3';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'i') {
blurp[i] = '|';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'o') {
blurp[i] = '0';
printf("%c", blurp[i]);
}
else if (blurp[i] == 's') {
blurp[i] = '5';
printf("%c", blurp[i]);
}
else {
printf("%c", blurp[i]);
}
}
}

Function and loop trouble in C

This program is expecting the user to input a 7-digit number (except 1 and 0), and any digit has a corresponding set of letters
(2=ABC, 3=DEF, 4=GHI, 5=JKL, 6=MNO, 7=PRS, 8=TUV, 9=XYZ, as found on phones in the USA). Finally, it should output all 2187 possible sequences of letters.
ex: input 2345678
output should be ADGJMPT ADGJMPU ADGJMPV ADGJMRT ADGJMRU ADGJMRV ADGJMST..........CFILOSV
but my output always is AAAAAAA AAAAAAB AAAAAAC..........CCCCCCC
(I also have trouble in checking number. I first set a loop and array, if (che[1] != 1 && che[0] != 1) break; but sometimes it doesn't break.)
Can you explain what's wrong?
#include<stdio.h>
int main(){
int che[50] = { 0 };
int a, b, c, d, e, f, g, i, r, q, number, check;
char word2[7];
char word1[8][3] = {
{ 'A', 'B', 'C' },
{ 'D', 'E', 'F' },
{ 'G', 'H', 'I' },
{ 'J', 'K', 'L' },
{ 'M', 'N', 'O' },
{ 'P', 'R', 'S' },
{ 'T', 'U', 'V' },
{ 'W', 'X', 'Y' } };
while (1)
{
printf("Enter seven digit(except 0 and 1):");
scanf("%d", &number);
check = number;
for (; number != 0; number /= 10)
{
q = number % 10;
che[q] = 1;
}
if (che[1] != 1 && che[0] != 1) break;
}
number = check;
for (i = 6; number == 0; i--)
{
r = number % 10;
if (r == 2){ word2[i] = 0; }
if (r == 3){ word2[i] = 1; }
if (r == 4){ word2[i] = 2; }
if (r == 5){ word2[i] = 3; }
if (r == 6){ word2[i] = 4; }
if (r == 7){ word2[i] = 5; }
if (r == 8){ word2[i] = 6; }
if (r == 9){ word2[i] = 7; }
number /= 10;
}
for (a = 0; a < 3; a++){
for (b = 0; b < 3; b++){
for (c = 0; c < 3; c++){
for (d = 0; d < 3; d++){
for (e = 0; e < 3; e++){
for (f = 0; f < 3; f++){
for (g = 0; g < 3; g++){
printf("%c%c%c%c%c%c%c ",word1[word2[0]][a], word1[word2[1]][b], word1[word2[2]][c], word1[word2[3]][d], word1[word2[4]][e], word1[word2[5]][f], word1[word2[6]][g]);
}
}
}
}
}
}
}
return 0;
}
Main problem is here:
for (i = 6; number == 0; i--)
The loop condition is the opposite of the one it should be. You want to keep iterating on the number until you reach 0 (by successively dividing it by 10).
It should be
for (i = 6; number != 0; i--)
or
for (i = 6; i >= 0; i--)
In addition mind that
if (r == 2){ word2[i] = 0; }
if (r == 3){ word2[i] = 1; }
if (r == 4){ word2[i] = 2; }
if (r == 5){ word2[i] = 3; }
if (r == 6){ word2[i] = 4; }
if (r == 7){ word2[i] = 5; }
if (r == 8){ word2[i] = 6; }
if (r == 9){ word2[i] = 7; }
is equivalent to
if (r >= 2 && r <= 9)
word2[i] = r - 2;
I think this works as you expected:
For some convenience in debugging, I changed some IO format:
Input separated number 2-9 divided by space and end with -1.
The input sequence can be of any length, just end with -1.
For example, input:2 3 4 2 -1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char word1[8][3] = {
{ 'A', 'B', 'C' },
{ 'D', 'E', 'F' },
{ 'G', 'H', 'I' },
{ 'J', 'K', 'L' },
{ 'M', 'N', 'O' },
{ 'P', 'R', 'S' },
{ 'T', 'U', 'V' },
{ 'W', 'X', 'Y' } };
void translate(char * headStr,int * pattern,int pos_to_do)
{
if(pattern[pos_to_do]<0)
{
printf("%s\n",headStr);
return;
}
char str[3][20];
int i;
for(i=0;i<3;i++)
{
strcpy(str[i],headStr);
char str_this[2];
str_this[0]=word1[ pattern[pos_to_do] ][i];
str_this[1]='\0';
strcat(str[i],str_this);
translate(str[i],pattern,pos_to_do+1);
}
return;
}
int main(){
int che[20];
int number, check,len;
while (1)
{
printf("Enter digits(except 0 and 1):");
len=0;
scanf(" %d", &number);
while(number>=2)
{
che[len]=number-2;
len++;
scanf("%d", &number);
}
che[len]=-1;
char str_start[]="";
translate(str_start,che,0);
}
return 0;
}
Test output:
Enter digits(except 0 and 1):2 3 4 -1
ADG
ADH
ADI
AEG
AEH
AEI
AFG
AFH
AFI
BDG
BDH
BDI
BEG
BEH
BEI
BFG
BFH
BFI
CDG
CDH
CDI
CEG
CEH
CEI
CFG
CFH
CFI
Enter digits(except 0 and 1):

How to priority sort characters?

I edit my question. I wrote my functions. But there is some mistakes in output .How can I fix it ?
void enqueue(char type,int p){
if (que == NULL){
que =malloc(sizeof(struct node));
que->nextPtr = NULL;
que->pages=p;
que->userType=type;
return;
}
q = prev = que;
while((q->nextPtr != NULL) && charcmp(q->userType,type)<0){
prev = q;
q = q->nextPtr;
}
if(charcmp(q->userType,type)==0){
while(q->nextPtr !=NULL && q->pages < p){
prev = q;
q = q->nextPtr;
}
}
n = malloc(sizeof(struct node));
n->pages=p;
n->userType=type;
if ( (charcmp(q->userType,type) < 0 || charcmp(q->userType,type)==0 ) && q->pages < p){
n->nextPtr = q->nextPtr;
q->nextPtr = n;
}
if((q->pages >= p )){
n->nextPtr = q;
if (prev->nextPtr == q){
prev->nextPtr = n;
}
else if (que == q)
{
que = n;
}
}
}
and this is my charcmp function .
int charcmp(char a,char b){
while(a==b){
return 0;
}
while(a!=b){
if(a=='C' && (b=='G'|| b=='A')){
return -1;
}
if(a=='G' && (b=='A')){
return -1;
}
if(a=='A' && (b=='C' || b=='G')){
return 1;
}
if(a=='G' && b=='C'){
return 1;
}
}
}
My output has some mistakes. I can't fix it.For example its return C,C,A,C,G
So, you want to sort an array of custom objects, like this:
typedef struct {
char c; // a character
int i; // a number
} pair;
Then you can apply a standard function qsort() with a custom compare function like this:
int compare(const void *a, const void *b) {
pair *p1 = (pair*) a;
pair *p2 = (pair*) b;
if (p1->c == p2->c) return p1->i - p2->i;
if (p1->c == 'C' || p1->c == 'G' && p2->c == 'A') return -1;
return 1;
}
So that this testing code with your test data will print C,1 C,8 G,2 G,5 A,4 A,7 A,10
int main() {
pair input[] = {{'A', 10}, {'A', 7}, {'G', 5},
{'C', 8}, {'A', 4}, {'C', 1}, {'G', 2}};
qsort(input, 7, sizeof(pair), compare);
pair *cur = input;
for (int i=0; i<7; ++i, ++cur) {
printf("%c,%d ", cur->c, cur->i);
}
printf("\n");
}
How to transform arbitrary input into array of structures, is an exercise.
#include <stdio.h>
int main(){
char inputChar[] = {'A', 'C', 'G'};
char bufferChar[3] = {'\0', '\0', '\0'};
for(int i = 0; i < 3; i++)
{
if(inputChar[i] == 'C')
bufferChar[0] = inputChar[i];
else if(inputChar[i] == 'G')
bufferChar[1] = inputChar[i];
else
bufferChar[2] = inputChar[i];
}
for(int i = 0; i < 3; i++)
{
printf("%c ", bufferChar[i]);
}
printf("\n");
return 0;
}
I hope this is what you are looking for. Try fiddle with the inputChar you would still get C G A

Resources