-5

In this very basic program which ask the user to input two numbers, and then the program will sum these numbers together. I want at the end to ask the user if he/she want to repeat the program again or to exit the program! for example if he/she press y the program will reask the user to input two number, otherwise the program will be closed. How to do it ?

main(){
float x,y,sum;
printf ("Enter the first number:");
scanf ("%f",&x);
printf ("Enter the second number:");
scanf ("%f",&y);
sum=x+y;
printf ("The total number is:%f",sum);
}
yaya
  • 107
  • 1
  • 1
  • 5
  • A `while` loop, along with reading a `char`. – Haris Jan 22 '16 at 07:58
  • Your code nested in a do while loop, that's the answer – Claudio Cortese Jan 22 '16 at 08:07
  • 2
    You really should spend some time reading some book on C programming, and reading documentation of standard functions like `scanf` & `printf`. Your `main` is wrongly declared. You should enable all warnings & debug info when compiling (`gcc -Wall -Wextra -g`). You should use the debugger. Voting to close your question as a *fix my code* request. – Basile Starynkevitch Jan 22 '16 at 08:20
  • 1
    @yaya, check my answer, it has a tutorial for `do-while` loop. That might help you. – Box Box Box Box Jan 22 '16 at 08:28

3 Answers3

5
main(){
    float x,y,sum;
    char ch;

    do{
    printf ("Enter the first number:");
    scanf ("%f",&x);
    printf ("Enter the second number:");
    scanf ("%f",&y);
    sum=x+y;
    printf ("The total number is:%f",sum);
    printf ("Do you want to continue: y/n");
    scanf (" %c", &ch);
    } while(ch == 'y');
    }

OR you can also try this:

main(){
    float x,y,sum;
    char ch;

    do{
    printf ("Enter the first number:");
    scanf ("%f",&x);
    printf ("Enter the second number:");
    scanf ("%f",&y);
    sum=x+y;
    printf ("The total number is:%f",sum);
    printf ("Do you want to continue: y/n");
    ch = getchar();
    getchar();
    } while(ch == 'y');
    }
Tanveer Shaikh
  • 1,678
  • 14
  • 27
  • This is not the proper declaration of the `main`. It has to be `int main ()`, `int main (void)` or `int main (int argc, char **argv)` – Box Box Box Box Jan 22 '16 at 08:35
  • @AshishAhuja It's an allowed form in the old C90 standard. It could also be allowed on any C compiler given that the form `main()` is documented by the compiler. – Lundin Jan 22 '16 at 08:54
  • @Lundin, is it documented by `gcc`? – Box Box Box Box Jan 22 '16 at 08:56
  • @AshishAhuja in C90 when no explicit type of a function is used, int is implicitly used – Claudio Cortese Jan 22 '16 at 10:02
  • @AshishAhuja `-ffreestanding` is properly documented and allows any form. When compiled for hosted (default/`-fhosted`), gcc does not allow `main()` unless you tell it to compile against the old C90 (`-std=c90`) standard. It will then only give a warning. – Lundin Jan 22 '16 at 10:05
1
int main(void) {
float x,y,sum;
char ch;
do {
printf ("Enter the first number:");
scanf ("%f",&x);
printf ("Enter the second number:");
scanf ("%f",&y);
sum=x+y;
printf ("The total number is:%f\n",sum);
printf ("Do you want to repeat the operation Y/N: ");
scanf (" %c", &ch);
}
while (ch == 'y' || ch == 'Y');
}

This uses a do-while loop. It will continue till the condition in the while of the do-while will return false.

In simple words, whenever the user enters y or Y, the while loop will return true. Thus, it will continue.

Check this example and tutorial for do-while loop.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
-1

[In this program I used 'goto' statement because if I use do while loop then if I enter anything without "Y or y" then the program will be close.To avoid this problem I use 'goto' statement.1

#include<stdio.h>
int main(){
float x, y, sum;
char ch;
print:
    printf ("Enter the first number:");
    scanf ("%f",&x);
    printf ("Enter the second number:");
    scanf ("%f",&y);
    sum=x+y;
    printf ("\nThe total number is:%.2f\n",sum);
again:
    printf ("\n\t\t\t\t\tDo you want to repeat the operation(Y/N): ");
    scanf (" %c", &ch);

    if(ch == 'y' || ch == 'Y'){
        goto print;
    }
    else if(ch == 'n' || ch == 'N'){
        return 0;
    }
    else{
        printf("\n\t\t\t\t\tPlease enter Yes or NO.\n");
        goto again;
    }
   return 0;}