1. In the following program I would like to make it so that instead of the program shutting down by inputting a negative number, it shuts down when I press the "esc" key on my keyboard, but continues when I press "enter" |
No reliable way to do this with standard libs alone. You'll need to venture into platform specific libraries to do that.
conio.h (as Kyon suggested) is nonstandard and may not exist on everyone's computer (or even on yours -- it's pretty ancient and it doesn't come with some newer compilers), so I don't recommend using it.
All input with the standard libs is line-by-line. So to receive any input from the user, the user
must press enter. The closest you could get to having Escape exit the program would be to have the user press Escape, then Enter (this could be accomplished with something like what sourena suggested, but that solution is a little fugly, IMO).
Personally I wouldn't worry bother with this. Doing fancy things with the console typically isn't worth the effort, IMO. The console isn't really designed to be user friendly.
void calculate2(int x, int y) // Since we dont RETURN an int or anything else, this function returns "void" |
This is pretty good advise, since you had a function that wasn't returning anything.
Although better advise would be to return something, and not output to cout.
The function's name is "calculate", not "calculate_and_print". So it should only calculate the number. Printing the result is (or should be) outside its scope.
What if you want to do more than one calculation with that number? What if you don't want to print the result, but what to just keep it for later use? Or what if you want to output it to a file instead of printing it?
Your function doesn't allow for any of that. The value is printed and then immediately forgotten by the program. Thereby limiting the function's usefullness to one very specific task.
To make reusable code, you generally want to keep the jobs of functions very minimalist. Have them to a very specific job
and nothing more.
A better calculate function would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int calculate1(int nVar1, int nVar2)
{
int nVar3 = nVar1/nVar2-1;
// calculate the modulus
int nVar = nVar1 - nVar2 * nVar3;
return nVar;
// note I never send the result to 'cout', I just calculate it and return it.
// let the calling funtion send it to cout if you want to print it.
}
|
2. Make the functions calculate1 and calculate2, in to a seperate file so that I can use it with the #include command with my other programs. |
1 2 3 4
|
// calculate.h
int calculate1(int,int);
int calculate2(int,int);
|
1 2 3 4 5 6 7
|
// calculate.cpp
#include "calculate.h"
/*
put function bodies for calculate1 and calculate2 here
*/
|
1 2 3 4 5 6 7 8 9 10 11 12
|
// main.cpp
#include <iostream>
#include "calculate.h"
int main()
{
// now we can call it from another cpp file:
std::cout << calculate1(1,2);
return 0;
}
|
other notes
You should try to name your functions to be more clear. The function name should describe what the function does. This makes your code much easier to follow and reduces mistakes caused by calling the wrong function.
"calculate1" leaves a lot to be desired. What is it calcuating?
From the comments I see that it's calculating the modulus. So the name "CalcModulus" makes a bit more sense, doesn't it?