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:
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:
Language is not English as you can see, but you can see what I did.
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.
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; }
Perhaps
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.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"); } }
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.
Is there any way i could have restart option and exit option?
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.
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.
Alright, I will try it. Thanks for help!