How to restart main() from the beginning on an error condition?











up vote
1
down vote

favorite












#include <stdio.h>
int main()
{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo numbers.n");
float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
printf("Please type the expression you want to calculate: ");
if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("nInvalid input! Please try again...nn");
/* want to restart main() again here */
}
else {
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if(num2 == 0) {
printf("Division by zero is not possible!nPlease try again...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",ans);
printf("nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...n");
scanf("%s",&ask);
if (ask == 'y' || ask == 'Y') {
printf("n");
main();
}
else {
printf("Thank you for using the program. Please give full marks.");
}
}
return 0;
}









share|improve this question









New contributor




John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
    – Some programmer dude
    yesterday






  • 2




    As for "repeating" something, use loops.
    – Some programmer dude
    yesterday










  • Possible duplicate of calling main() in main() in c
    – borrible
    yesterday






  • 3




    @John Hunter. Are you aware of loops in programming? Also, are you aware of recursion? Because it seems you are trying to use recursion for something that a simple loop would be more than enough
    – kyriakosSt
    yesterday






  • 2




    @JohnHunter I understand what you need to do, but I need to know if you are aware of loops in the first place in order to explain. Kristjan Kica 's answer however seems pretty good
    – kyriakosSt
    yesterday















up vote
1
down vote

favorite












#include <stdio.h>
int main()
{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo numbers.n");
float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
printf("Please type the expression you want to calculate: ");
if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("nInvalid input! Please try again...nn");
/* want to restart main() again here */
}
else {
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if(num2 == 0) {
printf("Division by zero is not possible!nPlease try again...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",ans);
printf("nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...n");
scanf("%s",&ask);
if (ask == 'y' || ask == 'Y') {
printf("n");
main();
}
else {
printf("Thank you for using the program. Please give full marks.");
}
}
return 0;
}









share|improve this question









New contributor




John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
    – Some programmer dude
    yesterday






  • 2




    As for "repeating" something, use loops.
    – Some programmer dude
    yesterday










  • Possible duplicate of calling main() in main() in c
    – borrible
    yesterday






  • 3




    @John Hunter. Are you aware of loops in programming? Also, are you aware of recursion? Because it seems you are trying to use recursion for something that a simple loop would be more than enough
    – kyriakosSt
    yesterday






  • 2




    @JohnHunter I understand what you need to do, but I need to know if you are aware of loops in the first place in order to explain. Kristjan Kica 's answer however seems pretty good
    – kyriakosSt
    yesterday













up vote
1
down vote

favorite









up vote
1
down vote

favorite











#include <stdio.h>
int main()
{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo numbers.n");
float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
printf("Please type the expression you want to calculate: ");
if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("nInvalid input! Please try again...nn");
/* want to restart main() again here */
}
else {
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if(num2 == 0) {
printf("Division by zero is not possible!nPlease try again...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",ans);
printf("nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...n");
scanf("%s",&ask);
if (ask == 'y' || ask == 'Y') {
printf("n");
main();
}
else {
printf("Thank you for using the program. Please give full marks.");
}
}
return 0;
}









share|improve this question









New contributor




John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











#include <stdio.h>
int main()
{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo numbers.n");
float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
printf("Please type the expression you want to calculate: ");
if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("nInvalid input! Please try again...nn");
/* want to restart main() again here */
}
else {
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if(num2 == 0) {
printf("Division by zero is not possible!nPlease try again...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",ans);
printf("nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...n");
scanf("%s",&ask);
if (ask == 'y' || ask == 'Y') {
printf("n");
main();
}
else {
printf("Thank you for using the program. Please give full marks.");
}
}
return 0;
}






c






share|improve this question









New contributor




John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 11 hours ago









HostileFork

24.6k775131




24.6k775131






New contributor




John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









John Hunter

74




74




New contributor




John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
    – Some programmer dude
    yesterday






  • 2




    As for "repeating" something, use loops.
    – Some programmer dude
    yesterday










  • Possible duplicate of calling main() in main() in c
    – borrible
    yesterday






  • 3




    @John Hunter. Are you aware of loops in programming? Also, are you aware of recursion? Because it seems you are trying to use recursion for something that a simple loop would be more than enough
    – kyriakosSt
    yesterday






  • 2




    @JohnHunter I understand what you need to do, but I need to know if you are aware of loops in the first place in order to explain. Kristjan Kica 's answer however seems pretty good
    – kyriakosSt
    yesterday














  • 2




    Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
    – Some programmer dude
    yesterday






  • 2




    As for "repeating" something, use loops.
    – Some programmer dude
    yesterday










  • Possible duplicate of calling main() in main() in c
    – borrible
    yesterday






  • 3




    @John Hunter. Are you aware of loops in programming? Also, are you aware of recursion? Because it seems you are trying to use recursion for something that a simple loop would be more than enough
    – kyriakosSt
    yesterday






  • 2




    @JohnHunter I understand what you need to do, but I need to know if you are aware of loops in the first place in order to explain. Kristjan Kica 's answer however seems pretty good
    – kyriakosSt
    yesterday








2




2




Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
– Some programmer dude
yesterday




Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
– Some programmer dude
yesterday




2




2




As for "repeating" something, use loops.
– Some programmer dude
yesterday




As for "repeating" something, use loops.
– Some programmer dude
yesterday












Possible duplicate of calling main() in main() in c
– borrible
yesterday




Possible duplicate of calling main() in main() in c
– borrible
yesterday




3




3




@John Hunter. Are you aware of loops in programming? Also, are you aware of recursion? Because it seems you are trying to use recursion for something that a simple loop would be more than enough
– kyriakosSt
yesterday




@John Hunter. Are you aware of loops in programming? Also, are you aware of recursion? Because it seems you are trying to use recursion for something that a simple loop would be more than enough
– kyriakosSt
yesterday




2




2




@JohnHunter I understand what you need to do, but I need to know if you are aware of loops in the first place in order to explain. Kristjan Kica 's answer however seems pretty good
– kyriakosSt
yesterday




@JohnHunter I understand what you need to do, but I need to know if you are aware of loops in the first place in order to explain. Kristjan Kica 's answer however seems pretty good
– kyriakosSt
yesterday












3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










To answer your question.



I would not recommend calling main.



You could create another function that has all you code.



Inside main, you call that function.



You can call a function inside that function (called recursion)



However, a simple loop could do the job.



do{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo numbers.n");
float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
printf("Please type the expression you want to calculate: ");
if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("nInvalid input! Please try again...nn");
}
else {
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if (num2 == 0) {
printf("Division by zero is not possible!nPlease try again...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",ans);
printf("nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...n");
scanf("%s",&ask);
printf("n");
}while(ask == 'y' || ask == 'Y') ;
printf("Thank you for using the program. Please give full marks.");
}


Edit: To answer the comment to this question what you want to do is:



while(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("nInvalid input! Please try again...nn");
}


And remove the else



EDIT2: Full code. Note that the expression cannot be more than 99 characters.



#include <stdio.h>

int main()
{

float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
char string[100];
do{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo numbers.n");

printf("Please type the expression you want to calculate: ");

while(1){
fgets (string , 100 ,stdin);
if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
printf("nInvalid input! Please try again...nn");
else
break;

}

switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if (num2 == 0) {
printf("Division by zero is not possible!nPlease try again...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",ans);
printf("nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...n");
scanf("%s",&ask);
printf("n");

}while(ask == 'y' || ask == 'Y') ;

printf("Thank you for using the program. Please give full marks.");

return 0;

}





share|improve this answer























  • Hi, please note that I was referring to this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
    – John Hunter
    yesterday












  • You have called main elsewhere. Let me edit my answer. The same principle applies
    – Kristjan Kica
    yesterday










  • Showing invalid input infinite no. times... Something like this... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again....
    – John Hunter
    yesterday












  • If scanf can't find matching input for the expected format, it will return. With this loop you no longer know where the input is, and will probably fail indefinitely. This pattern is generally a bad idea. Consider using line based input with fgets and then sscanf.
    – paddy
    yesterday










  • Please give an example @paddy... I am really new to programming....
    – John Hunter
    yesterday


















up vote
0
down vote













@Kristjan Kica answer is good. I think you are using spaces in your input like 1 + 2.



According to manual page of scanf



 All conversions are introduced by the % (percent sign) character.  The format string may also contain other characters.  White space
(such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else
matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input
conversion cannot be made.


Remove the spaces and try again.



Example:
1+2 should work by the changes mentioned in kristijan answer.



Edit:
Replace the line in @Kristjan Kica answer



while(ask == 'y' || ask == 'Y') ;                        


with



}while(ask == 'y' || ask == 'Y') ;                        


Edit 2:
Last closing } should be your main functions closing brace.






share|improve this answer










New contributor




bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Upon running Kristjan's code, it say expected while before }
    – John Hunter
    yesterday










  • Yup it's working... But how can I do the same thing in this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
    – John Hunter
    yesterday












  • Are u getting the output or is it just compiled?
    – bhanu7k
    yesterday










  • You can achieve the same with if statement(remove while inside do-while and try again).
    – bhanu7k
    yesterday


















up vote
0
down vote













Finally solved it. All thanks to Kristjan Kica...



#include <stdio.h>

int main(void)
{
float num1;
float num2;
float ans;
char symbol;
char ask;
char string[100];
fflush(stdin);
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by
Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo numbers.n");
printf("Please type the expression you want to calculate: ");
fgets (string , 100 ,stdin);
if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
{
printf("nInvalid input! Please try again...nn");
main();
}
else
{
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if (num2 == 0) {
printf("Division by zero is not possible!nPlease try again...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",ans);
printf("nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...n");
scanf("%s",&ask);
printf("n");
if (ask == 'y' || ask == 'Y')
{
main();
}
else {
return 0;
}
}
}





share|improve this answer








New contributor




John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    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',
    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
    });


    }
    });






    John Hunter is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372593%2fhow-to-restart-main-from-the-beginning-on-an-error-condition%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote



    accepted










    To answer your question.



    I would not recommend calling main.



    You could create another function that has all you code.



    Inside main, you call that function.



    You can call a function inside that function (called recursion)



    However, a simple loop could do the job.



    do{
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");
    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    printf("Please type the expression you want to calculate: ");
    if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
    printf("nInvalid input! Please try again...nn");
    }
    else {
    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");
    }while(ask == 'y' || ask == 'Y') ;
    printf("Thank you for using the program. Please give full marks.");
    }


    Edit: To answer the comment to this question what you want to do is:



    while(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
    printf("nInvalid input! Please try again...nn");
    }


    And remove the else



    EDIT2: Full code. Note that the expression cannot be more than 99 characters.



    #include <stdio.h>

    int main()
    {

    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    char string[100];
    do{
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");

    printf("Please type the expression you want to calculate: ");

    while(1){
    fgets (string , 100 ,stdin);
    if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
    printf("nInvalid input! Please try again...nn");
    else
    break;

    }

    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");

    }while(ask == 'y' || ask == 'Y') ;

    printf("Thank you for using the program. Please give full marks.");

    return 0;

    }





    share|improve this answer























    • Hi, please note that I was referring to this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
      – John Hunter
      yesterday












    • You have called main elsewhere. Let me edit my answer. The same principle applies
      – Kristjan Kica
      yesterday










    • Showing invalid input infinite no. times... Something like this... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again....
      – John Hunter
      yesterday












    • If scanf can't find matching input for the expected format, it will return. With this loop you no longer know where the input is, and will probably fail indefinitely. This pattern is generally a bad idea. Consider using line based input with fgets and then sscanf.
      – paddy
      yesterday










    • Please give an example @paddy... I am really new to programming....
      – John Hunter
      yesterday















    up vote
    7
    down vote



    accepted










    To answer your question.



    I would not recommend calling main.



    You could create another function that has all you code.



    Inside main, you call that function.



    You can call a function inside that function (called recursion)



    However, a simple loop could do the job.



    do{
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");
    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    printf("Please type the expression you want to calculate: ");
    if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
    printf("nInvalid input! Please try again...nn");
    }
    else {
    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");
    }while(ask == 'y' || ask == 'Y') ;
    printf("Thank you for using the program. Please give full marks.");
    }


    Edit: To answer the comment to this question what you want to do is:



    while(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
    printf("nInvalid input! Please try again...nn");
    }


    And remove the else



    EDIT2: Full code. Note that the expression cannot be more than 99 characters.



    #include <stdio.h>

    int main()
    {

    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    char string[100];
    do{
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");

    printf("Please type the expression you want to calculate: ");

    while(1){
    fgets (string , 100 ,stdin);
    if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
    printf("nInvalid input! Please try again...nn");
    else
    break;

    }

    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");

    }while(ask == 'y' || ask == 'Y') ;

    printf("Thank you for using the program. Please give full marks.");

    return 0;

    }





    share|improve this answer























    • Hi, please note that I was referring to this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
      – John Hunter
      yesterday












    • You have called main elsewhere. Let me edit my answer. The same principle applies
      – Kristjan Kica
      yesterday










    • Showing invalid input infinite no. times... Something like this... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again....
      – John Hunter
      yesterday












    • If scanf can't find matching input for the expected format, it will return. With this loop you no longer know where the input is, and will probably fail indefinitely. This pattern is generally a bad idea. Consider using line based input with fgets and then sscanf.
      – paddy
      yesterday










    • Please give an example @paddy... I am really new to programming....
      – John Hunter
      yesterday













    up vote
    7
    down vote



    accepted







    up vote
    7
    down vote



    accepted






    To answer your question.



    I would not recommend calling main.



    You could create another function that has all you code.



    Inside main, you call that function.



    You can call a function inside that function (called recursion)



    However, a simple loop could do the job.



    do{
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");
    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    printf("Please type the expression you want to calculate: ");
    if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
    printf("nInvalid input! Please try again...nn");
    }
    else {
    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");
    }while(ask == 'y' || ask == 'Y') ;
    printf("Thank you for using the program. Please give full marks.");
    }


    Edit: To answer the comment to this question what you want to do is:



    while(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
    printf("nInvalid input! Please try again...nn");
    }


    And remove the else



    EDIT2: Full code. Note that the expression cannot be more than 99 characters.



    #include <stdio.h>

    int main()
    {

    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    char string[100];
    do{
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");

    printf("Please type the expression you want to calculate: ");

    while(1){
    fgets (string , 100 ,stdin);
    if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
    printf("nInvalid input! Please try again...nn");
    else
    break;

    }

    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");

    }while(ask == 'y' || ask == 'Y') ;

    printf("Thank you for using the program. Please give full marks.");

    return 0;

    }





    share|improve this answer














    To answer your question.



    I would not recommend calling main.



    You could create another function that has all you code.



    Inside main, you call that function.



    You can call a function inside that function (called recursion)



    However, a simple loop could do the job.



    do{
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");
    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    printf("Please type the expression you want to calculate: ");
    if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
    printf("nInvalid input! Please try again...nn");
    }
    else {
    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");
    }while(ask == 'y' || ask == 'Y') ;
    printf("Thank you for using the program. Please give full marks.");
    }


    Edit: To answer the comment to this question what you want to do is:



    while(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
    {
    printf("nInvalid input! Please try again...nn");
    }


    And remove the else



    EDIT2: Full code. Note that the expression cannot be more than 99 characters.



    #include <stdio.h>

    int main()
    {

    float num1;
    float num2;
    float ans = 0.0;
    char symbol;
    char ask;
    char string[100];
    do{
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");

    printf("Please type the expression you want to calculate: ");

    while(1){
    fgets (string , 100 ,stdin);
    if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
    printf("nInvalid input! Please try again...nn");
    else
    break;

    }

    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");

    }while(ask == 'y' || ask == 'Y') ;

    printf("Thank you for using the program. Please give full marks.");

    return 0;

    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 15 hours ago

























    answered yesterday









    Kristjan Kica

    2,0311826




    2,0311826












    • Hi, please note that I was referring to this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
      – John Hunter
      yesterday












    • You have called main elsewhere. Let me edit my answer. The same principle applies
      – Kristjan Kica
      yesterday










    • Showing invalid input infinite no. times... Something like this... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again....
      – John Hunter
      yesterday












    • If scanf can't find matching input for the expected format, it will return. With this loop you no longer know where the input is, and will probably fail indefinitely. This pattern is generally a bad idea. Consider using line based input with fgets and then sscanf.
      – paddy
      yesterday










    • Please give an example @paddy... I am really new to programming....
      – John Hunter
      yesterday


















    • Hi, please note that I was referring to this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
      – John Hunter
      yesterday












    • You have called main elsewhere. Let me edit my answer. The same principle applies
      – Kristjan Kica
      yesterday










    • Showing invalid input infinite no. times... Something like this... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again....
      – John Hunter
      yesterday












    • If scanf can't find matching input for the expected format, it will return. With this loop you no longer know where the input is, and will probably fail indefinitely. This pattern is generally a bad idea. Consider using line based input with fgets and then sscanf.
      – paddy
      yesterday










    • Please give an example @paddy... I am really new to programming....
      – John Hunter
      yesterday
















    Hi, please note that I was referring to this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
    – John Hunter
    yesterday






    Hi, please note that I was referring to this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
    – John Hunter
    yesterday














    You have called main elsewhere. Let me edit my answer. The same principle applies
    – Kristjan Kica
    yesterday




    You have called main elsewhere. Let me edit my answer. The same principle applies
    – Kristjan Kica
    yesterday












    Showing invalid input infinite no. times... Something like this... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again....
    – John Hunter
    yesterday






    Showing invalid input infinite no. times... Something like this... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again....
    – John Hunter
    yesterday














    If scanf can't find matching input for the expected format, it will return. With this loop you no longer know where the input is, and will probably fail indefinitely. This pattern is generally a bad idea. Consider using line based input with fgets and then sscanf.
    – paddy
    yesterday




    If scanf can't find matching input for the expected format, it will return. With this loop you no longer know where the input is, and will probably fail indefinitely. This pattern is generally a bad idea. Consider using line based input with fgets and then sscanf.
    – paddy
    yesterday












    Please give an example @paddy... I am really new to programming....
    – John Hunter
    yesterday




    Please give an example @paddy... I am really new to programming....
    – John Hunter
    yesterday












    up vote
    0
    down vote













    @Kristjan Kica answer is good. I think you are using spaces in your input like 1 + 2.



    According to manual page of scanf



     All conversions are introduced by the % (percent sign) character.  The format string may also contain other characters.  White space
    (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else
    matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input
    conversion cannot be made.


    Remove the spaces and try again.



    Example:
    1+2 should work by the changes mentioned in kristijan answer.



    Edit:
    Replace the line in @Kristjan Kica answer



    while(ask == 'y' || ask == 'Y') ;                        


    with



    }while(ask == 'y' || ask == 'Y') ;                        


    Edit 2:
    Last closing } should be your main functions closing brace.






    share|improve this answer










    New contributor




    bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Upon running Kristjan's code, it say expected while before }
      – John Hunter
      yesterday










    • Yup it's working... But how can I do the same thing in this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
      – John Hunter
      yesterday












    • Are u getting the output or is it just compiled?
      – bhanu7k
      yesterday










    • You can achieve the same with if statement(remove while inside do-while and try again).
      – bhanu7k
      yesterday















    up vote
    0
    down vote













    @Kristjan Kica answer is good. I think you are using spaces in your input like 1 + 2.



    According to manual page of scanf



     All conversions are introduced by the % (percent sign) character.  The format string may also contain other characters.  White space
    (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else
    matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input
    conversion cannot be made.


    Remove the spaces and try again.



    Example:
    1+2 should work by the changes mentioned in kristijan answer.



    Edit:
    Replace the line in @Kristjan Kica answer



    while(ask == 'y' || ask == 'Y') ;                        


    with



    }while(ask == 'y' || ask == 'Y') ;                        


    Edit 2:
    Last closing } should be your main functions closing brace.






    share|improve this answer










    New contributor




    bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Upon running Kristjan's code, it say expected while before }
      – John Hunter
      yesterday










    • Yup it's working... But how can I do the same thing in this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
      – John Hunter
      yesterday












    • Are u getting the output or is it just compiled?
      – bhanu7k
      yesterday










    • You can achieve the same with if statement(remove while inside do-while and try again).
      – bhanu7k
      yesterday













    up vote
    0
    down vote










    up vote
    0
    down vote









    @Kristjan Kica answer is good. I think you are using spaces in your input like 1 + 2.



    According to manual page of scanf



     All conversions are introduced by the % (percent sign) character.  The format string may also contain other characters.  White space
    (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else
    matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input
    conversion cannot be made.


    Remove the spaces and try again.



    Example:
    1+2 should work by the changes mentioned in kristijan answer.



    Edit:
    Replace the line in @Kristjan Kica answer



    while(ask == 'y' || ask == 'Y') ;                        


    with



    }while(ask == 'y' || ask == 'Y') ;                        


    Edit 2:
    Last closing } should be your main functions closing brace.






    share|improve this answer










    New contributor




    bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    @Kristjan Kica answer is good. I think you are using spaces in your input like 1 + 2.



    According to manual page of scanf



     All conversions are introduced by the % (percent sign) character.  The format string may also contain other characters.  White space
    (such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else
    matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input
    conversion cannot be made.


    Remove the spaces and try again.



    Example:
    1+2 should work by the changes mentioned in kristijan answer.



    Edit:
    Replace the line in @Kristjan Kica answer



    while(ask == 'y' || ask == 'Y') ;                        


    with



    }while(ask == 'y' || ask == 'Y') ;                        


    Edit 2:
    Last closing } should be your main functions closing brace.







    share|improve this answer










    New contributor




    bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer








    edited yesterday





















    New contributor




    bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered yesterday









    bhanu7k

    385




    385




    New contributor




    bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    bhanu7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.












    • Upon running Kristjan's code, it say expected while before }
      – John Hunter
      yesterday










    • Yup it's working... But how can I do the same thing in this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
      – John Hunter
      yesterday












    • Are u getting the output or is it just compiled?
      – bhanu7k
      yesterday










    • You can achieve the same with if statement(remove while inside do-while and try again).
      – bhanu7k
      yesterday


















    • Upon running Kristjan's code, it say expected while before }
      – John Hunter
      yesterday










    • Yup it's working... But how can I do the same thing in this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
      – John Hunter
      yesterday












    • Are u getting the output or is it just compiled?
      – bhanu7k
      yesterday










    • You can achieve the same with if statement(remove while inside do-while and try again).
      – bhanu7k
      yesterday
















    Upon running Kristjan's code, it say expected while before }
    – John Hunter
    yesterday




    Upon running Kristjan's code, it say expected while before }
    – John Hunter
    yesterday












    Yup it's working... But how can I do the same thing in this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
    – John Hunter
    yesterday






    Yup it's working... But how can I do the same thing in this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("nInvalid input! Please try again...nn"); }
    – John Hunter
    yesterday














    Are u getting the output or is it just compiled?
    – bhanu7k
    yesterday




    Are u getting the output or is it just compiled?
    – bhanu7k
    yesterday












    You can achieve the same with if statement(remove while inside do-while and try again).
    – bhanu7k
    yesterday




    You can achieve the same with if statement(remove while inside do-while and try again).
    – bhanu7k
    yesterday










    up vote
    0
    down vote













    Finally solved it. All thanks to Kristjan Kica...



    #include <stdio.h>

    int main(void)
    {
    float num1;
    float num2;
    float ans;
    char symbol;
    char ask;
    char string[100];
    fflush(stdin);
    printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by
    Sankasuvra Bhattacharyan");
    printf("that performs arithmetic operations onntwo numbers.n");
    printf("Please type the expression you want to calculate: ");
    fgets (string , 100 ,stdin);
    if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
    {
    printf("nInvalid input! Please try again...nn");
    main();
    }
    else
    {
    switch(symbol) {
    case '+' : ans = num1 + num2;
    break;
    case '-' : ans = num1 - num2;
    break;
    case '*' :
    case 'x' :
    ans = num1 * num2;
    break;
    case '/' :
    if (num2 == 0) {
    printf("Division by zero is not possible!nPlease try again...nn");
    return main();
    }
    else {
    ans = num1 / num2;
    break;
    }
    default :
    printf("nInvalid input! Please try again...nn");
    return main();
    }
    printf("The answer is %gn",ans);
    printf("nTo use the calculator again, type 'Y'. ");
    printf("To exit, type any other character...n");
    scanf("%s",&ask);
    printf("n");
    if (ask == 'y' || ask == 'Y')
    {
    main();
    }
    else {
    return 0;
    }
    }
    }





    share|improve this answer








    New contributor




    John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













      Finally solved it. All thanks to Kristjan Kica...



      #include <stdio.h>

      int main(void)
      {
      float num1;
      float num2;
      float ans;
      char symbol;
      char ask;
      char string[100];
      fflush(stdin);
      printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by
      Sankasuvra Bhattacharyan");
      printf("that performs arithmetic operations onntwo numbers.n");
      printf("Please type the expression you want to calculate: ");
      fgets (string , 100 ,stdin);
      if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
      {
      printf("nInvalid input! Please try again...nn");
      main();
      }
      else
      {
      switch(symbol) {
      case '+' : ans = num1 + num2;
      break;
      case '-' : ans = num1 - num2;
      break;
      case '*' :
      case 'x' :
      ans = num1 * num2;
      break;
      case '/' :
      if (num2 == 0) {
      printf("Division by zero is not possible!nPlease try again...nn");
      return main();
      }
      else {
      ans = num1 / num2;
      break;
      }
      default :
      printf("nInvalid input! Please try again...nn");
      return main();
      }
      printf("The answer is %gn",ans);
      printf("nTo use the calculator again, type 'Y'. ");
      printf("To exit, type any other character...n");
      scanf("%s",&ask);
      printf("n");
      if (ask == 'y' || ask == 'Y')
      {
      main();
      }
      else {
      return 0;
      }
      }
      }





      share|improve this answer








      New contributor




      John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









        Finally solved it. All thanks to Kristjan Kica...



        #include <stdio.h>

        int main(void)
        {
        float num1;
        float num2;
        float ans;
        char symbol;
        char ask;
        char string[100];
        fflush(stdin);
        printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by
        Sankasuvra Bhattacharyan");
        printf("that performs arithmetic operations onntwo numbers.n");
        printf("Please type the expression you want to calculate: ");
        fgets (string , 100 ,stdin);
        if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
        {
        printf("nInvalid input! Please try again...nn");
        main();
        }
        else
        {
        switch(symbol) {
        case '+' : ans = num1 + num2;
        break;
        case '-' : ans = num1 - num2;
        break;
        case '*' :
        case 'x' :
        ans = num1 * num2;
        break;
        case '/' :
        if (num2 == 0) {
        printf("Division by zero is not possible!nPlease try again...nn");
        return main();
        }
        else {
        ans = num1 / num2;
        break;
        }
        default :
        printf("nInvalid input! Please try again...nn");
        return main();
        }
        printf("The answer is %gn",ans);
        printf("nTo use the calculator again, type 'Y'. ");
        printf("To exit, type any other character...n");
        scanf("%s",&ask);
        printf("n");
        if (ask == 'y' || ask == 'Y')
        {
        main();
        }
        else {
        return 0;
        }
        }
        }





        share|improve this answer








        New contributor




        John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        Finally solved it. All thanks to Kristjan Kica...



        #include <stdio.h>

        int main(void)
        {
        float num1;
        float num2;
        float ans;
        char symbol;
        char ask;
        char string[100];
        fflush(stdin);
        printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by
        Sankasuvra Bhattacharyan");
        printf("that performs arithmetic operations onntwo numbers.n");
        printf("Please type the expression you want to calculate: ");
        fgets (string , 100 ,stdin);
        if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
        {
        printf("nInvalid input! Please try again...nn");
        main();
        }
        else
        {
        switch(symbol) {
        case '+' : ans = num1 + num2;
        break;
        case '-' : ans = num1 - num2;
        break;
        case '*' :
        case 'x' :
        ans = num1 * num2;
        break;
        case '/' :
        if (num2 == 0) {
        printf("Division by zero is not possible!nPlease try again...nn");
        return main();
        }
        else {
        ans = num1 / num2;
        break;
        }
        default :
        printf("nInvalid input! Please try again...nn");
        return main();
        }
        printf("The answer is %gn",ans);
        printf("nTo use the calculator again, type 'Y'. ");
        printf("To exit, type any other character...n");
        scanf("%s",&ask);
        printf("n");
        if (ask == 'y' || ask == 'Y')
        {
        main();
        }
        else {
        return 0;
        }
        }
        }






        share|improve this answer








        New contributor




        John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 11 hours ago









        John Hunter

        74




        74




        New contributor




        John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        John Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            John Hunter is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            John Hunter is a new contributor. Be nice, and check out our Code of Conduct.













            John Hunter is a new contributor. Be nice, and check out our Code of Conduct.












            John Hunter is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372593%2fhow-to-restart-main-from-the-beginning-on-an-error-condition%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            ts Property 'filter' does not exist on type '{}'

            Notepad++ export/extract a list of installed plugins