ToshiXZ
11-18-2008, 10:03 PM
Good 2nd or third program to make.
There is going to be things like fabs which you don't understand, it is simply part of math.h, which has explanations for it all at:
http://www.cplusplus.com/reference/clibrary/cmath/
The
keybd_event(VK_MENU,0x36,0,0);
keybd_event(VK_RETURN,0x1c,0,0);
keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0x36,KEYEVENTF_KEYUP,0);simply pushes alt+enter, and releases it. I added comments to help you understand it. I also put an run-time error, so you can still compile it and see how functions work, but it will tell you that you have an operator bug, so you can't just stamp your name on this and make it public.
I use strings in here, which CAN get complex. op.compare basically compares the string "op" to see if it equals "=" and then if it does, it will do some math. Google anything you don't understand, or ask here.
Here's the code:
#include <iostream> // used to write and get from DOS prompt
#include <windows.h> // used for sending keystrokes, in our case handling full screen
#include <string> // used in handling our operator and name
#include <math.h> // used with square root, floor, ceiling, and powers
using namespace std; //so we don't have to type std::(standard) in front of our iostream library functions
int main() //start of program, void is so no arguments may be accepted by main
{
float num1; //will be used as the variable for storing the first number in a multiple variable needing equation, and for the base number in a lone number needing program
float num2;
float ans; // variable for storing answer
string op; // variable for storing the operator choice
string name; //variable for storing user's name
int dwloop; // fake int never needed, will just be used for a do while loop
// the following code gets windows to push the keys alt and enter, then release them, causing ful screen DOS
keybd_event(VK_MENU,0x36,0,0);
keybd_event(VK_RETURN,0x1c,0,0);
keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0x36,KEYEVENTF_KEYUP,0);
cout << "Hi there! What's your name?" << endl;
getline (cin, name);
do { // calculator loop
dwloop = 0;
do { //operator error loop
system("CLS"); // clears the screen, so multiple instances do not appear after the do while loop
cout << "Hi " << name << "! Welcome to my calculator!" << endl;
cout << "\nPress the corrosponding operator to start an action!";
cout << "\nOptions:";
cout << "\n+ Addition";
cout << "\n- Subtraction";
cout << "\n* Multiplication";
cout << "\n/ Division";
cout << "\n% Modulus";
cout << "\n^ Power";
cout << "\nsqr Square root";
cout << "\nflr Floor";
cout << "\nceil Ceiling";
cout << "\nabs Absolute value";
cout << "\n Choice: ";
getline (cin, op); // takes everything that the user typed and stores in op. Since we use other cin's for integers, we place cin.ignore under them
// below checks to see if an invalid operator, and displays error message
if (op.compare("+") != 0 && op.compare("-")!= 0 && op.compare("*")!= 0 && op.compare("/") != 0 && op.compare("%") != 0 && op.compare("^") != 0 && op.compare("sqr")== 0 && op.compare("flr") != 0 && op.compare("ceil") != 1 && op.compare("abs") != 1) {
system("CLS"); // clears the screen. we don't want the program to ask for an operator while displaying and error message!
cout << "Bad operator choice " << name << "! Please try again!";
system("PAUSE");
} // operator error if end
//end of do while loop for the error message
} while (op.compare("+") == 0 && op.compare("-")== 0 && op.compare("*")== 0 && op.compare("/")== 0 && op.compare("%")== 0 && op.compare("^")== 0 && op.compare("sqr")== 0 && op.compare("flr")== 0 && op.compare("ceil")== 0 && op.compare("abs")== 0);
system("CLS");
cout << "Enter your first/base number: ";
cin >> num1;
cin.ignore(); // read comment below
if (op.compare("sqr") != 0 && op.compare("flr") != 0 && op.compare("ceil") != 0 && op.compare("abs") != 0) {
cout << "Enter your second number: ";
cin >> num2;
cin.ignore(); // so getline() doesn't grab the integers as well
} // 1-number needed if end
if (op.compare("+") == 0) {
ans = num1 + num2;
}
else if (op.compare("-") == 0) {
ans = num1 - num2;
}
else if (op.compare("*") == 0) {
ans = num1 * num2;
}else if (op.compare("/") == 0) {
ans = num1 / num2;
}else if (op.compare("%") == 0) {
ans = (int)num1 % (int)num2;
}
else if (op.compare("^") == 0) {
ans = pow (num1,num2);
}
else if (op.compare("sqr") == 0) {
ans = sqrt(num1);
}
else if (op.compare("ceil") == 0) {
ans = ceil(num1);
}
else if (op.compare("flr") == 0) {
ans = floor(num1);
}
else if (op.compare("abs") == 0) {
ans = fabs(num1);
}
system("CLS");
cout << "\nYour answer is : " << ans << "\n";
system("PAUSE");
} while (dwloop != 100);
return 0;
} // end of main
There is going to be things like fabs which you don't understand, it is simply part of math.h, which has explanations for it all at:
http://www.cplusplus.com/reference/clibrary/cmath/
The
keybd_event(VK_MENU,0x36,0,0);
keybd_event(VK_RETURN,0x1c,0,0);
keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0x36,KEYEVENTF_KEYUP,0);simply pushes alt+enter, and releases it. I added comments to help you understand it. I also put an run-time error, so you can still compile it and see how functions work, but it will tell you that you have an operator bug, so you can't just stamp your name on this and make it public.
I use strings in here, which CAN get complex. op.compare basically compares the string "op" to see if it equals "=" and then if it does, it will do some math. Google anything you don't understand, or ask here.
Here's the code:
#include <iostream> // used to write and get from DOS prompt
#include <windows.h> // used for sending keystrokes, in our case handling full screen
#include <string> // used in handling our operator and name
#include <math.h> // used with square root, floor, ceiling, and powers
using namespace std; //so we don't have to type std::(standard) in front of our iostream library functions
int main() //start of program, void is so no arguments may be accepted by main
{
float num1; //will be used as the variable for storing the first number in a multiple variable needing equation, and for the base number in a lone number needing program
float num2;
float ans; // variable for storing answer
string op; // variable for storing the operator choice
string name; //variable for storing user's name
int dwloop; // fake int never needed, will just be used for a do while loop
// the following code gets windows to push the keys alt and enter, then release them, causing ful screen DOS
keybd_event(VK_MENU,0x36,0,0);
keybd_event(VK_RETURN,0x1c,0,0);
keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0x36,KEYEVENTF_KEYUP,0);
cout << "Hi there! What's your name?" << endl;
getline (cin, name);
do { // calculator loop
dwloop = 0;
do { //operator error loop
system("CLS"); // clears the screen, so multiple instances do not appear after the do while loop
cout << "Hi " << name << "! Welcome to my calculator!" << endl;
cout << "\nPress the corrosponding operator to start an action!";
cout << "\nOptions:";
cout << "\n+ Addition";
cout << "\n- Subtraction";
cout << "\n* Multiplication";
cout << "\n/ Division";
cout << "\n% Modulus";
cout << "\n^ Power";
cout << "\nsqr Square root";
cout << "\nflr Floor";
cout << "\nceil Ceiling";
cout << "\nabs Absolute value";
cout << "\n Choice: ";
getline (cin, op); // takes everything that the user typed and stores in op. Since we use other cin's for integers, we place cin.ignore under them
// below checks to see if an invalid operator, and displays error message
if (op.compare("+") != 0 && op.compare("-")!= 0 && op.compare("*")!= 0 && op.compare("/") != 0 && op.compare("%") != 0 && op.compare("^") != 0 && op.compare("sqr")== 0 && op.compare("flr") != 0 && op.compare("ceil") != 1 && op.compare("abs") != 1) {
system("CLS"); // clears the screen. we don't want the program to ask for an operator while displaying and error message!
cout << "Bad operator choice " << name << "! Please try again!";
system("PAUSE");
} // operator error if end
//end of do while loop for the error message
} while (op.compare("+") == 0 && op.compare("-")== 0 && op.compare("*")== 0 && op.compare("/")== 0 && op.compare("%")== 0 && op.compare("^")== 0 && op.compare("sqr")== 0 && op.compare("flr")== 0 && op.compare("ceil")== 0 && op.compare("abs")== 0);
system("CLS");
cout << "Enter your first/base number: ";
cin >> num1;
cin.ignore(); // read comment below
if (op.compare("sqr") != 0 && op.compare("flr") != 0 && op.compare("ceil") != 0 && op.compare("abs") != 0) {
cout << "Enter your second number: ";
cin >> num2;
cin.ignore(); // so getline() doesn't grab the integers as well
} // 1-number needed if end
if (op.compare("+") == 0) {
ans = num1 + num2;
}
else if (op.compare("-") == 0) {
ans = num1 - num2;
}
else if (op.compare("*") == 0) {
ans = num1 * num2;
}else if (op.compare("/") == 0) {
ans = num1 / num2;
}else if (op.compare("%") == 0) {
ans = (int)num1 % (int)num2;
}
else if (op.compare("^") == 0) {
ans = pow (num1,num2);
}
else if (op.compare("sqr") == 0) {
ans = sqrt(num1);
}
else if (op.compare("ceil") == 0) {
ans = ceil(num1);
}
else if (op.compare("flr") == 0) {
ans = floor(num1);
}
else if (op.compare("abs") == 0) {
ans = fabs(num1);
}
system("CLS");
cout << "\nYour answer is : " << ans << "\n";
system("PAUSE");
} while (dwloop != 100);
return 0;
} // end of main