How to make a C program loop back to starting function?
For example, to this program that calculates equations of grade 3, after it calculates the equation, it stops. how do I make it so it loops back again to the start without executing again? I'm still new to this platform so I'll be in your care!
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
return 0;
}
c
add a comment |
For example, to this program that calculates equations of grade 3, after it calculates the equation, it stops. how do I make it so it loops back again to the start without executing again? I'm still new to this platform so I'll be in your care!
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
return 0;
}
c
add a comment |
For example, to this program that calculates equations of grade 3, after it calculates the equation, it stops. how do I make it so it loops back again to the start without executing again? I'm still new to this platform so I'll be in your care!
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
return 0;
}
c
For example, to this program that calculates equations of grade 3, after it calculates the equation, it stops. how do I make it so it loops back again to the start without executing again? I'm still new to this platform so I'll be in your care!
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
return 0;
}
c
c
edited Nov 22 '18 at 15:26
terales
1,7121526
1,7121526
asked Nov 22 '18 at 14:34
Rami RaghfanRami Raghfan
55
55
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Just wrap your code into an infinite loop (while(1) { /* Your code here*/ }
):
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
while (1) {
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
}
return 0;
}
I see! So a formation of indents can do that? Will you be so kind to provide me with an in depth resource on using this?
– Rami Raghfan
Nov 22 '18 at 14:46
It's not about indentation (C
doesn't care about it, but humans do). It's about adding an infinite loop, check an example I've added in the beginning and accept an answer if it was helpful :)
– terales
Nov 22 '18 at 14:50
add a comment |
You could wrap everything in a do-while
loop and ask the user whether he wants to continue the execution or not, for example:
int a,v,b,c,delt;
float x1,x2;
char choice;
do{
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", &a, &b, &c);
if(a!=0)
{
v=pow(b, 2);
delt = v-(4*a*c);
if (delt>=0)
{
delt=sqrt(delt);
x1=-(b+delt)/(2.0*a);
x2=-(b-delt)/(2.0*a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
}
else
{
printf("Ecuatia nu are soluti! n");
}
}
else if(a==0)
{
printf("nBLACKHOLE");
}
printf("nEvaluate new equation?(y/n) ")
scanf("%c",&choice)
}while(strcmp(choice,"y")==0);
return 0;
The block inside the do{...}
will execute at least once, then the user will be asked to input a char (y/n) to decide whether to continue or not.
The strcmp(string1,string2)
compares two strings and returns 0 if they are equal, so if the user chose "y", the strcmp will return 0 and the do-while will be executed again.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433211%2fhow-to-make-a-c-program-loop-back-to-starting-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just wrap your code into an infinite loop (while(1) { /* Your code here*/ }
):
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
while (1) {
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
}
return 0;
}
I see! So a formation of indents can do that? Will you be so kind to provide me with an in depth resource on using this?
– Rami Raghfan
Nov 22 '18 at 14:46
It's not about indentation (C
doesn't care about it, but humans do). It's about adding an infinite loop, check an example I've added in the beginning and accept an answer if it was helpful :)
– terales
Nov 22 '18 at 14:50
add a comment |
Just wrap your code into an infinite loop (while(1) { /* Your code here*/ }
):
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
while (1) {
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
}
return 0;
}
I see! So a formation of indents can do that? Will you be so kind to provide me with an in depth resource on using this?
– Rami Raghfan
Nov 22 '18 at 14:46
It's not about indentation (C
doesn't care about it, but humans do). It's about adding an infinite loop, check an example I've added in the beginning and accept an answer if it was helpful :)
– terales
Nov 22 '18 at 14:50
add a comment |
Just wrap your code into an infinite loop (while(1) { /* Your code here*/ }
):
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
while (1) {
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
}
return 0;
}
Just wrap your code into an infinite loop (while(1) { /* Your code here*/ }
):
#include < stdio.h >
int main() {
int a, v, b, c, delt;
float x1, x2;
while (1) {
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", & a, & b, & c);
if (a != 0) {
v = pow(b, 2);
delt = v - (4 * a * c);
if (delt >= 0) {
delt = sqrt(delt);
x1 = -(b + delt) / (2.0 * a);
x2 = -(b - delt) / (2.0 * a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
} else {
printf("Ecuatia nu are soluti! n");
}
} else if (a == 0) {
printf("nBLACKHOLE");
}
}
return 0;
}
edited Nov 22 '18 at 14:49
answered Nov 22 '18 at 14:41
teralesterales
1,7121526
1,7121526
I see! So a formation of indents can do that? Will you be so kind to provide me with an in depth resource on using this?
– Rami Raghfan
Nov 22 '18 at 14:46
It's not about indentation (C
doesn't care about it, but humans do). It's about adding an infinite loop, check an example I've added in the beginning and accept an answer if it was helpful :)
– terales
Nov 22 '18 at 14:50
add a comment |
I see! So a formation of indents can do that? Will you be so kind to provide me with an in depth resource on using this?
– Rami Raghfan
Nov 22 '18 at 14:46
It's not about indentation (C
doesn't care about it, but humans do). It's about adding an infinite loop, check an example I've added in the beginning and accept an answer if it was helpful :)
– terales
Nov 22 '18 at 14:50
I see! So a formation of indents can do that? Will you be so kind to provide me with an in depth resource on using this?
– Rami Raghfan
Nov 22 '18 at 14:46
I see! So a formation of indents can do that? Will you be so kind to provide me with an in depth resource on using this?
– Rami Raghfan
Nov 22 '18 at 14:46
It's not about indentation (
C
doesn't care about it, but humans do). It's about adding an infinite loop, check an example I've added in the beginning and accept an answer if it was helpful :)– terales
Nov 22 '18 at 14:50
It's not about indentation (
C
doesn't care about it, but humans do). It's about adding an infinite loop, check an example I've added in the beginning and accept an answer if it was helpful :)– terales
Nov 22 '18 at 14:50
add a comment |
You could wrap everything in a do-while
loop and ask the user whether he wants to continue the execution or not, for example:
int a,v,b,c,delt;
float x1,x2;
char choice;
do{
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", &a, &b, &c);
if(a!=0)
{
v=pow(b, 2);
delt = v-(4*a*c);
if (delt>=0)
{
delt=sqrt(delt);
x1=-(b+delt)/(2.0*a);
x2=-(b-delt)/(2.0*a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
}
else
{
printf("Ecuatia nu are soluti! n");
}
}
else if(a==0)
{
printf("nBLACKHOLE");
}
printf("nEvaluate new equation?(y/n) ")
scanf("%c",&choice)
}while(strcmp(choice,"y")==0);
return 0;
The block inside the do{...}
will execute at least once, then the user will be asked to input a char (y/n) to decide whether to continue or not.
The strcmp(string1,string2)
compares two strings and returns 0 if they are equal, so if the user chose "y", the strcmp will return 0 and the do-while will be executed again.
add a comment |
You could wrap everything in a do-while
loop and ask the user whether he wants to continue the execution or not, for example:
int a,v,b,c,delt;
float x1,x2;
char choice;
do{
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", &a, &b, &c);
if(a!=0)
{
v=pow(b, 2);
delt = v-(4*a*c);
if (delt>=0)
{
delt=sqrt(delt);
x1=-(b+delt)/(2.0*a);
x2=-(b-delt)/(2.0*a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
}
else
{
printf("Ecuatia nu are soluti! n");
}
}
else if(a==0)
{
printf("nBLACKHOLE");
}
printf("nEvaluate new equation?(y/n) ")
scanf("%c",&choice)
}while(strcmp(choice,"y")==0);
return 0;
The block inside the do{...}
will execute at least once, then the user will be asked to input a char (y/n) to decide whether to continue or not.
The strcmp(string1,string2)
compares two strings and returns 0 if they are equal, so if the user chose "y", the strcmp will return 0 and the do-while will be executed again.
add a comment |
You could wrap everything in a do-while
loop and ask the user whether he wants to continue the execution or not, for example:
int a,v,b,c,delt;
float x1,x2;
char choice;
do{
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", &a, &b, &c);
if(a!=0)
{
v=pow(b, 2);
delt = v-(4*a*c);
if (delt>=0)
{
delt=sqrt(delt);
x1=-(b+delt)/(2.0*a);
x2=-(b-delt)/(2.0*a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
}
else
{
printf("Ecuatia nu are soluti! n");
}
}
else if(a==0)
{
printf("nBLACKHOLE");
}
printf("nEvaluate new equation?(y/n) ")
scanf("%c",&choice)
}while(strcmp(choice,"y")==0);
return 0;
The block inside the do{...}
will execute at least once, then the user will be asked to input a char (y/n) to decide whether to continue or not.
The strcmp(string1,string2)
compares two strings and returns 0 if they are equal, so if the user chose "y", the strcmp will return 0 and the do-while will be executed again.
You could wrap everything in a do-while
loop and ask the user whether he wants to continue the execution or not, for example:
int a,v,b,c,delt;
float x1,x2;
char choice;
do{
printf("nIntroduceti cele 3 parametrii ecuatia: ");
scanf("%d %d %d", &a, &b, &c);
if(a!=0)
{
v=pow(b, 2);
delt = v-(4*a*c);
if (delt>=0)
{
delt=sqrt(delt);
x1=-(b+delt)/(2.0*a);
x2=-(b-delt)/(2.0*a);
printf("nValoara lui x1 este: %f", x1);
printf("n");
printf("nValoara lui x2 este: %f", x2);
}
else
{
printf("Ecuatia nu are soluti! n");
}
}
else if(a==0)
{
printf("nBLACKHOLE");
}
printf("nEvaluate new equation?(y/n) ")
scanf("%c",&choice)
}while(strcmp(choice,"y")==0);
return 0;
The block inside the do{...}
will execute at least once, then the user will be asked to input a char (y/n) to decide whether to continue or not.
The strcmp(string1,string2)
compares two strings and returns 0 if they are equal, so if the user chose "y", the strcmp will return 0 and the do-while will be executed again.
answered Nov 22 '18 at 14:50
DLMDLM
584
584
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433211%2fhow-to-make-a-c-program-loop-back-to-starting-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown