I made a C++ console program (it's a calculator) and I was wondering how I could put this into the Windows Template(http://tinyurl.com/63ydtc) to make it cleaner looking and a true Windows program. Example of how I want it to look: http://tinyurl.com/623drd
Except I want my program in that window ;).
Code:
// Calculator
// Include files below
#include <iostream>
#include <string>
#include <math.h>
#include <cstdlib>
usingnamespace std;
//Beginning of calulator
int main()
{
//Mathematical operators' corresponding number
//Done = 5;
//Subtraction = 4;
//Addition = 3;
//Multiplication = 2;
//Division = 1;
int answer;
//Answer must be initialized to be used
cout << "What basic mathematical operator shall I perform ?" << endl;
cout << "Please answer with: Division(1), Multiplication(2), Addition(3), Subtraction(4)" << endl;
cout << "Square Root(5), Sine(6), Cosine(7), Tangent(8), Exponetial(9) or End Program(10)" << endl;
cout << "Want a operator not seen here? More operators can be added at your request!" << endl;
cin >> answer;
while(answer!=10) // This begins a loop that will only end when answer is equal to 10
{
if (answer == 1) //Division section of the calculator
{
// Long double is the type of variable that can have up to 15 digits
// Again variables must be initialized before using them.
// However because of the braces around this section the variables
// are local and can be initialized/used in other sections again
longdouble value1;
longdouble value2;
// This asks for values and then inputs the values into the variables
cout << "Input value to be divided: ";
cin >> value1;
cout << "Input the dividend: ";
cin >> value2;
//Dividing the values and storing the answer to value1
value1 /= value2;
cout << "Answer: "<< value1 << endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 2) //Multiplication section of the calculator
{
longdouble value1;
longdouble value2;
cout << "Input first number: ";
cin >> value1;
cout << "Input the second number: ";
cin >> value2;
value1*=value2;
cout << "Answer: "<< value1 << endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 3) //Addition section of the calculator
{
longdouble value1;
longdouble value2;
cout << "Input the first number : ";
cin >> value1;
cout << "Input the second number: ";
cin >> value2;
value1+=value2;
cout << "Answer: "<< value1 << endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 4) //Subtraction section of the calculator
{
longdouble value1;
longdouble value2;
cout << "Input value to be subtracted from: ";
cin >> value1;
cout << "Input the number to subtract from the first: ";
cin >> value2;
value1-=value2;
cout << "Answer: "<< value1 << endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 5) //root section of the calculator
{
longdouble value1;
longdouble value2;
cout << "Input value to be square rooted: ";
cin >> value1;
value2 = sqrt (value1);
cout << "Answer: "<< value2 << endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 6) //sine section of the calculator
{
longdouble value1;
longdouble value2;
cout << "Input value to get the sine for: ";
cin >> value1;
value2 = sin (value1);
cout << "Answer: "<< value2 << endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 7) //cosine section of the calculator
{
longdouble value1;
longdouble value2;
cout << "Input value to get the cosine for: ";
cin >> value1;
value2 = cos (value1);
cout << "Answer: "<< value2 << endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 8) //tangent section of the calculator
{
longdouble value1;
longdouble value2;
cout << "Input value to get the tangent for: ";
cin >> value1;
value2 = tan (value1);
cout << "Answer: " << value2 << endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 9) //exponetial section of the calculator
{
longdouble value1;
longdouble value2;
cout << "Input value to raise exponentially: ";
cin >> value1;
cout << "Input exponet: ";
cin >> value2;
cout << "Answer: "<< pow (value1,value2)<< endl <<endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
else
{
cout << "Please input a correct number" << endl;
cin >> answer;
}
// Braces are needed at each level of abstraction
}
// Return value shows that program executed as expected and then ends the program
return 0;
}
I dont know windows programming but a way to improve this would be to give the user and unlimited amount of numbers they could multiply, add, subtract, ect. Instead of value1 * value2 have:
answer *= value1;
replace the * sign with whatever operator you need. You would have to modify the code some so it asks the user if it wants to input another value but thats not hard.
thanks for the advice but come on could this really be that hard? I've figured out how to put text in the box but I need to have the entire program in the box. This would really help me as this is a project. Thanks. Heres a pic of it in execution:
<a href="http://img56.imageshack.us/my.php?image=windowsprogramexecutionud5.png"
@bananae
WTL is not necessarily that hard...if you understand Windows API programming. You can, as firedraco suggest, go to msdn and try to pick up odds and ends as you struggle forward but I would suggest buying a book - “Programming Windows (5th ed.)” by Charles Petzold.
If you want to have more of a look at WTL then you could start here:
http://www.codeproject.com/KB/wtl/
Create a dialog based appliction using MFC in VS, and put a edit box on dialog,
then
1)instead of cout, use this->Edit1->Addstring("data")
2) put thereanother edit box to get input data
Alas, bananae, if you have to ask, you are way over your head.
As Grey Wolf indicated, neither Win32 API nor WTL are particularly difficult, but it is an entirely new programming paradigm for you, and takes a fair amount of study and effort to get the hang of it.
I would recommend getting C++Builder Explorer 2006, since that gives you a very nice IDE and a powerful and easy to use visual component library. Drag and drop stuff on your forms, set some properties, double click the events you want to handle (like button presses) and you'll be automatically placed in a spot where you can add your code. And it is 100% free. http://cc.codegear.com/free/cppbuilder
No matter what you do, you've got a bit of a learning curve ahead of you. I also endorse Grey Wolf's recommendation to get yourself a textbook.
Another alternative would be to check out Tcl/Tk or Expect. (But that's yet another language. Alas.)