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












1















#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




















  • 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
    Nov 19 '18 at 10:27






  • 2





    As for "repeating" something, use loops.

    – Some programmer dude
    Nov 19 '18 at 10:28











  • Possible duplicate of calling main() in main() in c

    – borrible
    Nov 19 '18 at 10:31






  • 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
    Nov 19 '18 at 10:35






  • 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
    Nov 19 '18 at 10:59
















1















#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




















  • 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
    Nov 19 '18 at 10:27






  • 2





    As for "repeating" something, use loops.

    – Some programmer dude
    Nov 19 '18 at 10:28











  • Possible duplicate of calling main() in main() in c

    – borrible
    Nov 19 '18 at 10:31






  • 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
    Nov 19 '18 at 10:35






  • 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
    Nov 19 '18 at 10:59














1












1








1








#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
















#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















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 16:36









HostileFork

25.6k778133




25.6k778133










asked Nov 19 '18 at 10:26









John HunterJohn Hunter

134




134








  • 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
    Nov 19 '18 at 10:27






  • 2





    As for "repeating" something, use loops.

    – Some programmer dude
    Nov 19 '18 at 10:28











  • Possible duplicate of calling main() in main() in c

    – borrible
    Nov 19 '18 at 10:31






  • 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
    Nov 19 '18 at 10:35






  • 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
    Nov 19 '18 at 10:59














  • 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
    Nov 19 '18 at 10:27






  • 2





    As for "repeating" something, use loops.

    – Some programmer dude
    Nov 19 '18 at 10:28











  • Possible duplicate of calling main() in main() in c

    – borrible
    Nov 19 '18 at 10:31






  • 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
    Nov 19 '18 at 10:35






  • 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
    Nov 19 '18 at 10:59








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
Nov 19 '18 at 10:27





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
Nov 19 '18 at 10:27




2




2





As for "repeating" something, use loops.

– Some programmer dude
Nov 19 '18 at 10:28





As for "repeating" something, use loops.

– Some programmer dude
Nov 19 '18 at 10:28













Possible duplicate of calling main() in main() in c

– borrible
Nov 19 '18 at 10:31





Possible duplicate of calling main() in main() in c

– borrible
Nov 19 '18 at 10:31




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
Nov 19 '18 at 10:35





@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
Nov 19 '18 at 10:35




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
Nov 19 '18 at 10:59





@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
Nov 19 '18 at 10:59












3 Answers
3






active

oldest

votes


















7














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
    Nov 19 '18 at 10:37













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

    – Kristjan Kica
    Nov 19 '18 at 10:42











  • 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
    Nov 19 '18 at 10:50













  • 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
    Nov 19 '18 at 11:36











  • Please give an example @paddy... I am really new to programming....

    – John Hunter
    Nov 19 '18 at 11:58



















0














@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


























  • Upon running Kristjan's code, it say expected while before }

    – John Hunter
    Nov 19 '18 at 12:50











  • 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
    Nov 19 '18 at 13:23













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

    – bhanu7k
    Nov 19 '18 at 13:29











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

    – bhanu7k
    Nov 19 '18 at 13:31



















0














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























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


    }
    });














    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









    7














    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
      Nov 19 '18 at 10:37













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

      – Kristjan Kica
      Nov 19 '18 at 10:42











    • 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
      Nov 19 '18 at 10:50













    • 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
      Nov 19 '18 at 11:36











    • Please give an example @paddy... I am really new to programming....

      – John Hunter
      Nov 19 '18 at 11:58
















    7














    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
      Nov 19 '18 at 10:37













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

      – Kristjan Kica
      Nov 19 '18 at 10:42











    • 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
      Nov 19 '18 at 10:50













    • 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
      Nov 19 '18 at 11:36











    • Please give an example @paddy... I am really new to programming....

      – John Hunter
      Nov 19 '18 at 11:58














    7












    7








    7







    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 Nov 20 '18 at 12:36

























    answered Nov 19 '18 at 10:32









    Kristjan KicaKristjan Kica

    2,2071926




    2,2071926













    • 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
      Nov 19 '18 at 10:37













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

      – Kristjan Kica
      Nov 19 '18 at 10:42











    • 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
      Nov 19 '18 at 10:50













    • 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
      Nov 19 '18 at 11:36











    • Please give an example @paddy... I am really new to programming....

      – John Hunter
      Nov 19 '18 at 11:58



















    • 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
      Nov 19 '18 at 10:37













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

      – Kristjan Kica
      Nov 19 '18 at 10:42











    • 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
      Nov 19 '18 at 10:50













    • 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
      Nov 19 '18 at 11:36











    • Please give an example @paddy... I am really new to programming....

      – John Hunter
      Nov 19 '18 at 11:58

















    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
    Nov 19 '18 at 10:37







    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
    Nov 19 '18 at 10:37















    You have called main elsewhere. Let me edit my answer. The same principle applies

    – Kristjan Kica
    Nov 19 '18 at 10:42





    You have called main elsewhere. Let me edit my answer. The same principle applies

    – Kristjan Kica
    Nov 19 '18 at 10:42













    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
    Nov 19 '18 at 10:50







    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
    Nov 19 '18 at 10:50















    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
    Nov 19 '18 at 11:36





    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
    Nov 19 '18 at 11:36













    Please give an example @paddy... I am really new to programming....

    – John Hunter
    Nov 19 '18 at 11:58





    Please give an example @paddy... I am really new to programming....

    – John Hunter
    Nov 19 '18 at 11:58













    0














    @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


























    • Upon running Kristjan's code, it say expected while before }

      – John Hunter
      Nov 19 '18 at 12:50











    • 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
      Nov 19 '18 at 13:23













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

      – bhanu7k
      Nov 19 '18 at 13:29











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

      – bhanu7k
      Nov 19 '18 at 13:31
















    0














    @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


























    • Upon running Kristjan's code, it say expected while before }

      – John Hunter
      Nov 19 '18 at 12:50











    • 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
      Nov 19 '18 at 13:23













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

      – bhanu7k
      Nov 19 '18 at 13:29











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

      – bhanu7k
      Nov 19 '18 at 13:31














    0












    0








    0







    @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















    @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














    share|improve this answer



    share|improve this answer








    edited Nov 19 '18 at 13:12

























    answered Nov 19 '18 at 12:24









    bhanu7kbhanu7k

    536




    536













    • Upon running Kristjan's code, it say expected while before }

      – John Hunter
      Nov 19 '18 at 12:50











    • 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
      Nov 19 '18 at 13:23













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

      – bhanu7k
      Nov 19 '18 at 13:29











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

      – bhanu7k
      Nov 19 '18 at 13:31



















    • Upon running Kristjan's code, it say expected while before }

      – John Hunter
      Nov 19 '18 at 12:50











    • 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
      Nov 19 '18 at 13:23













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

      – bhanu7k
      Nov 19 '18 at 13:29











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

      – bhanu7k
      Nov 19 '18 at 13:31

















    Upon running Kristjan's code, it say expected while before }

    – John Hunter
    Nov 19 '18 at 12:50





    Upon running Kristjan's code, it say expected while before }

    – John Hunter
    Nov 19 '18 at 12:50













    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
    Nov 19 '18 at 13:23







    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
    Nov 19 '18 at 13:23















    Are u getting the output or is it just compiled?

    – bhanu7k
    Nov 19 '18 at 13:29





    Are u getting the output or is it just compiled?

    – bhanu7k
    Nov 19 '18 at 13:29













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

    – bhanu7k
    Nov 19 '18 at 13:31





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

    – bhanu7k
    Nov 19 '18 at 13:31











    0














    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




























      0














      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


























        0












        0








        0







        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













        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












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 16:26









        John HunterJohn Hunter

        134




        134






























            draft saved

            draft discarded




















































            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.




            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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()