C program compiles but console screen gives a black screen [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 last year.
Improve this question
I'm new in C coding and I just wrote a C program, but when I tried to run the code, it gave a black screen. What do I need to change in my code to fix this problem?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int islemler(){
int x,y,z,c,s0,s1,s2,s3;
printf("Merhaba, matematik islemlerine baslamak icinn lutfen ilk sayiyi giriniz...\n"); /*wants to first number*/
scanf("%d", &x);
printf("Simdi ise ikinci sayiyi giriniz...\n"); /*wants to second number*/
scanf("%d", &y);
printf("Son olarak 3. sayiyi giriniz...\n"); /*wants to thirth number*/
scanf("%d", &z);
printf("Yapacaginiz islemi secmek icin lutfen bir sayi seciniz...\n 1-----Toplama\n 2-----Cikarma\n 3-----Carpma\n 4-----Bolme"); /*Prompts the user to select an action. 1- addition 2- subtraction 3- multiplication 4- division*/
scanf("%d", &c);
s0= x+y+z;
s1= x-y-z;
s2= x*y*z;
s3= x/y/z;
if (c==1){
printf("Sonucunuz %d", s0); /*give answers*/
}
if (c==2){
printf("Sonucunuz %d", s1);
}
if (c==3){
printf("Sonucunuz %d", s2);
}
if (c==4){
printf("Sonucunuz %d", s3);
}
}
int main(){
int islemler();
return 0;
}

int main(){
int islemler();
return 0;
}
You basically declare islemler and then return. It does nothing. I think you wanted to call islemler and not declare it. Remove the int from that. Make it islemler():
int main() {
islemler();
return 0;
}
In your code, you're declaring it. Which is basically like letting the compiler know that the function exists somewhere and not to worry. And it also helps to catch incorrect calls to functions. They are only required in certain situations when the compiler has no way to know that a function exist. If it occurs later on or if it is in a different compilation unit, it's a good practice to declare the prototype like you have done. It does not call the function. And is also completely unnecessary here.
Also, the islemler function should return an int but doesn't return anything. You should get a warning about this. Do not ignore warnings.

Related

Subtraction doesn't work in do while how can I fix it? [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
Hi I am new in c and I making a text adventure game. But I have a problem withe subtraction in a do while loop. The problem is that the number doesn't change it remains at 95 or in 90 in the second statement. Can someone help how to fix that and explain to me how subtraction works in a loop? Also, I want the loop end when the enemy's life is at zero
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
struct batman{
int helath,punch,kick,darts;
};
int main(void) {
int s=0,sum=0,r,knife=5,gun1=10,punch,enemy=100;
struct batman b;
b.helath=100;
b.punch=5;
b.kick=10;
b.darts=100;
printf("\nPress 1 for punch and 2 for kick");
srand(time(NULL));
do{
scanf("%d",&punch);
if(punch==1){
sum=b.punch-enemy;
printf("\nEnemy's Helath %d",sum);
}
else if(punch==2){
sum=b.kick-enemy;
printf("\nEnemy's Helath %d",sum);
}
r=rand()%2;
if(r==1){
s=b.helath-knife;
printf("\nBatman's Health%d",s);
}
else if(r==2){
s=b.helath-gun1;
printf("\nBatman's Health%d",s);
}
}while(punch==1||punch>=2);
return 0;
}
The value of enemy never changes, so unless the value of punch changes this is effectively a constant. You probably want to do something like:
enemy -= punch;
if (enemy <= 0) {
printf("Batman wins!");
}
to update the health of the enemy. For batman you probably want to do the same, i.e.,
b.health -= gun;
if (b.health <= 0) {
printf("Oh no! Batman lost.");
} else {
printf("Batman's health: %d", b.health);
}
Your game should probably end when either batman.health or enemy reaches 0, so you would want to add a check.

I am trying to solve maximum number of platforms for given number of bus using C. I got an error and stuck there [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 am trying to solve a maximum number of platforms for given number of bus. I got an error and stuck there.
I am getting an error:
"online_test.c:3:31: error: expected ‘;’, ‘,’ or ‘)’ before ‘arrivals’".
Can anyone help me where I am making a mistake?
#include<stdio.h>
static int findMinPlatforms(int[] arrivals, int[] departures, int bus){
int no_of_platforms=0,low,high,max,i,j,min,arrival_i,departure_i;
arrival_i=0;
departure_i=0;
for(i=0;((arrival_i<bus)&& (departure_i<bus));i++){
//if(i>0){
if (arrivals[arrival_i]<departures[departure_i]) {
no_of_platforms=no_of_platforms+1;
arrival_i+=1;
} else(arrivals[arrival_i]<departures[departure_i]) {
no_of_platforms=no_of_platforms-1;
departure_i+=1;
}
// }
}
return no_of_platforms;
}
int main (void){
int arrivals[]={901,941,951,1101,1530,1854};
int departures[]={911,1201,1121,1133,1906,2003};
int bus=6;
printf("%d",findMinPlatforms(arrivals,departures,bus));
return 0;
}
The key here is to learn how to read and understand the compiler error:
3:31 means line 3, symbol 31. Which points at [].
Even if you didn't get that number, the text "expected something else before arrivals" should point out the error.
In other words the compiler is telling you: "I don't know why you typed [] there, this isn't valid C, the C language expects you to type something else". And of course, the error is that an array parameter should be written as int arrivals[] and not int[] arrivals.
I had completed my logic for finding a maximum number of platforms at at a given time.
#include<stdio.h>
static int findMinPlatforms(int arrivals[], int departures[], int bus){
int no_of_platforms=0,i,arrival_i,departure_i,max_platform=0;
arrival_i=0;
departure_i=0;
for(i=0;((arrival_i<bus)&& (departure_i<bus));i++){
//if(i>0){
if ((arrivals[arrival_i]<departures[departure_i])){
no_of_platforms=no_of_platforms+1;
if(no_of_platforms>max_platform){
max_platform=no_of_platforms;
}
//printf("i in array is %d\n",i);
// printf("no of gates %d\n",no_of_platforms);
// printf("least value for %d ind is %d and bus is %d\n",arrival_i,arrivals[arrival_i],bus);
// printf("arrival bool %d\n",(arrival_i<bus)&& (departure_i<bus));
if(arrival_i<bus){
arrival_i+=1;
}
// printf("least value for %d ind is %d and bus is %d\n",arrival_i,arrivals[arrival_i],bus);
}
if(arrivals[arrival_i]>departures[departure_i]){
no_of_platforms=no_of_platforms-1;
if(no_of_platforms>max_platform){
max_platform=no_of_platforms;
}
// printf("i in departure is %d\n",i);
// printf("no of gates %d\n",no_of_platforms);
// printf("least value %d ind is %d\n",departure_i,departures[departure_i]);
// printf("departure_i bool %d\n",(arrival_i<bus)&& (departure_i<bus));
if(departure_i<bus){
departure_i+=1;
}
// printf("least value %d ind is %d\n",departure_i,departures[departure_i]);
}
// printf("outside bool %d and %d\n",(arrival_i<bus), (departure_i<bus));
// }
}
return max_platform;
}
int main (void){
int arrivals[]={800,840,850,1000,1400,1700};
int departures[]={810,1100,1020,1030,1800,1900};
int bus=6;
printf("Maximum number of platforms is %d",findMinPlatforms(arrivals,departures,bus));
return 0;
}
//answer Maximum number of platforms is 3

Some logical error always executes if block? [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
#include<stdio.h>
#include<string.h>
char Sys_Pass[]="3699";
char verify_Pass(char *P,char *Q)
{
char ch;
ch=strcmp(P,Q);
printf("String Cmp %s,%s is %c %d\n",P,Q,ch,ch);
if(ch==0)
return 1;
else
return 0;
}
void main()
{
char Pass[10],New[10];
char fp=0,sp=0;
printf("Enter Password : ");
scanf("%s",Pass);
if(fp=verify_Pass(Pass,Sys_Pass))
{
Change: printf("Enter a New Password : ");
scanf("%s",Pass);
printf("Re-Type New Password : ");
scanf("%s",New);
if(sp=verify_Pass(Pass,New))
{
strcpy(Sys_Pass,New);
printf("Password Successfully changed\n");
printf("New Password : %s\n",Sys_Pass);
}
else
printf("Passwords Mismatch\n");
}
else
{
if(strcmp(Pass,"111999")==0);
goto Change;
printf("Wrong Password !!!\n");
}
printf("Fp : %c %d\nSp : %c %d\n",fp,fp,sp,sp);
}
This code is always executing the if block and provides the password change even though wrong current password is typed I want to know if there is a logical error if any ?
I am working on a 8051 project which i want to establish a secret password change i.e if i forgot a password and i type password as 111999 it should direct me to the password change menu but here it is always directing to change password menu. This was actually embedded C code but I tried to rectify that using C code but produces same output in both the cases.
You have a stray semicolon:
// here ---------------v
if(strcmp(Pass,"111999")==0);
goto Change;
As an aside, this is not a good use of goto. The better thing to do is check for either the current password or 111999 in the if condition.
printf("Enter Password : ");
scanf("%s",Pass);
if ((strcmp(Pass,"111999")==0) || (strcmp(Pass,Sys_Pass)==0))
{
printf("Enter a New Password : ");
...
}
else
{
printf("Wrong Password !!!\n");
}
There are several things in this code that have margin for improvement, but I will start by asking if you have used a debugger to debug the issue. If you haven't, you can check gdb: video here
I think also you should consider changing your main so it returns an int, except if you have a very good reason not to do it. Then you could have return codes. Same applies for the input arguments to main. If it receives none, I would strongly encourage you to specify it by typingint main(void). See here for more info
Personally I always add braces around every if/else block, even when it is one line. It helps avoid mistakes.
I dont have anything against the goto statement if it is used properly, but I believe in this case it is not. The Clean Code book by Robert Martin contains some good explanations on how to use it. I think you can try to rewrite this and avoid goto usage.

function prototype in c, compile error

So am trying to learn c by-myself (basically not having any previous experience in any programming language) and now I have some issues with prototyping some of my functions to use in header files.
For the sake of learning I only use the < stdio.h > lib and only use the printf and scanf functions and for now it only prints to console.
I was able to code a working prototype function for my menu that only uses the printf function but the scanf gives me more issues and it just refuses to compile and am having trouble to see where my thinking error is.
my main program:
#include "menu.h"
#include "circlefunctions.h"
#include "input.h"
int main(void){
float diameter;
double straal;
double oppervlakte;
double omtrek;
while(1){
menu();
user_input();
system("cls");
switch(user_input())
{
case 1:
printf(" ----------------------------------------\n");
printf(" Typ de diameter van de cirkel: ");
scanf("%g", &diameter);
printf(" ----------------------------------------\n");
straal = diameter / 2;
oppervlakte = PI * (straal * straal);
omtrek = 2 * PI * straal;
printf(" De straal = %f \n\n", straal );
printf(" De oppervlakte = %f \n\n" , oppervlakte);
printf(" De omtrek = %f \n" , omtrek);
printf(" ----------------------------------------\n");
break;
case 2:
return(0);
case 3:
return(0);
case 9:
return(0);
case 0:
return(0);
}
}
return 0;
}
and the stubborn header:
#include <stdio.h>
void user_input();
void user_input(){
scanf("%d", &user_input);
}
The error that I get while trying to compile is in input.h
the part with; scanf("%d", &user_input);
errorcode: format '%d' expects argument type of 'int ', but argument 2 has type 'void () ()'.
And I also got an error on the switch in the main program that the switch quantity is not an integer. I suspect that this error is related but am not sure. I still have to debug that part but if anyone is willing to point me to the right documentation i would much appreciate it.
And a second question that I have is also related to headers: I have < stdio.h > already included in "menu.h". Would I need to include it again in "input.h"?
(if i understand correctly how the preprocessor works i should not have to include it but I can't find anywhere where this is explained in simple terms unfortunately.)
Edit:
Thank you all for providing valuable information.
#zenith Thank you for your example. I hope you don't mind me asking some more.
I have replaced my code with yours in the "input.h" and it will compile and run now. However the behavior has changed. For some unclear reason i now have to input the choice twice before the program accepts my input. So the 1st input gets ignored after an enter and it will only accept the 2nd input.
Could you perhaps point me in the direction what causes this bug? or perhaps point me to some documentation where this is explained? I don't want to take up to much of you valuable time of-course.
Edit 2
Thanks for the reply and info. I got the bug out and it is working as intended(that was silly of me not to see that).
And to the rest who replied: Ill take your information of-course and also learn from that. Thank you all!
user_input() doesn't return anything, since it's declared void.
But you're trying to use the non-existing return value: switch(user_input()).
This causes undefined behavior.
Additionally, this:
scanf("%d", &user_input);
tries to read an int from stdin and store it in the memory address of the user_input function. Not a good idea. Again, undefined behavior.
What you probably want the function to look like:
int user_input(){
int number; // store user input to this variable
scanf("%d", &number);
return number; // return the user input so that it can be used outside the function
}
If you have header files declared in a previous header file. You will not need to include it again in the subsequent included header files. I tend to not include header files in my local *.h files just for that reason. It avoids circular includes if you declare your includes in the .c files as much as possible.
Your scanf function has as its second argument a function of type void(), void(). Meaning it takes no arguments and returns nothing or "void". I think you want your user_input to be a variable of type 'double' that is filled somewhere, maybe via some user input from the console using a call to 'gets' from stdin.
HTH

I am getting runtime error (SIGSEGV) in a c program [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 8 years ago.
Improve this question
Why I am getting runtime error (SIGSEGV) on the following code:
#include<stdio.h>
#include<string.h>
int main()
{
int t_line,count[10000],i;
scanf("%d",&t_line);
for(i=1;i<=t_line;i++)
{
fflush(stdin);
gets(t);
count[i]=(int)t[0]+(int)t[1]+(int)t[2];
}
for(i=1;i<=t_line;i++)
printf("%d\n",count[i]);
return 0;
}
I have also tried to solve this problem by initialized all the elements of array.
I am wondering how the code compiled, without declaration of the variable t. But, still the only missing elemnt was: char t[your choice of size];. Apart from that
#include<stdio.h>
//#include<string.h> No need of this header,a s you are not using any string functions
int main()
{
int t_line,count[10000],i;
char t[64];//you need to declare the variable before using it
scanf("%d",&t_line);
//Its safer if you check this
if(t_line >= 10000)//if you use 0 and < t_line in for loop below then change the condition to: if(t_line > 10000)
{
printf("ERROR: Limit exceeded. Not enough memory.\n");
return 1;//or you could use exit(1); and #include <stdlib.h>
}
for(i=1;i<=t_line;i++)//suggested: for(i=0;i<t_line;i++)
{
//fflush(stdin);
//gets(t);
char *rc = fgets(t, sizeof(t), stdin);
if(rc != NULL)
{ t[strlen(t) - 1] = '\0';\\because fgets gets the \n into the string too. This line makes fgets similar to gets, improving safety from overflow.
}
else
{
// handle fgets failed error
}
count[i]=(int)t[0]+(int)t[1]+(int)t[2];
}
for(i=1;i<=t_line;i++)//suggested: for(i=0;i<t_line;i++)
printf("%d\n",count[i]);
return 0;
}
Find the solution and suggested changes inline as code comments.
In C, its better to use indexes starting from 0, unless there is a specific requirement to use other values.

Resources