I have an issue with my code. It works properly but I want it to start again after processing, but the program ends and the console closes. I think I need to use a while loop but I'm getting an error. Can you help me please.
here is my code
#include <stdio.h>
#include <math.h>
#include <conio.h>
int main(){
int x,y,tercih; // menu dizilimi sor, programın calismaya devam etmesi gerekiyor.
printf("___ MENU ___\n");
printf("1) Kare Kup\n");
printf("2) Ucgen Tetrahedron\n");
printf("3) Daire Kure\n");
printf("4) Gauss Dagilimi\n");
printf("5) Programdan cik\n");
printf("Lutfen yapmak istediginiz islemi seciniz: ");
scanf("%d",&tercih);
if (tercih==1){
printf("Lutfen kare ve kupun bir ayritinin uzunlugunu giriniz: ");
scanf("%f",&x);
printf(" Alan Hacim\nKare %6.2f %6.2f\nKup %6.2f %6.2f
",x*x,0,6*(x*x),x*x*x); // sonuc 0.00 cikiyor?
} else if (tercih==2){
printf("Lutfen ucgen ve tetrahedronun bir ayritinin uzunlugunu giriniz: ");
scanf("%d",&x);
printf(" Alan Hacim\nUcgen %6.2f %6.2f\nTetra %6.2f %6.2f
",sqrt(3)/4*x*x,0,sqrt(3)*x*x,sqrt(2)/12*x*x*x);
} else if (tercih==3){
printf("Lutfen daire ve kurenin yaricap uzunlugunu giriniz: ");
scanf("%d",&x);
printf(" Alan Hacim\nDaire %6.2f %6.2f\nKure %6.2f %6.2f ",M_PI*x*x,0,M_PI*x*x*x*4/3,4*M_PI*x*x);
} else if (tercih==4){
printf("Lutfen sirasiyla sapma ve boyut degerleri giriniz: "); // soruyu anla yada sor
scanf("%d %d",&x,&y);
printf("Sonuc: %d",x/y);
} else if (tercih==5){
printf("Programdan cikiliyor. ");
} else {
printf("Yanlis islem girdiniz!");
}
return 0;
CodePudding user response:
I believe the problem is not due to your program, but the way that you launch it: I have the impression that you double-click your program in your Windows explorer, your program opens a command prompt, runs, stops and closes the command prompt.
You can alter this, using a pause
or a while
-loop, but you might also simply start your program in another way: you open a command prompt (cmd
) and your open your program in that prompt. Once your program is stopped, the command prompt won't be closed and you can read the output on screen.
P.s. a pause can be achieved using this C command:
getchar();
CodePudding user response:
In pulling your logic into a separate function, it becomes much more reasonable to deal with the actual menu
operations, without having to worry about polluting your current code with more logic:
int menu() { //summarizing the above function in the OP
int tercih; //declare our input variable
//...
scanf("%d", tercih); //read the input
//...
return tercih; //let the calling function know what was entered
}
void prompt() {
int tercih;
do {
tercih = menu(); //call menu, and note the input value 'tercih'
} while (tercih != 5); //continue looping until we meet the 'exit' condition
}
int main() {
prompt();
return 0;
}
You can even use this same approach to pull the logic for the individual menu options into their own function for handling the action.
CodePudding user response:
int main(){
int x,y,tercih; // menu dizilimi sor, programın calismaya devam etmesi gerekiyor.
do {
printf("___ MENU ___\n");
printf("1) Kare Kup\n");
printf("2) Ucgen Tetrahedron\n");
printf("3) Daire Kure\n");
printf("4) Gauss Dagilimi\n");
printf("5) Programdan cik\n");
printf("Lutfen yapmak istediginiz islemi seciniz: ");
scanf("%d",&tercih);
switch(tercih) {
case 1:
printf("Lutfen kare ve kupun bir ayritinin uzunlugunu giriniz: ");
scanf("%f",&x);
printf(" Alan Hacim\nKare %6.2f %6.2f\nKup %6.2f %6.2f
",x*x,0,6*(x*x),x*x*x); // sonuc 0.00 cikiyor?
break;
case 2:
printf("Lutfen ucgen ve tetrahedronun bir ayritinin uzunlugunu giriniz: ");
scanf("%d",&x);
printf(" Alan Hacim\nUcgen %6.2f %6.2f\nTetra %6.2f %6.2f
",sqrt(3)/4*x*x,0,sqrt(3)*x*x,sqrt(2)/12*x*x*x);
break;
case 3:
printf("Lutfen daire ve kurenin yaricap uzunlugunu giriniz: ");
scanf("%d",&x);
printf(" Alan Hacim\nDaire %6.2f %6.2f\nKure %6.2f %6.2f ",M_PI*x*x,0,M_PI*x*x*x*4/3,4*M_PI*x*x);
break;
case 4:
printf("Lutfen sirasiyla sapma ve boyut degerleri giriniz: "); // soruyu anla yada sor
scanf("%d %d",&x,&y);
printf("Sonuc: %d",x/y);
break;
case 5:
printf("Programdan cikiliyor. ");
break;
default:
printf("Yanlis islem girdiniz!");
break;
}
} while(tercih != 5);
return 0;
}
It's easy just use a "do while" loop like this. & instead of using "nested if" you can use "switch"