Caculator?

Posts 1–10 of 10 · Page 1 of 1
Caculator?
I'm starting to learn C++ and I just made this calculator... Tell meh what you think?

Btw this is a Win32 Console app.
Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>

    using namespace std;

int main()
{
       float a;
       float b;
	   float s;
       string choice;
       string cont;

	cout << "Ready to do some math?\nAdd\nSubtract\nMultiply\nDivide\n";
	cin >> choice;
	if (choice == "add")
	{
		cout << "You have Picked add.\n";
		 	{
				cout << "Type in First Number: \n";
				cin >> a;
				cout << "Type in Second Number: \n";
				cin >> b;
				cout << "\n";
				cout << "	" << a <<" + " << b <<" = " <<a+b <<endl;
				cout << "\n";
				cout << "Would you like to continue?\nYes = y\nNo = n\n";
					cin >> cont;
					if (cont == "y")
					{
						main ();
					}
					else (cont == "n");
					{
						return 0;
					}
			}


	}
if (choice == "subtract")
	{
		cout << "You have Picked Subtract.\n";
		 	{
				cout << "Type in First Number: \n";
				cin >> a;
				cout << "Type in Second Number: \n";
				cin >> b;
				cout << "\n";
				cout << "	" << a <<" - " << b <<" = " <<a-b <<endl;
				cout << "\n";
				cout << "Would you like to continue?\nYes = y\nNo = n\n";
					cin >> cont;
					if (cont == "y")
					{
						main ();
					}
					else (cont == "n");
					{
						return 0;
					}
			}


	}
if (choice == "multiply")
	{
		cout << "You have Picked Multiply.\n";
		 	{
				cout << "Type in First Number: \n";
				cin >> a;
				cout << "Type in Second Number: \n";
				cin >> b;
				cout << "\n";
				cout << "	" << a <<" * " << b <<" = " <<a*b <<endl;
				cout << "\n";
				cout << "Would you like to continue?\nYes = y\nNo = n\n";
					cin >> cont;
					if (cont == "y")
					{
						main ();
					}
					else (cont == "n");
					{
						return 0;
					}
			}


	}
if (choice == "divide")
	{
		cout << "You have Picked Divide.\n";
		 	{
				cout << "Type in First Number: \n";
				cin >> a;
				cout << "Type in Second Number: \n";
				cin >> b;
				cout << "\n";
				cout << "	" << a <<" / " << b <<" = " <<a/b <<endl;
				cout << "\n";
				cout << "Would you like to continue?\nYes = y\nNo = n\n";
					cin >> cont;
					if (cont == "y")
					{
						main ();
					}
					else (cont == "n");
					{
						return 0;
					}
			}


	}



char f;
cin >> f;
return 0;
}
Oh the calculator is great, but lol.
I love the recursive call to main();

pretty good so far... nice work.
Quote Originally Posted by why06 View Post
Oh the calculator is great, but lol.
I love the recursive call to main();

pretty good so far... nice work.
yea when i was learning C i used to that too

about the calc, yea keep with it... seriously.. doing little things like that is where you learn at... not just jumping into hacking and etc... otherwise = fail ;o
Quote Originally Posted by 'Bruno View Post
yea when i was learning C i used to that too

about the calc, yea keep with it... seriously.. doing little things like that is where you learn at... not just jumping into hacking and etc... otherwise = fail ;o
lol thanks
now you can try making a string generator. at a dynamic number of characers
Quote Originally Posted by jmend736 View Post
I'm starting to learn C++ and I just made this calculator... Tell meh what you think?

Btw this is a Win32 Console app.
Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>

    using namespace std;

int main()
{
       float a;
       float b;
	   float s;
       string choice;
       string cont;

	cout << "Ready to do some math?\nAdd\nSubtract\nMultiply\nDivide\n";
	cin >> choice;
	if (choice == "add")
	{
		cout << "You have Picked add.\n";
		 	{
				cout << "Type in First Number: \n";
				cin >> a;
				cout << "Type in Second Number: \n";
				cin >> b;
				cout << "\n";
				cout << "	" << a <<" + " << b <<" = " <<a+b <<endl;
				cout << "\n";
				cout << "Would you like to continue?\nYes = y\nNo = n\n";
					cin >> cont;
					if (cont == "y")
					{
						main ();
					}
					else (cont == "n");
					{
						return 0;
					}
			}


	}
if (choice == "subtract")
	{
		cout << "You have Picked Subtract.\n";
		 	{
				cout << "Type in First Number: \n";
				cin >> a;
				cout << "Type in Second Number: \n";
				cin >> b;
				cout << "\n";
				cout << "	" << a <<" - " << b <<" = " <<a-b <<endl;
				cout << "\n";
				cout << "Would you like to continue?\nYes = y\nNo = n\n";
					cin >> cont;
					if (cont == "y")
					{
						main ();
					}
					else (cont == "n");
					{
						return 0;
					}
			}


	}
if (choice == "multiply")
	{
		cout << "You have Picked Multiply.\n";
		 	{
				cout << "Type in First Number: \n";
				cin >> a;
				cout << "Type in Second Number: \n";
				cin >> b;
				cout << "\n";
				cout << "	" << a <<" * " << b <<" = " <<a*b <<endl;
				cout << "\n";
				cout << "Would you like to continue?\nYes = y\nNo = n\n";
					cin >> cont;
					if (cont == "y")
					{
						main ();
					}
					else (cont == "n");
					{
						return 0;
					}
			}


	}
if (choice == "divide")
	{
		cout << "You have Picked Divide.\n";
		 	{
				cout << "Type in First Number: \n";
				cin >> a;
				cout << "Type in Second Number: \n";
				cin >> b;
				cout << "\n";
				cout << "	" << a <<" / " << b <<" = " <<a/b <<endl;
				cout << "\n";
				cout << "Would you like to continue?\nYes = y\nNo = n\n";
					cin >> cont;
					if (cont == "y")
					{
						main ();
					}
					else (cont == "n");
					{
						return 0;
					}
			}


	}



char f;
cin >> f;
return 0;
}
nice work

but for what are the char f;
cin >> f; lines and is float s; couldn't find it used in your code...
Quote Originally Posted by lalakijilp View Post
nice work

but for what are the char f;
cin >> f; lines and is float s; couldn't find it used in your code...
probably as a replacement of system("pause") or getch.
Quote Originally Posted by Hell_Demon View Post
probably as a replacement of system("pause") or getch.
that doesn't explain the float s;
Nice for being a beginner at c++
Some ideas/input you can consider as you continue learning:

*I do think the string check you use is case sensitive (correct me if I'm wrong). If I would use your app, I would input "Add", "Subtract" etc as that's the choices you stated, which would unfortunately not let me add or subtract because you check for "add" and "subtract" (low capitals). An easy way to get around this is output a menu to user like: 1. Add 2. Subtract etc, that way you can just check for a number and don't have to deal with case sensitivity.

*You don't seem to have any check whether the user chooses a non existing operation, try to make one like that in your next project

*You could get rid of a lot of junk code by simply letting the user enter operation to perform, read the numbers and then, based on what operation the user choosed you can output the result whether it's an add, subtract or something else. You could also put the check if the user wants to continue below your if statements, this gets rid of having unneccessary code in every if statement since the user will reach the end of your program anyway.

Just consider this some things to think about, as I said nice work
Code:
if(tolower(choice) == "add")
for case insensitive checking.

Oh, and make sure to check for a divide by 0
Posts 1–10 of 10 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Need help?