Hi there. Im having this:
Now, if the user press "Enter" the program starts again from the begining. If the Escape key is pressed, just quit the program.Code:printf("Press Enter to continue or Esc to leave the program.");
Any tips?
Thx
July
Hi there. Im having this:
Now, if the user press "Enter" the program starts again from the begining. If the Escape key is pressed, just quit the program.Code:printf("Press Enter to continue or Esc to leave the program.");
Any tips?
Thx
July
I presume you want to be able to read a key without the user having to hit enter first. That's not possible in standard C. Check the FAQ, a couple of system-specific methods are covered.
I dont know If anyone got it, but its a program to change Celsius to Fahrenheit.
Pressing Enter will start the program again... =PCode:main(){ float var, opc; printf("Type the value:"); scanf("%f", &var); printf("\n(1)Celsius to Fahrenheit\n(2)Fahrenheit to Celsius\n\nType your number:"); scanf("%f", &opc); if (opc == 1) { var = var * 9 / 5; var = var + 32; } if (opc == 2) { var = var - 32; var = var * 5 / 9; } printf("%.1f", var); system ("pause > null"); printf("Press Enter to continue or Esc to leave the program."); }
Here's the first result I found in Google: Getch() Esc Key - C And C++ | Dream.In.Code
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
I need something like, "Press Enter do start the program again".
What do I need to do?
Now I just need to keep restarting it over and over, till esc is pressed.Code:#include <stdio.h> #include <stdlib.h> int calc() { float var, opc; int cmd; cmd = 0; printf("Type the temperature: "); scanf("%f", &var); printf("\n(1)Celsius to Fahrenheit\n(2)Fahrenheit to Celsius\n\nType here: "); scanf("%f", &opc); if (opc == 1) { var = var * 9 / 5; var = var + 32; } if (opc == 2) { var = var - 32; var = var * 5 / 9; } printf("%.1f", var); system ("pause > null"); return 0; } int main() { calc(); }
Thx. =)
Have you learned how to use while loops yet?
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
I used the do/while, and its ok now. thx for the help
For anyone how ever needs it, here is the code:
Code:/* Celsius to Fahrenheit and vice-versa */ #include <stdio.h> #include <stdlib.h> main() { float var, opc; int c; do{ printf("Type the value: "); scanf("%f", &var); printf("\n(1)Celsius to Fahrenheit\n(2)Fahrenheit to Celsius\n\nOption: "); scanf("%f", &opc); if (opc == 1) { var = var * 9 / 5; var = var + 32; } if (opc == 2) { var = var - 32; var = var * 5 / 9; } printf("%.1f\n\n", var); printf("Type 1 to continue or 0 to exit: "); scanf("%d", &c); system ("cls"); }while (c != 0); }
That'll only work on DOS/Windows though.Code:system ("cls");
On UNIX it's "clear" instead of "cls".
If your teacher tries running it on a different OS, it won't clear the screen and say that it doesn't know what "cls" is.
One way to make it work on any OS is to just print about 50 '\n' characters instead of clearing the screen (although it won't look at nice, since you can just scroll up in the command window and see all the previous output).
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
Or just note that almost all actual command line tools (with the possible exception of the "more"-like tools) donīt bother with clearing the screen at all.
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.