C program: if statement within switch statement [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to create a program that will switch between "modes". For example here are little snippet of the code:
int main()
{
int mode,input;
mode = 1;
for(;;)
{
scanf("%d", &input);
switch(input)
case 1:
if(mode = 1)
{
//statements go here;
mode = 2;
}
else
{
//statements go here;
mode = 1;
}
break;
}
}
So what I'm trying to do is get the program to switch between mode 1 and mode 2 by the input of the 1 button. However each time I press the number 1 key, it will only print the statements of mode 1 but won't switch to mode 2 and print out the statements for mode 2 if i press the number 1 button a second time. Is there something fundamentally wrong with my code?
*restriction: I must use the switch statements in the program.

To test the value of a variable you need double equals:
if(mode == 1)

Related

Why does this show me an error bad for loop on my pi 4 (begginer, first timer with normal c) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
#include <wiringPi.h>
#include <stdio.h>
#define ledPin 0
main()
{
wiringPiSetup()
int x;
for(x=0; x<4; x+1)
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
the error is on line 7 and i've been stuck on it for 2 days (i code in geany)
It sounds like you're getting a compile error on line 8:
wiringPiSetup() /* <-- You need to end the line with ";" */
Your loop should look like this:
for(x=0; x<4; x++) {...} /* "x+1" doesn't change the value of "x", so the loop will never terminate */
The problem is in your for loop.
for(x=0; x<4; x+1)
The third parameter in brackets does not do anything. You probably wanted to increment x and you will do that with x++ or x+=1.
x+1 will return the value, but that will not be stored anywhere.

Why does the computer ignore my program? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include <stdio.h>
#include <conio.h>
void positive(int input, int t);
void negative(int input, int t);
void main(){
int input,t;
clrscr();
textcolor(YELLOW);
cprintf("ENTER YOUR NUMBER: ");
scanf("%i",&input);
cprintf("\nNUMBER IN WORD(S): ");
if (input < -9999 && input > 9999) // <------ THIS GUY
printf("INVALID ENTRY, PROGRAM TERMINATED"); // <-------- AND ALSO THIS THING
if(input == 0)
printf("Zero");
if(input < 0 && input >-10000){
negative(input,t);
}
if(input > 0 && input <10000){
positive(input,t);
}
getch();
}
This is the main function of my code, it has other functions but whenever a user types an integer either less that -9999 and greater that 9999 the print command is not appearing on the screen but the program is up and running it just ignores the code.
EXAMPLE:
Enter a Number: 123151 Your number in Word:
no answer.
What to fix here?
You need to replace following 'if' condition
if (input < -9999 && input > 9999)
with
if (input < -9999 || input > 9999)

scanf() causing segmentation fault when previously working fine [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm currently programming a version of Conway's Game of Life in C as part of an assignment for my degree course. Within the code, I ask the user to enter an integer representing a menu item describing the game's initial conditions.
When I tested the code surrounding this on it's own, it worked fine, scanning in the values correctly and printing them out fine etc.
However, I have now continued with my code and begun developing the next stage and now suddenly I am getting a segmentation fault which, using printf, I pinpointed back to this very same previously working scanf statement.
Is anyone able to point me in the direction of why this scanf is suddenly giving such a fault, so I can hence address the problem.
My code is as follows:
#include <stdio.h>
#include <math.h>
#include <string.h>
#define WIDTH 60
#define HEIGHT 60
#define NAMELENGTH 128
void initGrid(int choice, int grid[][WIDTH]){
int a,b;
for(a=0;a<HEIGHT;++a){
for(b=0;a<WIDTH;++b){
grid[a][b]=0;
}
}
switch(choice){
case 1 :
grid[6][3]=1;
grid[7][3]=1;
grid[6][4]=1;
grid[7][4]=1;
grid[6][13]=1;
grid[7][13]=1;
grid[8][13]=1;
grid[5][14]=1;
grid[4][15]=1;
grid[4][16]=1;
grid[9][14]=1;
grid[10][15]=1;
grid[10][16]=1;
grid[7][17]=1;
grid[5][18]=1;
grid[9][18]=1;
grid[6][19]=1;
grid[7][19]=1;
grid[8][19]=1;
grid[7][20]=1;
grid[4][23]=1;
grid[5][23]=1;
grid[6][23]=1;
grid[4][24]=1;
grid[5][24]=1;
grid[6][24]=1;
grid[3][25]=1;
grid[7][25]=1;
grid[2][27]=1;
grid[3][27]=1;
grid[7][27]=1;
grid[8][27]=1;
grid[4][37]=1;
grid[5][37]=1;
grid[4][38]=1;
grid[5][38]=1;
break;
case 2 :
grid[29][29]=1;
grid[28][29]=1;
grid[30][29]=1;
grid[29][28]=1;
grid[28][30]=1;
break;
default :
break;
}
}
int main() {
int currGrid[HEIGHT][WIDTH];
//int nextGrid[HEIGHT][WIDTH];
char name[NAMELENGTH];
printf("Welcome to Conway's Game of Life. To Begin, What Is Your Name?\n");
scanf("%[^\n]%*c", name);
int menSelect;
printf("Hello %s, Please Enter the Integer Next to the Item Below That Describes How You Would Like Your Game of Life to Initially Be Set Up\n \n 1. Gosper's Glider Gun \n 2. R-Pentomino\n ", name);
for(;;){
int checkIn=scanf("%d",&menSelect);
if(checkIn!=1){
fprintf(stderr,"Scanf Has Failed to Read In Any Values\n");
}
if(menSelect!=1 && menSelect!=2){
fprintf(stderr,"%s, %d Is Not a Valid Selection\nPlease Try Again\n",name,menSelect);
}else{
break;
}
}
initGrid(menSelect,currGrid);
return 1;
}
The offending line is int checkIn=scanf("%d",&menSelect); from what I can tell but I can't figure out why.
Many thanks
Undefined behaviour because of how you zero the grid. Looks like a copy/paste error.
for(b=0;a<WIDTH;++b){
^^^
You meant b < WIDTH.

Basic C program does not display anything [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
What does my program not output anything. I have tried other similar versions of solutions for project euler problem 1. I don't need the answer I would just like to know why their is no output. After I compile with gcc and execute the file it seems like is freezes with no output. I have to ctrl-z to kill the program.
#include <stdio.h>
/* Project Euler Problem 1 */
int main()
{
int sum = 0;
int i = 0;
while (i <= 1000);
{
if (i % 3 == 0 || i % 5 == 0);
{
sum += i;
}
i++;
}
printf("%d\n", sum);
return 0;
}
You are closing the while without doing any instruction by putting a semi-colon:
while (i <= 1000);
You should drop the semicolon.
The same for the if instruction:
if (i % 3 == 0 || i % 5 == 0);

I don't understand why it is giving this output? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include <stdio.h>
void main ()
{
int i=0;
for (i=0; i<21; i++)
{
switch(i)
{
case 0:
i+=5;
case 1:
i+=2;
case 5:
i+=5;
default:
i+=4;
break;
}
printf("%d ",i);
}
getchar();
}
Now the Output of this program is 16 21 I don't understand why this program is giving this output when the limit of the loop is less than 18 it gives only 16 but when value is greater than 18 output is 16 21 any help
You need to put break statements at the end of each case. Otherwise each case will 'fall through' to the next one.

Resources