Well, I've never used it but I think cin.get() SHOULD make your program wait for a key before exiting. Code looks okay at first glance.
Easiest way to see if this is the problem IMO is to run a DOS prompt, cd to the directory the executable is in and run it from there, then the screen output will be preserved even if the program is terminating automatically without waiting for a key.
If you are developing for Win32, you could try system("pause"); at the end (you'll need stdlib.h) but this is highly implementation dependant.
Calculator (I'm total newbie!)
I think you should use switch instead of many if/else. Because switch is faster than if/else.
switch(var)
case '+':
....
break;
case '-':
....
break;
case '/':
....
break;
case '*':
....
break;
default:
....
}
switch(var)
case '+':
....
break;
case '-':
....
break;
case '/':
....
break;
case '*':
....
break;
default:
....
}
you can just use getchar() for C code too
that makes to program wait till you press a key
just put that at the end of your code
that makes to program wait till you press a key
just put that at the end of your code
Quote:
Originally Posted By Easily Confused
Well, I've never used it but I think cin.get() SHOULD make your program wait for a key before exiting. Code looks okay at first glance.
Easiest way to see if this is the problem IMO is to run a DOS prompt, cd to the directory the executable is in and run it from there, then the screen output will be preserved even if the program is terminating automatically without waiting for a key.
If you are developing for Win32, you could try system("pause"); at the end (you'll need stdlib.h) but this is highly implementation dependant.
Well it works fine I see! so just the pause thing, and Invisal, I'm trying not to change the code completely, but thanks for the advice :-D I'll use it the next time!
Quote:Original post by Endar
You can have an if inside an if inside an if inside an if, ad infinitum.
Okay, well not really to infinity, but I don't know the limit. Does anyone know? Does anyone have the C/C++ standard lying around?
There is no limit. The program needs to fit into memory, though.
That's correct :-D
Hehe!
found out the calculator close problem, it seems that the last time you press enter (at the second number) it activates the next cin.get() :-) so just placed one more and it works! :-D
Hehe!
found out the calculator close problem, it seems that the last time you press enter (at the second number) it activates the next cin.get() :-) so just placed one more and it works! :-D
Yeah, actually don't use system("pause") ever. Someone has just pointed out in another thread that if someone malicious puts a program called pause.exe in the same directory as your program, they can do anything they want when your program ends. Hadn't thought of that.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement