Why does the computer ignore my program? [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 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)

Related

Segmentation fault(core dumped) in C after running [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 3 months ago.
Improve this question
I am a beginner to C language and I wrote this code
#include <stdio.h>
int main() {
int day = 35;
if (day>31)
printf('Please enter a valid date');
else if (day%7==1)
printf('Monday');
else if (day%7==2)
printf('Tuesday');
else if (day%7==3)
printf('Wednesday');
else if (day%7==4)
printf('Thursday');
else if (day%7==5)
printf('Friday');
else if (day%7==6)
printf('Saturday');
else
printf('Sunday');
return 0;
}
I ran this code on Replit and it is showing Segmentation error(core dumped).
enter image description here
Plz help me to solve this problem and explain about my mistake
I tried the above code on replit and I expected to get my result as-Please enter a valid date
But I got segmentation error(core dumped)
'' are character literals; "" are string literals. You cannot have a multi-length character literal. Here is working code:
int day = 35;
if (day > 31)
printf("Please enter a valid date");
else if (day % 7 == 1)
printf("Monday");
else if (day % 7 == 2)
printf("Tuesday");
else if (day % 7 == 3)
printf("Wednesday");
else if (day % 7 == 4)
printf("Thursday");
else if (day % 7 == 5)
printf("Friday");
else if (day % 7 == 6)
printf("Saturday");
else
printf("Sunday");

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.

The output is correct but the online judge wouldn't accept? [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 2 years ago.
Improve this question
I had tried all the C compiler but keep getting wrong answer on test 1, even though i already try to use the input on test 1 and had received the correct output when I tried it on the DEV c++.
input:
5
0 1 0 1 1
output:
4
this is a case study, so basically 5 in the input means the amount of period.
0 means break time and 1 means study time. if input is 1 total hour of study hours would be added by one, if its 0 there will be no study hence study hours would not be added. there is also a special case, like shown in the input if previously she was studying and now she is having breaks but after the breaks she will have a class (1 0 1) the break will be counted as study time and so study hours will be ++.
any idea? is it the code perhaps?
(the accepted code)
#include <stdio.h>
#include <string.h>
int main()
{
int n,hours=0;
scanf("%d",&n);
char str[n*2];
int x[n];
while ((getchar()) != '\n');
fgets(str, n*2 , stdin);
char* piece = strtok(str, " ");
for(int i=1; piece != NULL ; i++)
{
sscanf(piece, "%d" , &x[i]);
piece = strtok(NULL, " ");
}
for(int i=0 ; i<n+1; i++)
{
if(i==0)
{
if(x[0]==1){
hours++;
}
}
else
{
if(x[i]==0)
{
if( x[i-1]==1 && x[i+1]==1 )
{
hours++;
}
}
else
{
hours++;
}
}
}
printf("%d\n",hours);
return 0;
}
You are using [ i-1 ] and when index is 0 in for some inputs you will get a garbage value. May be it will work for some inputs,and not for others.
The most common reason the online judge do not accept the solution even though the answer is correct in other ide is either because you have extra indentation in your program or a loop doesn't terminate, or you get garbage value or you try incrementing a not defined value.

C program: if statement within switch statement [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 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)

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);

Resources