My calculator - evaluate it please :)

Hi there, this is actually my first real program i've made. took me only 10 min to make or so and im doing it to "demonstrate" wether im feeling confident enough with the if else statements. Next im moving on to Loops :) no doubt it can be made better, but i just started programming a few days ago so hopefully im doing progress. Give me ur thoughts :)

thanks for reading!

#include <iostream>
#include <math.h>

using namespace std;



int main()
{
cout << "type 2 numbers " << endl;
int result;
int numbera;
int numberb;
cin >> numbera;
cin>>numberb;

cout << "you choose the numbers " << numbera << " " << numberb << endl;
cout << "now, type adding, subtracting, multiply or division." << endl;
string calcMethod;
cin >> calcMethod;
if(calcMethod == "adding")
{
result = numbera + numberb;
cout << "these are the results" << endl;
cout << numbera << " + " << numberb << " = " << result << endl;
}
else if(calcMethod == "subtracting")
{
result = numbera - numberb;
cout << "these are the results" << endl;
cout << numbera << " - " << numberb << " = " << result << endl;
}
else if(calcMethod == "multiply")
{
result = numbera * numberb;
cout << "these are the results;" << endl;
cout << numbera << " * " << numberb << " = " << result << endl;
}
else if(calcMethod == "division")
{
result = numbera / numberb;
cout << "these are the results" << endl;
cout << numbera << " / " << numberb << " = " << result << endl;
}
else
{
cout << "you failed to spell correctly!" << endl;
}

return 0;
}
Not bad for 10 minutes.
You don't need to #include<math.h> because you don't use any functions there, and if you need the math functions you should #include<cmath>.
You could improve it with switch statements. See the bottom of this page:
http://www.cplusplus.com/doc/tutorial/control/
Very good. I really don't like to criticize. but i recommend above return 0; you should place system("pause"); so you can read what the outcome is.

but none less it's very good i have been programming for a month you try your best and keep at it! :).

There i so much room for improvement with this program. improve it as you learn and it will be a very good calculator. Good Luck.

also learn at a steady pace, dont rush like i did you then forget the basics.
Last edited on
If your going to use strings instead of char* (which is good) you might want to get input using "getline", like so:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

using namespace std;

int main() {
  string str;
  cout << "Enter a string: ";
  getline( cin, str ); //get a whole line, including ' ' (space) and '\t' (tab) characters

  return 0;
}


However don't use "cin>>" and "getline" together before you get better at coding. In this program you don't need it, but rememberr that "cin>>" will stop at the first whitespace character, even though strings can have spaces in them. So If the user entered "adding and subtracting" in your program, you would still only have "adding" in the variable calcMethod. Go here http://cplusplus.com/reference/string/getline if you need more detailed information.
Last edited on
thanks mathhead200 i was used to cin >>. but i never knew there was alternatives thanks you taught me something.

There are many. The global "getline" function comes form the <string> header, but beacuse cin is an istream object, you can invoke any of it's member functions (aka methods) also. See http://cplusplus.com/reference/iostream/istream for a list. Same goes for cout and cerr, that is, they are ostream objects. See http://cplusplus.com/reference/iostream/ostream for a list of their methods.

PS: If your not familiar with cerr don't worry. It's similar to cout, but used for printing error messages, instead of normal output... not that you have to use it mind you.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  if( argc <= 1 ) {
    cerr << "No command line arguments." << endl; //print an error message
    return 1; //a non 0 return signifies an error
  } else for( int i = 1; i < argc; i++ ) {
    //print all the arguments passed to the program
    cout << argv[i] << endl;
  }
  return 0;
}
Last edited on
thanks.
PracticingProgrammer wrote:
i recommend above return 0; you should place system("pause"); so you can read what the outcome is.

If you're going to do this, please don't use system("pause"); ( see http://www.cplusplus.com/forum/articles/11153/ ). Use std::cin.ignore(std::numeric_limits<std::streamsize>::max, '\n'); instead. I know it is longer but it does not have the drawbacks that using the system() function has. Really, you should never use the system() function at all. It's probably ok for programs you're writing to learn or for your own usage but I say it's best not to get into the habit of using it.

Mathhead200 wrote:
PS: If your not familiar with cerr don't worry. It's similar to cout, but used for printing error messages, instead of normal output... not that you have to use it mind you.

It depends what kind of program you're writing. If you're writing a "filter" style program (one which takes input (either via the input stream or the command-line) and then produces output), you should send actual output to the standard output stream and error messages to the error stream. If you're writing an interactive program (one which requires the user to enter commands in real-time) then I would recommend you send all output to the standard output stream. If you're writing a graphical program and want to have a program log then I would suggest sending all output to the error stream.
Last edited on
You can experiment with cin, cout, and cerr redirection if you want...
Just run your programs like this:

prog.exe <in.txt

  this will get input (cin) from a file (in.txt) instead of from the keyboard
  here cout and cerr act like normal
  you could use this if you needed a lot of input and you wanted to store it all in a text file to save time
--------------------------------------------------------------------------------
prog.exe >out.txt

  this will send the output (cout) to a file (out.txt) instead of the screen
  here cin and cerr act like normal
  you could use this if you wanted to save the output of a program and maybe show it to someone
--------------------------------------------------------------------------------
prog.exe 2>err.txt

  this will send the errors (cerr) to a file (err.txt) instead of the screen
  here cin and cout act like normal
  you could use this if you wanted to make an error log file
--------------------------------------------------------------------------------
prog <in.txt >out.txt 2>err.txt

  this does all three of the above, gets input from one file, and sends output and errors to two other seprate files
--------------------------------------------------------------------------------
prog.exe >>out.log 2>>err.log

  note that above I used two > symbols (i.e. ">>"), this will append the output and/or errors to the file instead of replacing the old content.
  use this if you want to save the output or errors of separate executions of program(s) to the same file

Last edited on
Thanks i never new how bad it was to use system("pause"); but i don't understand why the book i am reading teaches it to you. they should make you learn std::cin.ignore(std::numeric_limits<std::streamsize>::max, '\n'); instead. to get used to it. it's a bit of a mouthful and will take some getting used to.
closed account (3hM2Nwbp)
You might consider checking if the denominator is zero in your division code-branch and display the appropriate message, otherwise the program will crash. Input validation would also be a bonus. I believe there are a couple articles floating around this site that cover input validation.
Last edited on
practicingprogrammer its because the parameters are a little more complex, and system("pause") is fine for simple programs that you dont plan to market, and you wont see it in more complex things anyways because virtually no one sells/uses console applications anymore.

system("pause"); is fine if youre just screwing around at last IMO
Thanks ascii. I was worried i was taught wrongly.
Topic archived. No new replies allowed.