Thread: Exit Option

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    5

    Exit Option

    Hello guys, I made a simple calculator in C, and I am trying to implement exit option (something like "Press ESC to close the program").
    This is how the program looks like:
    Attached Images Attached Images Exit Option-capture-png 

  2. #2
    Registered User
    Join Date
    Dec 2019
    Posts
    5
    Language is not English as you can see, but you can see what I did.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    For future reference, please copy/paste your code between [code][/code] tags.
    Fuzzy pictures just don't work that well; we can't quote your code for mistakes and can't try your code to check results.

    Detecting keys like ESC requires knowledge of your specific OS and compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    5
    It doesnt have to be ESC, any button is good.
    Code:
    int main(){
        int odgovor = 1;
        float a, b;
        do
        {
        int c;
        printf("Unesi dva broja:\n");
        scanf("%f%f", &a, &b);
        printf("Lista operacija:\n1-Sabiranje\n2-Oduzimanje\n3-Mnozenje\n4-Deljenje\nOdaberi operaciju: ");
        scanf("%d", &c);
        switch(c) {
        case 1: printf("Zbir %f i %f je %f", a, b, a+b); break;
        case 2: printf("Razlika %f i %f je %f", a, b, a-b); break;
        case 3: printf("Proizvod %f i %f je %f", a, b, a*b); break;
        case 4: printf("Kolicnik je %f i %f je %f", a, b, a/b); break;
        default: printf("Unesi ispravan broj");}
        printf("\n\nAko hoces da restartujes program upisi 1; ako hoces da iskljucis program: ");
        scanf("%d", &odgovor);
        } while(odgovor == 1);
    
    
        return 0;
        }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Perhaps
    Code:
    while ( scanf("%f%f", &a, &b) == 2 ) {
        printf("Lista operacija:\n1-Sabiranje\n2-Oduzimanje\n3-Mnozenje\n4-Deljenje\nOdaberi operaciju: ");
        scanf("%d", &c);
        switch(c) {
        case 1: printf("Zbir %f i %f je %f", a, b, a+b); break;
        case 2: printf("Razlika %f i %f je %f", a, b, a-b); break;
        case 3: printf("Proizvod %f i %f je %f", a, b, a*b); break;
        case 4: printf("Kolicnik je %f i %f je %f", a, b, a/b); break;
        default: printf("Unesi ispravan broj");
      }
    }
    You can type anything you want to exit, as the scanf will fail if you type in something that doesn't look like a float.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2019
    Posts
    5
    Is there any way i could have restart option and exit option?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Sure, why not.
    It's just code to do what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Maybe?


    Code:
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,stdin) ) {
      float a,b;
      if ( sscanf(buff,"%f %f", &a, &b) == 2 ) {
        calculate(a,b);
      }
      else if ( strncmp(buff,"exit",4) == 0 ) {
        // user wants to go
      } 
      // other matches?
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2019
    Posts
    5
    Alright, I will try it. Thanks for help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-13-2016, 07:17 AM
  2. is C++ or PHP the best option?
    By Yarin in forum Tech Board
    Replies: 7
    Last Post: 10-13-2009, 01:04 PM
  3. How to do the option for Yes and No?
    By karipap in forum C Programming
    Replies: 18
    Last Post: 03-17-2009, 09:29 AM
  4. y or n option?
    By Munkey01 in forum C++ Programming
    Replies: 11
    Last Post: 12-24-2002, 10:41 AM

Tags for this Thread