Why while loop is filling only the first condition? - c

while loop give result back when one of the condition succeed , i need both of them, any help
#include <stdio.h>
int yRechner()
{
int x = 2;
int y =0;
int i =0;
char answer[2];
while (i < 1 && y <= x)
{
printf("is the wall here pleas answer yes(Y/y) or No(N/n) \n");
scanf("%s", &answer);
if (answer[0] == 'N' || answer[0] == 'n')
{
printf("one step \n");
y++;
}
else if (answer[0] == 'Y' || answer[0] == 'y')
{
if (i < 1 && y > x)
{
i++;
printf("rotate \n");
}
else
{
printf("am not seeing the wall please say no. \n");
}
}
else
{
printf("wrong answer \n");
}
}
return y;
}
int main()
{
int y = yRechner();
printf("y = %d\n", y);
return 0;
}

It isn't apparent whether these conditions, i < 1 and y <= x, are negated or not. In general, if you want to loop while not both of the conditions are true, !(c1 && c2), you can use De Morgan's laws to simplify !c1 || !c2.

I found a Solution , thx for your hints
#include <stdio.h>
int yRechner()
{
int x = 2;
int y =0;
int i =0;
char answer[2];
while (i < 1 )
{
printf("is the wall here please answer yes(Y/y) or No(N/n) \n");
scanf("%s", &answer);
if (answer[0] == 'N' || answer[0] == 'n')
{
printf("one step \n");
y++;
}
else if (answer[0] == 'Y' || answer[0] == 'y')
{
if (i < 1 && y > x )
{
i++;
printf("rotate \n");
continue;
}
else if (i < 1 && y < x)
{
printf("am not seeing the wall please say no. \n");
continue;
}
}
else
{
printf("wrong answer \n");
}
}
return y;
}
int main()
{
int y = yRechner();
printf("y = %d\n", y);
return 0;
}

Related

How can I fix my problem about terminating the program instead of repeat it from the start using while loop? C Language

I have new problem about while-loop!
When I input the l_res as y it means that the condition of while loop is still TRUE, instead repeat it from the start, it terminated the program. → enter image description here
int main()
{
//VARIABLES
char f_res[4];
char l_res = 'y';
int a,b,c,x;
while(l_res == 'y')
{
printf("Enter 3 digit capacitor Code: ");
scanf("%d %d %d",&a,&b,&c);
x = (a*10)+b;
printf("What is the unit of the value?(Pico-picofarad, Nano-Nanofarad): ");
scanf("%s", f_res);
if(strcmp(f_res, "Pico") == 0)
{
if(c == 0)
{
x = x;
}
else
{
while(c <= 5 && c > 0)
{
c--;
x = x*10;
}
}
printf("\nCapacitor value is: %d pF\n\n",x);
}
else if(strcmp(f_res, "Nano") == 0)
{
float x = ((a*10)+b)*0.001;
if(c == 0)
{
x = x;
}
else
{
while(c <= 5 && c > 0)
{
c--;
x = x*10;
}
}
printf("\nCapacitor value is: %.3f pF\n\n",x);
}
else
{
printf("\nINVALID Input!\n\n");
}
printf("Do you want to enter other capacitor Code?( y-Yes, n-No): ");
scanf(" %c", l_res);
}
return 0;
}
How can I fix this?

How to Fix the Logic Errors in My "Guess the Movie" Game as a C Program

I have written a Guess the Movie game in the C programming language. My logic seems to be correct but whenever I run the program, it doesn't work as expected.
Here is my code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ran = 1;
int l, j = 0, i = 0, total = 0, d = 0;
char b;
char a[20];
char s[1000];
int z;
FILE *my;
printf("Enter your name:\n ");
scanf("%s", s);
ran = rand() % 6;
if (ran == 1)
{
my = fopen("my1.txt", "r");
}
else if (ran == 2)
{
my = fopen("my.txt", "r");
}
else if (ran == 3)
{
my = fopen("my2.txt", "r");
}
else if (ran == 4)
{
my = fopen("my3.txt", "r");
}
else if (ran == 5)
{
my = fopen("my4.txt", "r");
}
for (d = 0; d < 20; d++)
fscanf(my, "%c", &a[d]);
fclose(my);
printf("GUESS THE MOVIE GAME\n");
for (j = 0; j < 7; j++)
{
if (a[j] == 'm')
{
printf("M ");
}
else
{
printf("_ ");
}
}
printf("\n");
printf("Let's begin the game\n");
for (i = 0; i < 7;)
{
if (a[i] != 'm')
{
printf("enter character number %d\n",i+1);
scanf("%c", &b);
if (b == a[i])
{
printf("its a right guess\n");
total = total + 4;
i++;
}
else if (b != a[i])
{
printf("Wrong choice\n");
if (total == 1 || total == 0)
{
total=0;
}
else
{
total = total - 2;
}
}
}
}
printf("You have guessd the movie\n");
printf("The movie name is: ");
for (i = 0; i < 7; i++)
{
printf("%c",a[i]);
}
printf("Your score is %d\n",total);
}
This is the program output that I get each time I run the above code:
Enter your name:
raj
GUESS THE MOVIE GAME
_ _ _ _ M _ _
Let's begin the game
Enter character number 1
Wrong choice
Enter character number 1
I
Wrong choice
Enter character number 1
Wrong choice
Enter character number 1
Besides the deficiencies pointed out in comments, there's this major logic error:
for (i = 0; i < 7;)
{
if (a[i] != 'm')
{
…
}
}
If the loop encounters an m, it repeats endlessly. Eliminate the if (a[i] != 'm') or add an else ++i.

C: How to add a try again function to this asterisk square printing program

I need a program that asks for an integer &prints out an emtpy asterisk square based on the input number. And if the input was invalid then i need it to try and ask again
.
THIS is what i have so far (it already can make a square based on a variable number) i just need to enhence it so it asks again if the user is too dumb to input an integer
#include <stdio.h>
int Loops() {
int s, e, z;
printf("Input number \n");
scanf("%d", &s);
for(e = 0; e < s; e++) {
for(z = 0; z < s; z++) {
if(e==0 || e==s-1 || z==0 || z==s-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
int main() {
Loops();
}
i think the teacher mentioned something of adding a switch case...?
I propose this solution, in a few words I check the type of number entered by the user, if it's >0 then run,you do not have to worry that it is float because you have defined an integer type and the part not integer is automatically troncated.
#include <stdio.h>
int Loops() {
int s, e, z;
printf("Input number \n");
scanf("%d", &s);
if (s > 0 ) {
for (e = 0; e < s; e++) {
for (z = 0; z < s; z++) {
if(e == 0 || e == s - 1 || z == 0 || z == s - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
else
Loops();
}
int main() {
Loops();
return 0;
}
You can simply use an infinite loop i.e. while(1) loop with a condition to break. Let's say you want a user to input positive integer and discard negative numbers and alphabets as well, you can do that in the following manner:
int Loops() {
int s, e, z;
while(1)
{
printf("Input number \n");
e = scanf("%d", &s);
if((s <= 0) || (e != 1))
{
printf("Please Enter Non-negaitve Number\n");
getchar(); //This is to consume the '\n'
}
else
{
break;
}
}
for(e = 0; e < s; e++) {
for(z = 0; z < s; z++) {
if(e==0 || e==s-1 || z==0 || z==s-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}

I am getting a _\377 in my output

I have a school assignment to make a hangman game. The game works how I want it to except for one small glitch. If the user entered word is 4 letters or less, the hidden word is displayed with an extra "_\377" at the end. When the user entered word is 5 letters or more, then there is no glitch. I am hoping that someone would be kind enough to help me trouble shoot the problem. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int letterfinder(char string[], char a, int vari)
{
int length = strlen(string);
int i = vari;
int val = 0;
while( i <= length && val != 1)
{
if( string[i] == a)
{
val = 1;
}
i++;
}
if( val == 0)
{
return 100;
}
else
{
return i;
}
}
int main()
{
char inWord[] = "1111111111111111111111111111";
char outWord2[] = "1111111111111111111111111111";
char guess;
int gameover = 0;
int trys = 10;
int vari = 0;
printf("Please enter a word: ");
gets(inWord);
printf("%s\n", inWord);
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
int i2 = 0;
int j2 = 0;
int i3 = 0;
i2 = strcspn(inWord, outWord2);
char outWord[80];
while(i3 < i2)
{
outWord[i3] = '1';
i3++;
}
while(j2 < i2)
{
outWord[j2] = '-';
j2++;
}
puts(outWord);
while(gameover != 1 )
{
printf("What is your guess: ");
scanf("%s", &guess);
vari = 0;
if(letterfinder(inWord, guess, vari) == 100)
{
printf("Wrong!");
trys--;
printf("You have %d attempts left\n", trys);
if(trys == 0)
{
gameover = 1;
printf("You ran out of attempts. Game over\n");
}
}
else
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = (letterfinder(inWord, guess, vari));
while(letterfinder(inWord, guess, vari) != 100)
{
outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
vari = letterfinder(inWord, guess, vari);
}
puts(outWord);
}
int value = 0;
i3 = 0;
while( i3 <= i2)
{
if( outWord[i3] == '-')
{
value = 1;
}
i3++;
}
if(value != 1)
{
printf("Congratulations, you have guessed the word!\n");
gameover = 1;
}
}
return 0;
}
Your code has Undefined Behaviour. In the cases it "works" it is only by chance/luck. char guess; scanf("%s", &guess); That causes memory corruption as you are writing a string to a variable that can only hold a single char. Even a single letter guess will require two characters to store as all C strings are NUL terminated.
– kaylum

Connecting Random number in 2d array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have generated random number into a 2d array.What I want now is to connect the same number by moving it inside the game board.The problems is it doesn't move.
I really need help!!! I am new to c programming btw. Thanks in advance.
Here's my code:
void playgame(char box[ROW][COL])
{
int x, y, choice2,num,direction=0;
char input;
do{
printf("Please select a number (1-7) : \n");
scanf("%i",&num);
if(x==0 || x==ROW-1 ||y==0 || y==COL-1)
{
printf("Invalid!\n");
}
else
{
printf("Numer %i is currently selected!\n", num);
}
}while(x==0 || x==ROW-1 ||y==0 || y==COL-1);
printf("[1]Move\n[2]Sign out\n");
printf("Enter choice: ");
scanf("%d", &choice2);
switch(choice2)
{
case 1:
{
printf("Press 'E' to go up\n");
/*codes for moving the character up.....*/
}
{
printf("Press 'D' to go right\n");
/*codes for moving the character down.....*/
}
{
printf("Press 'S' to go left\n");
/*codes for moving the character left.....*/
}
{
printf("Press 'X' to go down\n");
/*codes for moving the character up.....*/
}
{
printf("Press 'R' to remove the existing path\n");
}
fflush(stdin);
scanf("%c", &input);
break;
case 2: printf("Bye!\n");
}
for(x=0; x<9; x++)
for(y=0; y<9; y++)
{
if(input == 'E')
if(box[x][y]==num)
{
box[--x][y]==num;
}
if(input == 'D')
if(box[x][y]==num)
{
box[x][y++]==num;
}
if(input == 'S')
if(box[x][y]== num)
{
box[x][--y]== num;
}
if(input == 'X')
if(box[x][y]==num)
{
box[--x][y]==num;
}
}
}
try this:
#include <stdio.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
void display(char box[ROW][COL]);
int validMove(int r, int c, char box[ROW][COL]);
void playgame(char box[ROW][COL]){
int r, c, num;
int choice, nr, nc;
char input, n;
for(;;){
printf("Please select a number (1-7) : \n");
if(1 == scanf("%i", &num) && 1<= num && num <= 7)
break;
printf("Invalid!\n");
while(getchar() != '\n');
}
printf("Numer %i is currently selected!\n", num);
n = num + '0';
for(r = 1; r < ROW-1; ++r)
for(c = 1; c < COL-1; ++c)
if(box[r][c] == n)
goto find;
find:
for(;;){
printf("[1]Move\n"
"[2]Sign out\n"
"Enter choice: ");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Press 'E' to go up\n"
"Press 'D' to go right\n"
"Press 'S' to go left\n"
"Press 'X' to go down\n"
"Press 'R' to remove the existing path\n");
scanf(" %c", &input);
nr = r;
nc = c;
switch(input){
case 'E':
nr = r - 1;
break;
case 'D':
nc = c + 1;
break;
case 'S':
nc = c - 1;
break;
case 'X':
nr = r + 1;
break;
case 'R':
break;
default:
break;
}
if(validMove(nr, nc, box)){
box[nr][nc] = box[r][c];
box[r][c] = ' ';
r = nr;
c = nc;
display(box);
} else if(input != 'R'){
printf("invalid move!\n");
}
break;
case 2: printf("Bye!\n");
exit(0);
}
}
}
int validMove(int r, int c, char box[ROW][COL]){
if(r == 0 || r == ROW-1 || c == 0 || c == COL-1)
return 0;
if(box[r][c] != ' ')
return 0;
return 1;
}
void initBoard(char box[ROW][COL]){
int r, c;
for(r = 0; r < ROW; ++r){
for(c = 0; c < COL; ++c){
if(r == 0 || r == ROW-1 || c == 0 || c == COL-1)
box[r][c] = '#';
else
box[r][c] = ' ';
}
}
for(int i = 1; i <= 7; ++i){
int r = rand() % (ROW-1-1) + 1;
int c = rand() % (COL-1-1) + 1;
if(box[r][c] == ' ')
box[r][c] = i + '0';
else
--i;
}
}
void display(char box[ROW][COL]){
for(int r = 0; r < ROW; ++r){
for(int c = 0; c < COL; ++c){
putchar(box[r][c]);
}
putchar('\n');
}
}
int main(void){
char board[ROW][COL];
initBoard(board);
display(board);
playgame(board);
return 0;
}

Resources